How do I jump enemy every radom second

Get help using Construct 2

Post » Sat May 14, 2016 11:31 am

Hello (excuse my english), :oops:

How do I jump enemy every radom second with "for each instance" function.
I tried that but it is jumping all at once or just one jump.

http://joueralamaitresse.com/test/enemy_jump_random.capx

Help me please. :)
B
12
S
3
Posts: 147
Reputation: 2,026

Post » Sat May 14, 2016 1:00 pm

Will the enemys all start/stop moving at once, as they do now (with 1 global variable for all) ?
Or, will they (in your gamelogic) start/stop moving when they have a personal reason to do so ?

It is a differend approach.

A reason could be, when they see the player .....
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat May 14, 2016 2:46 pm

chrislechat wrote:Hello (excuse my english), :oops:

How do I jump enemy every radom second with "for each instance" function.
I tried that but it is jumping all at once or just one jump.

http://joueralamaitresse.com/test/enemy_jump_random.capx

Help me please. :)


If I understood you correctly:

Use timer behaviour (add it for the enemy object), set timer to random duration for each enemy. On timer expiration do jump and then set new timer with new random duration.

I personally avoid putting every X seconds inside a for each object loop, because loops need to finish their actions within one tick/frame. Timer behaviour was made for such things (that need to be executed across multiple frames/ticks) and it works great.

Please see attached capx (I decided to go with 1-3 seconds between jumps, also disabled your events for jumping):

https://www.dropbox.com/s/e5igcja8182g9 ... .capx?dl=1

Note: this will only work if no new enemies are created after layout start. If new enemies are created while it is running, you would also need to add an "on enemy create" event to set the timer for that new enemy.
Last edited by Pulstar on Sat Jan 21, 2017 8:47 am, edited 1 time in total.
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Sat May 14, 2016 8:12 pm

It's exactly that !

my code seems logical but does not works. (Logic internal function..."for each instance" ?)
your solution works very well.

Thank you very much.
B
12
S
3
Posts: 147
Reputation: 2,026

Post » Sun May 15, 2016 9:43 am

chrislechat wrote:my code seems logical but does not works. (Logic internal function..."for each instance" ?)
your solution works very well.
.


No problem. However you have to realize that your events set the every random seconds condition every engine tick (so every frame). Enemy_go=1 was true every tick.
The event would set it once and then overwrite it in the next frame up to 60 times a second, as a new for each loop runs every tick. Also once it finished the for each enemy loop it should no longer track the every random seconds, as the tracking exists only within the for each loop and ceases to exist once the loop finishes. That is why I wrote that I was surprised that it even worked for one enemy at all.

As timer behaviour exist outside of events (just like other object properties and variables) it works in such cases.
B
15
S
6
Posts: 85
Reputation: 1,195


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider] and 11 guests