Loop question

For questions about using Classic.

Post » Wed Sep 07, 2011 1:17 pm

Say I start a loop that spawns 12 objects and sets their x positions to the loop index*8, and sets each one's .value('ID') to the current loop index.

This will create a horizontal row of 12 objects and each will have an ID; 1 at the far left and 12 at the far right.

Well, instead of a purely horizontal row, I want the objects to zig-zag. I thought I could do this by figuring out which objects have an even-number ID, and which objects have an odd-number ID, then reposition their hotspots to create this 'zigzag'.

But um..how do I tell if the ID is an odd number or even number?

I could just have 12 events comparing the IDs and telling each individual object to be higher or lower, but that sucks. I'd like to do this in one or two events if possible! Any ideas?

Tokinsom2011-09-07 13:19:57
Image
B
225
S
27
G
13
Posts: 1,774
Reputation: 18,024

Post » Wed Sep 07, 2011 1:44 pm

Just solved that problem Myself. :]


Condition:
(Floor(Sprite.Value('ID')/2)) * 2 = Sprite.Value('ID')

"Floor(X)" means to round DOWN (X) to whole number.
"Sprite" is the name of an object.
'ID' is the name of the private variable of that object.

Lets say the number you wanna test if it is an Odd or even number is 1.

(Floor(1/2)) * 2 = Sprite.Value('ID')= FALSE (It is an odd number)
(Floor(2/2)) * 2 = Sprite.Value('ID')= TRUE (It is an even number)

How?

1/2 rounded down is 0, because 0.5 rounded DOWN is 0, therefore condition is FALSE because (0 * 2 =/= 1)

Now say the number you wanna test if 2 is an Odd or even number.

(Floor(2/2)) * 2 = Sprite.Value('ID')= True

How?

2/2 rounded DOWN is 1, because 1.0 rounded down is 1.0, therefore condition is TRUE because (1 * 2 = 2)

Hope I was clear enough and understood what you wanted.Nifl2011-09-07 13:47:46
B
6
S
1
G
2
Posts: 18
Reputation: 711

Post » Wed Sep 07, 2011 1:45 pm

I know I might soon be called "grampa mod", but modulo is a perfect function to use here.

modulo returns the remains of the division of two numbers. If you divide a number by 2, and the number is even, it will have a remain of 0 (4/2 = 2, 5/2 = 2 remain 1)

if n % 2 = 0
-> number is even, do what you want to do to even numbered instances.

EDIT:
btw, this will also allow you to do much more versatile things. Set it to 3 instead of 2 to only change every third instance (3 % 3 = 0, 4 % 3 = 1, 5 % 3 = 2, 6 % 3 = 0, etc). Works with any other number as well, of course.tulamide2011-09-07 14:03:53
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Sep 07, 2011 2:02 pm

If I ever make a game with an old wise man I'll be sure to name him Tulamide ;)

Not that you've got a long grey beard and wear a cloak or anything...right? :P

Anyhow.. Thanks! I remember reading about mod() back when I used MMF but I never really looked into it. The more you know..

Thanks for your suggestion too, Nifl. I'd go with that but mod seems a bit simpler :)Tokinsom2011-09-07 14:03:50
Image
B
225
S
27
G
13
Posts: 1,774
Reputation: 18,024

Post » Wed Sep 07, 2011 2:05 pm

hehe, no cloak, no long brey beard (goatie^^) :D
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests