0 Favourites

# For Loop

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

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

``````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;
}

}[/code:2pzsb4uo]``````
• 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?

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

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