Repeat isn't working properly and I have no idea why

  • The situation is that:

    When Space is pressed, system should pick three random instances of the same object (there are 9 copies of it on the layout) and destroy them. What is happening, though, is that the number of instances destroyed is inconsistent: sometimes it works correctly, sometimes it destroys only two, and sometimes just one. The code is literally just that:

    Why is this not working?

    PS: This is the first time I work with both "Pick Instances" and loops, so probably I'm missing some optimization setting/tool that I'm not aware of.

  • The way that logic is set up, it picks the 3 random sprites first before doing anything else, so it can pick the same one more than once.

  • The way that logic is set up, it picks the 3 random sprites first before doing anything else, so it can pick the same one more than once.

    Wow! But What am I doing wrong then? What should I do to fix it?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I would use a function for something like this because of the trigger event. I've left a wait in so you can actually see what it's picking.

    https://www.dropbox.com/s/omt8tfzvdsvga ... .capx?dl=0

  • What's actually happening is the sprites are being marked to be destroyed at the end of the event. They aren't being destroyed right then which is why they can be picked multiple times.

    One solution would be to add a Boolean variable "alive" with an initial value of true and then do this:

    Space pressed

    Repeat 3 times

    Sprite Is alive

    Pick random Sprite

    --- Sprite: set alive to false

    --- Sprite: destroy

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