Randomizing enemy shoot times

Get help using Construct 2

Post » Tue May 15, 2012 3:56 pm

I'm currently spawning multiple of the same enemy on screen at the same time and using "Every X seconds" to have them shoot at the player.

However, this makes it so the enemies all shoot at the same time.

I've been playing with other ideas to get them to shoot at different times, but I've not found anything that works yet.

Oh, also, I'm trying to add 1 to a variable on every tick, but the number doesn't increase. You can see a text field in the play area with the tick number when playing.

Here's my capx

Thanks for the help!
Posts: 18
Reputation: 586

Post » Tue May 15, 2012 9:14 pm

I couldn't get your example to do anything--just shows the coin. So I created an example to show you where I think the problems you describe lie.


At the start this example creates a random number of enemies of random colors in random locations. Then every so often (ie randomly) it:
a) creates another enemy of random color in a random location
b) randomly selects an enemy and destroys it
c) have all enemies fire a bullet in random directions.
d) randomly selects an enemy and has it shoot a bullet in a random direction.

(EDIT: I added the case (c) where all of the enemies fire at once.)

Based on your description of the problem with all of the enemies firing at the same time, I suspect that you are doing (c): at random intervals you are (without meaning to) selecting ALL of the enemies and having them fire bullets.

The other problem you have is that one of your variables is not incrementing. I think you are experiencing first hand variable scope -- that is, the difference between local and global variables. This is a classic bug. Pretty much everyone runs into it at some point. Local and global variables each have their uses, so it is important to understand them.

In the example I create two variables: LocalVariable and GlobalVariable. LocalVariable is created inside an event or event group each time it runs and is destroyed at the end of the event/event group. GlobalVariable is created outside the group or event and hangs around.

Both variables are added to every tick and the result is displayed. But notice that since LocalVariable is created and used briefly every time Every Tick is run it only ever gets set to 1. In contrast GlobalVariable hangs around and keeps its value so when it is added to, the number it contains gets larger and larger. kittiewan2012-05-15 21:40:52
Posts: 281
Reputation: 12,448

Post » Wed May 16, 2012 2:18 am

It's actually really simple:

System | For each enemy
enemy | is on-screen
> System | Every [random (2, 5)] seconds -> enemy | spawn bullet on layer 1
Posts: 85
Reputation: 2,810

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 4 guests