Selecting and deleting sprites in a For loop (should work?)

0 favourites
  • 6 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • I have a bug in my prototype and I'm trying to track it down. Here's my "code" in nutshell:

    First, I fill the empty cells in a grid with instances of "spr_test". This seems to work fine. Then I call a function that does the following:

    For "MakeItems" = 1 to 3
         Pick an instance of spr_test at random --> set spr_test.var to a random number between 1 and 3
               spr_test.var = 1 --> create newSprite1 at location of spr_test --> delete spr_test
               spr_test.var = 2 --> create newSprite2 at location of spr_test --> delete spr_test
               spr_test.var = 3 --> create newSprite3 at location of spr_test --> delete spr_test[/code:295myv0b]
    
    Everything about this works great, except it sometimes puts two newSprites on top of each other. I'm guessing that the MakeItems loop happens all three times and THEN it deletes the instances of spr_test. Is that right?
  • Yes, the delete isn't immediate. Assuming spr_test.var starts at zero, just add the extra condition in the pick to pick spr_test.var = 0 first.

  • I think the problem is the "Destroy" command only has an effect at the end of the frame (it's due to how C2 works, it's not the fault of your code). A destroyed item can still be picked in the same frame it's been destroyed.

    Try creating a Boolean for your Spr_test called "Destroyed". Then, change your code a bit :

    For "MakeItems" = 1 to 3

    Pick an instance of spr_test at random with "Destroyed = True" (just add this condition under the "pick at random" part)

    --> set spr_test.var to a random number between 1 and 3

    if spr_test.var = 1 --> create newSprite1 at location of spr_test --> set spr_test.Destroyed to True and delete spr_test

    if spr_test.var = 2 --> create newSprite2 at location of spr_test --> set spr_test.Destroyed to True and delete spr_test

    if spr_test.var = 3 --> create newSprite3 at location of spr_test --> set spr_test.Destroyed to True and delete spr_test

    Edit : well I was too slow to answer ^^

    You could indeed use the spr_test.var as a sort of boolean if it's starts at 0, yup.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Awesome replies. Thanks, guys!

  • Hmm. I followed blackhornet's advice above, but it's not spawning the correct number of sprites in my grid.

    My code now looks like this:

    For "MakeItems" = 1 to 5
         Pick an instance of spr_test at random (if spr_test.var = 0) --> set spr_test.var to a random number between 1 and 3
               spr_test.var = 1 --> create newSprite1 at location of spr_test --> delete spr_test
               spr_test.var = 2 --> create newSprite2 at location of spr_test --> delete spr_test
               spr_test.var = 3 --> create newSprite3 at location of spr_test --> delete spr_test
    [/code:9v6lliz9]
    
    So if it randomly picks an instance of spr_test where var = 0 then it sets var to a random number between 1 and 3. But if it randomly picks an instance where var does NOT equal 0 does it just fall through and loop back? That would explain why I'm not always getting 5 items in my grid.
    
    UPDATE: I may have fixed the problem by moving "if spr_test.var =0" ABOVE "Pick a random instance".
  • Yeah, the order of conditions in an event matters.

    "If spr_test.var=0 -> Pick a random instance" first picks all instances with var=0 and then selects one random from them.

    "Pick a random instance -> If spr_test.var=0" first picks a random instance from all instances, and then checks if its var=0. If var<>0, the whole event will not be executed.

    By the way, you can optimize this code a bit. If you add Sprite1, Sprite2, Sprite3 to a family, you can spawn SpriteFamily and a random sprite will be created.

    For each spr_test Order By random(1) -> spr_test Spawn SpriteFamily
                                            spr_test Destroy
        If loopindex=4                   -> Stop loop   
    [/code:1xsq5tgj]
    
    That's it!
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)