for each + every random sec

Get help using Construct 2

Post » Tue Jan 07, 2014 8:04 pm

Hi there!

I came across a problem whilst working on my rpg project and I hope you could help me to fix it.
I got enemies which should every now and then (~2sec) spawn a firebolt.
To shorten up my code i created an 'enemy' family.
I thougth it would be the best way to do it like this:

System.forEach(enemy) && System.everyRandomSeconds(X)
{
   System.SpawnObjectAt(enemy)
}


But instead of spawing a firebolt every X seconds individually on every enemy, it resulted in something unintented. All enemies are now spawning firebolts at the exact same time (every X seconds), which is leaving the player no chance to survive the massive amount of firebolts.
However I want them all to attack (=spawn firebolts) seperatly, every one of them in it's own X seconds rythm. How do i need to modify my code to do this?

I hope it became somewhat clear what I was trying to achieve.


Sincerely


     Gougth2014-01-07 20:05:25
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Tue Jan 07, 2014 8:23 pm


You could use an Instance Variable to control if an enemy can fire and then apply a System.Wait for each enemy


System.forEach(enemy)
{
     enemy.ReadyToFire == true
     {
          enemy.ReadyToFire = false -- so it doesn't just do it again next Tick
          System.Wait(random(1,3)) -- Wait random time
          System.SpawnObjectAt(enemy) -- Spawn Firebolt
          enemy.ReadyToFire = true -- Ready to fire again
     }
}
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Tue Jan 07, 2014 8:27 pm

Use the Timer behaviour. It's superior to Wait or Every X...
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Wed Jan 08, 2014 6:15 pm

Thank you for anwsering that quick!
I got it working now, didn't use Timer behaviour, tough. If I use the "Timer" as a family behaviour I would always want to start a new "Timer" when an enemy spawns since the one family "Timer" does only trigger for the instance which were on layout when i first started the overall family "timer"? Or am I missing the point here?
Nevertheless I found a suitable workaround. Used 'every x seconds' methode from here: https://www.scirra.com/tutorials/450/timers.

But again: Thank you both a lot!
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Wed Jan 08, 2014 6:17 pm

Why give the timer behaviour to the family?
Why not to the object itself?
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Wed Jan 08, 2014 6:36 pm

Would that make a difference?
I mean i would still have to start them separately or this there an option to start the timer when the object is created?
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Wed Jan 08, 2014 7:31 pm

I used to start a timer for the specific uid i.e.

-> Start Timer -> Tag "My_Timer_" & Self.UID

-> On Timer -> "MY_Timer_" & Self.UID

don't know if that is any good, just my 2c.

And have the duration ... + random(...)5Type2014-01-08 19:33:07
B
22
S
7
G
4
Posts: 154
Reputation: 3,699


Return to How do I....?

Who is online

Users browsing this forum: blackhornet, newt and 13 guests