Use For loop to control spawned objects

0 favourites
  • 6 posts
From the Asset Store
6 looping tracks to use in your games and projects. These tracks are in the style of the 1960s detective movie genre.
  • 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

  • I think your problem might be that you are using 'rotate to' instead of 'set angle'.

  • 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

  • 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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)