How do I use "loopindex" with "wait" in a for loop

Get help using Construct 2

Post » Tue May 24, 2016 12:22 am

I understand to get an event to trigger in a for loop with "wait" you have to multiply the time between each event by the loop index so;

onStart
forLoop 1-4 -> wait loopindex * 1

And it does trigger once every second for four seconds. But the problem is that "loopindex" is coming up as -1 instead of the actual loop index. And that is causing me problems. Does anyone know a way around this?

Here is a link to display what I am running into.

https://dl.dropboxusercontent.com/u/820 ... est.caproj

Many thanks!
B
19
S
8
Posts: 15
Reputation: 1,405

Post » Tue May 24, 2016 9:11 am

That is a bit of a weird way to do this.

When you make a 'for' loop. The system stays in that loop, till its done. Adding a wait will cause you many problems.
Especialy when you add to the wait time.

May i suggest to do this in a differend way. Use the timer behaviour on any object.
Start a continue running timer with tag "loop".
Then 'on timer "loop"' do your stuff. Add 1 to some local variable to have a loopindex.
When that loop index has some value, stop the timer.

If you create objects in that loop, only create them in the on timer event.
Use a 'on creation' to do stuff with them. Because 'on creation' selects the right object after it is created.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue May 24, 2016 2:00 pm

Interesting. I've never used the timer behavior before. Specifically what I'm doing is;

When the player hits spacebar, the hero sprite will fire projectiles. The number of projectiles, how much damage they do, the angle they travel, etc., is all pulled from a database.

I'm trying to get the projectiles to be able to leave at slightly different times (if there is more than one projectile being fired) so they are clustered differently. I suppose I'd put the "timer" behavior on the "keyboard" and on space bar trigger the loop regularly from that?

I'll give it a try. Thank you!
B
19
S
8
Posts: 15
Reputation: 1,405

Post » Tue May 24, 2016 2:15 pm

Then the suggestion i gave you is a good solution. With as less as a performance drop as possible.

I dont think you can attach a behaviour to the keyboard object type.
Instead, attach it to the hero. Because. Well the 'on timer' will also set the hero selected that way.
Easy to fire from predefined imagepoints on the hero, or from whatever position, i suppose the position from where the firing is happening is linked somehow to the position of the hero.

That is another huge plus of the timer behaviour. No probs with picking.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue May 24, 2016 2:17 pm

Great! I appreciate the help.
B
19
S
8
Posts: 15
Reputation: 1,405


Return to How do I....?

Who is online

Users browsing this forum: lukewasthefish, marc254, Mayfly, tarek2 and 19 guests