Use For loop to control spawned objects

Get help using Construct 2

Post » Mon Aug 27, 2012 11:41 am

Hi all

In my game, I have 5 targets and a gunner. The gunner is supposed to
1) shoot 5 bullets to all of these targets.
2) wait a second after each shoot.

The target's angles are saved to an array called "targets"

This is how I do it

Local variable count = 0
For "attack" from 1 to 5
   Spawn a bullet
   Increase "count" by 1
   System pick the "count"th bullet
      Rotate to targets.At(count)       
   System pause 1 sec

Here are the problems
1) Only the 1st bullet flies to the target, the other bullets stay still right where they are spawned.
2) There is no pause between the spawning

Can you help me out?
Thank you very much
B
5
Posts: 17
Reputation: 509

Post » Mon Aug 27, 2012 12:48 pm

I think your problem might be that you are using 'rotate to' instead of 'set angle'.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Mon Aug 27, 2012 4:27 pm

@sqiddster thank you for the response.
I changed it to 'set angle'. Now the 1st bullet can fly to a target, but the other won't. Moreover, they are still spawned at the same time
B
5
Posts: 17
Reputation: 509

Post » Mon Aug 27, 2012 4:47 pm

Correct me if I am wrong, but I have do things like this

For "a" from 1 to 6
    Add 1 to "variable"
    Text -> Append "variable"
    Wait 2 secs
    Spawn object

Expectation: Variable is increased, its value is printed, object is spawned

Reality: Text's content is 123456, after 2 seconds, objects are spawned.

I conclude that each statement in For loop is executed times before the next statement. It is strange compared to other language.

Can you explain it to me?
Thank you very much
B
5
Posts: 17
Reputation: 509

Post » Mon Aug 27, 2012 5:05 pm

I think your problem is using the wait action inside a loop. Try to redesign your code to avoid this, then see if it works.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Aug 28, 2012 2:50 am

Hi sqiddster,
After getting rid of all wait action, the result is still the same.

Local variable count = 0
For "attack" from 1 to 5
   Spawn a bullet
   Increase "count" by 1
   System pick the "count"th bullet
      bullet.Set angle to(targets.At(count))

Thank you very much.
B
5
Posts: 17
Reputation: 509


Return to How do I....?

Who is online

Users browsing this forum: fredriksthlm, MPPlantOfficial and 21 guests