Jump to content

Call a function when sprites are no longer overlapping?


ISOmetric
 Share

Recommended Posts

I have two groups of sprites, lets call them locks and slots, and each slot has a custom parameter, lets call it slot.locked. I want to implement functionality whereby when any one of the locks is overlapping a slot, the variable slot.locked becomes true, which I managed to implement as follows:

function update() {
    game.physics.arcade.overlap(locks, slots, lockOverlap);
}

function lockOverlap(lock, slot) {
    slot.locked = true;
}

But now I want to have a situation in which whenever a lock is NOT overlapping a slot, the parameter slot.locked becomes false again.

Any ideas? Thanks.

Link to comment
Share on other sites

Is it more important that you know when a slot becomes unlocked or just that it gets unlocked again somehow?

In the first case, you would loop through the slots (only) after the overlap check:

game.physics.arcade.overlap(locks, slots, lockOverlap);

slots.forEach(function (slot){
    if (!slot.body.wasTouching.none && slot.body.touching.none) {
        // This slot just became unlocked
    }
});

In the second case, you can just reset all the locks before the overlap check:

slots.setAll('locked', false);

game.physics.arcade.overlap(locks, slots, lockOverlap);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...