Creating more of the same objects using loop

  • Hello there

    I'm still a beginner so maybe this question is easy for a lot of people

    I would like to create a bunch instances of the same object using loop. The position of the first instance is a certain point in the lay-out between each new instance there is a different distance of 75 on the Y-axe.

    I've tried to use loop to make the event efficient but I could'nt manage to find it working...

    Thanks in advance

  • Hello there

    I'm still a beginner so maybe this question is easy for a lot of people

    I would like to create a bunch instances of the same object using loop. The position of the first instance is a certain point in the lay-out between each new instance there is a different distance of 75 on the Y-axe.

    I've tried to use loop to make the event efficient but I could'nt manage to find it working...

    Thanks in advance

    This is one way of doing it

    https://www.dropbox.com/s/umz9604dhu0w77z/Multi%20Create%20In%20a%20Loop.capx?dl=0

    I If you need exactly 75 distance between objects counting from the edge of the objects you can just ad half of the with from the two shapes wich is in this case 25 + 25

    so you will have to replace the 75 * (loopindex + 1) For 125 * (loopindex + 1)

  • What have you tried? This should be pretty easy

    Var currY = 0
       For 1...x
          Create Object YOUR_OBJECT
          Set YOUR_OBJECT y to currY
          currY = currY + 75
    [/code:2x3i8av5]
    
    That should do it on a high level.
  • You don't really need currY variable, you can use loopindex*75

  • You don't really need currY variable, you can use loopindex*75

    True enough. I like using a variable for clarity sake, but both ways will work just fine.

  • Thank you everyone for your help and responses!

    I never used the loopindex before, it seems like something I need in this case

    but what I still don't understand, is how I can make a reference to a certain position of the first object in the lay out?

    So the position of the first instance of the object is fixed at X,Y

    The following instance is Y+75, the third is at Y + 75 + 75 and so on..

    thanks anyway!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you everyone for your help and responses!

    I never used the loopindex before, it seems like something I need in this case

    but what I still don't understand, is how I can make a reference to a certain position of the first object in the lay out?

    So the position of the first instance of the object is fixed at X,Y

    The following instance is Y+75, the third is at Y + 75 + 75 and so on..

    thanks anyway!

    If I understand correctly, what you want to do is maybe this:

    function doLoop
       LocalVar firstY = 0
           set firstY  = yourObject.Y
           Loop here
                create another yourObject
                set yourObject.Y = firstY + (75*loopindex)
    [/code:g8kl1c7i]
    
    That will only work well, if there is one instant of YourObject. If there is more than one, you need to make sure you pick the right one. I would recommend doing that anyway, for completeness and error-checking sake.
  • Thank you everyone for your help and responses!

    I never used the loopindex before, it seems like something I need in this case

    but what I still don't understand, is how I can make a reference to a certain position of the first object in the lay out?

    So the position of the first instance of the object is fixed at X,Y

    The following instance is Y+75, the third is at Y + 75 + 75 and so on..

    thanks anyway!

    To Reference, the First object you can use many ways to achieve this its only depends on how your Game Design is, Example if the first object is gonna change locations often then one way to reference him is ad an instance "Variable" or a "Boolean" to make the Picking easier

    Here Two examples

    1- https://www.dropbox.com/s/mnsbzjm5nurukpa/Multi%20Create%20In%20a%20Loop2.capx?dl=0

    2- https://www.dropbox.com/s/rhed9cfzsz7y5ps/Multi%20Create%20In%20a%20Loop3.capx?dl=0

    On this two examples, we wanna Pick the Middle Green Sprite as Reference and Create the object underneath,

    so I add a "Boolean" = "Selected" to the sprite

    Note: The Middle Sprite Green is the on only one that has "Selected" To True so C2 can Pick the right one

    Another thing you have a Global Variable "SpawnObjects" just to make it easier to modify how many objects you need to spawn, as sometimes you need to spawn different amounts of objects so this will help you to modify the amount in the Runtime

  • Yes! It's working!!

    I've done it just with the repeat-condition (without variable) and I managed to use this loopindex code! Since my objects had to be at certain imagepoints of another sprite, I tried this one for Y-position:

    Sprite.imagepointY((1)+(loopindex+1)))

    So with creating following instances, it will pick next imagepoint.

    Wow, I could not have done this without your examples! I've learned now how I can use this loopindex

    Thank you very very much! and everyone else who gave me advice!!

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