How to trigger event after all objects in family destroyed?

0 favourites
  • 8 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hello!

    I am making a game with various levels. Ultimately, a level is finished when all of the "cups" are destroyed. Is it possible to create an event where the system moves to the next layout only after all "cups" are destroyed, not just one?

    Currently, I have the system waiting 8.5 seconds and then switching to the next level. Not a great solution since it affects how long my level can be, but works in the meantime.

  • The easiest way is to use Count expression.

    System Compare two values Object.Count=0

    You can also make an event that will pick all instances of this object (if they exist) and add else condition.

    Sprite is on layer 0

    Else -> Go to another layout

  • Thank you!

  • I've just recently been using object.count in a project; I noticed that the count doesn't update within the same event, so if you have:

    on sprite touched: destroy sprite; set text to sprite.count

    ...it still includes the touched sprite within the count, because the sprite still exists until the next top level event (I think?).

    Changing this to:

    on sprite touched: destroy sprite; wait 0 seconds; set text to sprite.count

    ...returns a count value as 0, I'm guessing because in this case it only picks the sprite that's been touched and destroying it means there's no sprites left in the pick. I'm not sure why the same picking logic doesn't return a count value of 1 in the first example.

    One way I managed to get around this was:

    On sprite touched: destroy sprite; wait 0 seconds; call function "count"

    On function "count": set text to sprite.count

    I'm sure there are other ways though.

  • mekonbekon

    When you destroy an object, it doesn't disappear straight away. It's "marked" as destroyed, but still exists.

    It gets completely destroyed at the end of the tick, after all events are finished.

    "Wait 0" waits until next tick, so yeah, it can be used in this situation.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • dop2000

    Sorry, I should have clarified - using the wait 0 seconds returns a count of 0 even if other sprites still exist, hence me using a function after the wait to escape the picking

  • Interesting..

    I think this happens because the original instance of the sprite doesn't exist anymore after the Wait 0, so you can't access any of its properties. (even the "Count" expression)

    It works if you add "Pick all Sprite" event after the Wait.

  • Ah yes, "pick all", I forgot about that. Cheers!

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