reverse for loop

For questions about using Classic.

Post » Mon Jul 06, 2009 9:08 pm

Is there any way of doing a for loop in reverse i.e for x =10 to 0.I have tried but can't get it to work.
B
27
S
2
G
3
Posts: 61
Reputation: 2,498

Post » Mon Jul 06, 2009 9:12 pm

Are you trying to use loopindex as an expression? Because you could use 10-loopindex (total loops-current loop).
Moderator
B
88
S
32
G
33
Posts: 3,005
Reputation: 27,432

Post » Mon Jul 06, 2009 10:40 pm

[quote="westray":7gi64wlu]Is there any way of doing a for loop in reverse i.e for x =10 to 0.I have tried but can't get it to work.[/quote:7gi64wlu]

It doesn't work :(

In the past I made a request to add a "step" feature to loops like in BASIC so you could do something like this:

For "X" = 10 to 1 step -1
(counts backward by 1)

or

For "X" = 25 to 50 step 5
(counts forward by 5)

etc.

Also the loops only take intergers so you wouldn't be able to go from 10 to 0.1 anyway.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Tue Jul 07, 2009 6:15 am

some simple math on the loop index would do the trick

desiredindex = desiredstart+(((loopindex-start)/(end-start))*(desiredend-desiredstart))

that means if you loop from 1 to 10 and you really wanted from 1 to 2 in steps of 0.1

di = 1+( ((loopindex-1)/(10-1)) * (2-1) )
di = 1+((loopindex-1)/9)

if you loop from 1 to 10 and really wanted 10 to 1

di = 10+( ((loopindex-1)/(10-1)) * (1-10) )
di = 10+( ((loopindex-1)/9) * (-9) )

at loopindex 1 this is 10+(((1-1)/9)*(-9)) = 10
at loopindex 2 this is 10+(((2-1)/9)*(-9)) = 10+( (1/9) * (-9) ) = 10-1 = 9
at loopindex 10 this is 10+(((10-1)/9)*(-9)) = 10+( -9 ) = 1


PS: Or you could start all your loops from 0 to n-1 where n is the number of steps you want, then use the linear interpolation function that's built into construct to interpolate between the start of the sequence you want to the end. In fact, do that, it's the same only faster and simpler.
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Tue Jul 07, 2009 6:42 am

You could easily use a private variable, and probably a function would do the trick as well.
But for ease of use I would really like to see what deadeye mentioned implemeted.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Tue Jul 07, 2009 6:59 am

Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Tue Jul 07, 2009 11:41 am

I'd just do a for 1 to 10 and then use 10 - LoopIndex. Dusted!
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Tue Jul 07, 2009 6:45 pm

Lol it's a period after the zero so its not 10 to 0.1 it's 10 to 0
B
27
S
2
G
3
Posts: 61
Reputation: 2,498


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests