For Loop

Just started using Construct 2? Post your questions here

Post » Mon Jun 22, 2015 5:56 pm

Is there away to do a for loop within a condition. I know how to do it as a condition.

On left mouse click
for each sprite
do a for loop 10 times.

I'm trying to make it so when you click on a sprite it moves that sprite.
I'm trying to not have so much "code" for the movement.
B
4
Posts: 12
Reputation: 226

Post » Mon Jun 22, 2015 6:07 pm

Oh and I should clarify I want to do it with in the condition because i want to move wait then move back.

So some pseudo code:

Code: Select all
If (mouse == Clicked)
{
     For (i=0; I < 10; i++)
    {
         Sprite.x = Sprite.x + 10;
     }

    Wait(10 Seconds)

    For (i=0; I < 10; i++)
   {
         Sprite.x = Sprite.x - 10;
   }


}
B
4
Posts: 12
Reputation: 226

Post » Mon Jun 22, 2015 6:08 pm

I don't understand the question, sorry..

A for loop is done in 1 tick, so I'm not sure how you would use this for movement..
Isn't one of the many build in movement behaviours suitable for what you want to achieve?
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 » Mon Jun 22, 2015 6:09 pm

If you are using the loop to move 10 positions, there's no point, as all you will see is the final move, so just calculate the final move. If you need to see the movement, you need to store a state and have the movement outside of the Click event. Depends on exactly what you want.

To answer your question directly, you would just add the "For" condition (or as a sub-event), with a range of 1to10.
Edit: semi-ninja'd.
ImageImageImage
B
71
S
21
G
221
Posts: 3,651
Reputation: 124,231

Post » Mon Jun 22, 2015 6:54 pm

Thanks for the replies.

I can do what i want to do with out using a for loop i was just trying to figure out how to have less "actions"

Right now my actions are:
Move 5 pixels at Angel 90.
Wait 0.3 seconds.
Move 5 pixels at Angel 90.
Wait 0.3 seconds.
Move 5 pixels at Angel 90.
Wait 0.3 seconds.
Move 5 pixels at Angel 90.
Wait 0.3 seconds.
Move 5 pixels at Angel 90.

Wait 10 Seconds.

Move -5 pixels at Angel 90.
Wait 0.3 seconds.
Move -5 pixels at Angel 90.
Wait 0.3 seconds.
Move -5 pixels at Angel 90.
Wait 0.3 seconds.
Move -5 pixels at Angel 90.
Wait 0.3 seconds.
Move -5 pixels at Angel 90.


Trying to find out a more condensed way of doing this.
B
4
Posts: 12
Reputation: 226

Post » Mon Jun 22, 2015 7:30 pm

Use a timer and a state variable. The problem with the above is once you start it there is no way to stop it, if you need to.
ImageImageImage
B
71
S
21
G
221
Posts: 3,651
Reputation: 124,231


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest