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!
B
3
Posts: 18
Reputation: 436

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.

RandomExperiments.capx

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
B
29
S
11
G
12
Posts: 281
Reputation: 11,888

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
B
14
S
1
G
2
Posts: 85
Reputation: 2,810


Return to How do I....?

Who is online

Users browsing this forum: blackhornet, mjj1990 and 9 guests