-2

So i've began coding a small game and need the 'enemys' to spawn from two points.

Heres my code for the enemy spawn;

public void createEnemy(int enemy_count){
    for(int i = 0; i < enemy_count; i++){
        addEntity(new Enemy((200), -10, tex, this, game));
    }
}

As you can see the '(new Enemy((200)' is the point where one enemy would spawn. The other would be at 450.

so how do i alternate between these two point where the enemy spawns each time they are killed?

********************EDIT

// For respawn enemy
    if(y > (Game.HEIGHT * Game.SCALE)){
        x = 450;
        y = -10;
    }

Okay im pretty sure ive got to randomize that as well for it to work? ideas? (x needs to be random 450 or 200).

Thanks, Max

3 Answers3

1

If you want to alternate between 200 and 450 on every other iteration:

addEntity(new Enemy((i % 2 == 1 ? 200 : 450), -10, tex, this, game));

This uses the modulo operator % to determine whether the loop counter i is odd or even and uses the ternary operator to select either 200 or 450.

If you want to pseudorandomly choose one or the other, you can use Math.Random() instead, as @Chrisky expounded in a comment:

addEntity(new Enemy((Math.Random() > 0.5 ? 200 : 450), -10, tex, this, game));

Edit: You can apply the same patterns to the second block of code you've added to your question.

if (y > (Game.HEIGHT * Game.SCALE)) {
    x = (Math.Random() > 0.5 ? 200 : 450);
    y = -10;
}
tgies
  • 694
  • 4
  • 19
  • Ahh Okay ive got you. Thanks for the comment however i think i have got to randomize the part under edit for it work properly. – user3542953 Apr 16 '14 at 21:28
  • @user3542953 You can use the same pattern. Please review my edited answer, and click the checkmark to accept my answer if it helped you :) – tgies Apr 16 '14 at 21:35
  • Much appricated, One last thing, How would i random THIS -- //For speed control private int speed = (3); to pick a number between 1 & 8? – user3542953 Apr 16 '14 at 21:41
  • See this question, in particular the first answer, to find out how to get a given range of numbers out of `Math.Random()`: http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java – tgies Apr 16 '14 at 21:47
  • Perfect thanks for you help tgies! As you can tell im farily new to java but really want to code games for phones! Have you got any learning material that would be useful? Again thanks for the help saved me time, Max. – user3542953 Apr 16 '14 at 21:53
  • @user3542953 The funny thing is that I don't actually particularly know Java so I'm not sure. The best advice I can give you is to keep asking questions and working on projects that are interesting to you and cause you to try new things with the language. – tgies Apr 16 '14 at 22:00
0

Select a location at random:

Random random = new Random(); // Somewhere at the top of your code
addEntity(new Enemy((random.nextBoolean() ? 200 : 450), -10, tex, this, game));

Alternating locations, without dependency on the loop counter variable. This is useful if you spawn enemies outside of the loop as well. (For example, other loops for other types of enemies.)

bool flag = false; // Somewhere at the top of your method for spawning enemies.
addEntity(new Enemy((flag = !flag ? 200 : 450), -10, tex, this, game));
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

If you want the monster to spawn at a random undetermined location each time, you should generate a random value, in your case you only have two random points so you want 0 or 1.

Once you create a random number (either 0 or 1) check the value of the random number, and then turns it into either 250 or 400. This can be done using an associate array, or (much simpler) an if else statement.

See:

Generating a Random Number between 1 and 10 Java

Also, you should know there's no such thing as truly random. Some people insist on calling it psuedorandom but I find that somewhat pedantic.

Community
  • 1
  • 1
GriffinG
  • 662
  • 4
  • 13