Jump to content

How can I create a random spawn point for enemies?


WhiteSoul
 Share

Recommended Posts

Hello. :)

 

I'm almost finishing my first game, but I'm stuck with the enemies issue...

I want that the enemies comes from the upper part of the screen at random intervals but I can't do it.

 

I was looking for tutorials but I don't know how to do it.

I think that I need to create a group for the enemies and I did that already. :)

 

Thank you so much in advance. :D

 

Regards, Daniel.

Link to comment
Share on other sites

Yes, it worked! Finally! :D

I'm so happy my first game is almost done. ^^

 

But now I want to make the enemy group disappear when they hit the player, I was using "enemies.kill()" but that only kills the enemy that touched the player, the other ones keep appearing.

I want to use that so it only appears on the screen "Game Over".

 

Thank you guys again for all the interest. :)

Link to comment
Share on other sites

Yes, I'm using a colliding system, so the thing that I want to achieve is that when one enemy of the group touches the player the whole group disappear.

 

I already have the text "Game Over" placed but the problem is that the enemies keep appearing. :)

Link to comment
Share on other sites

As Heppell said it depends on how you set up the logic for your enemy spawning. Basically you have to have some logic so that you can stop the spawn, if you are using a timer, stop the timer, if you are calling a function that spawns enemies every frame in your update method, stop calling that when its time for game over, etc.

An easier more general answer would be to set up a GameOver State and switch states to that to display your game over, and a button for restarting, etc.

Link to comment
Share on other sites

I've been thinking how to do that, but I have no clue... ^^;

 

This is the code that spawns the enemies that I have inside the update function:

if (game.nextEnemyAt < game.time.now && enemies.countDead() > 0) {					game.nextEnemyAt = game.time.now + game.enemyDelay;	var enemy = enemies.getFirstExists(false);	enemy.reset(game.rnd.integerInRange(20, 280), 0);        enemy.body.velocity.y = game.rnd.integerInRange(190, 280);	enemy.play("movement");				}

The problem is that I don't know how to stop that "if".

Link to comment
Share on other sites

Add another check to the if statement that ensures it is not in the game over state so that if its game over it will not enter that statement.
 

var gameover = false;if (collision) {  gameover = true;}if (game.nextEnemyAt < game.time.now &&    enemies.countDead() > 0 &&    !gameover) {				  game.nextEnemyAt = game.time.now + game.enemyDelay;  var enemy = enemies.getFirstExists(false);  enemy.reset(game.rnd.integerInRange(20, 280), 0);  enemy.body.velocity.y = game.rnd.integerInRange(190, 280);  enemy.play("movement");				}
Link to comment
Share on other sites

I don't know how exact you were following my code, but make sure var gameover = false is not in the update loop, initialize it only once.

 

You can also do as Heppell said earlier and check if the player still exists after being removed from collision.

if (game.nextEnemyAt < game.time.now &&    enemies.countDead() > 0 &&    player.exists) {				  game.nextEnemyAt = game.time.now + game.enemyDelay;  var enemy = enemies.getFirstExists(false);  enemy.reset(game.rnd.integerInRange(20, 280), 0);  enemy.body.velocity.y = game.rnd.integerInRange(190, 280);  enemy.play("movement");				}
Link to comment
Share on other sites

Oh, one last thing on this topic, do you guys mind if I post the game here with a CC license?

I'm mentioning this just in case there's a policy about that in these forums and because you guys helped me a lot so I feel like I'm on a group. :P

It's my first one, so I don't know exactly what to do... :unsure:

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...