Loop question

This forum is currently in read-only mode.
  • 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?

  • Just solved that problem Myself. :]


    (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)


    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


    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.

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


    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.

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • hehe, no cloak, no long brey beard (goatie^^) :D

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