How to collide once per box in phaser 3

Hello, I am reviving the super mario game and I want to do the animation of the box, the problem is that when I collide with the box for the second time it continues to apply the tweens. How can I collide only once per box?

code here: https://codepen.io/DevGame/pen/OJjmgwR?editors=1010

Do you mean, after jumping and colliding with the box once, you want the question mark box to not play the bouncing tween when you hit it again?

If I understand it correctly, you’ll have to find some way to manage the state of each block. I think the data manager component would work fine here.

create() {
  ...
  // after defining your physics group 'cuestion'
  cuestion.getChildren().forEach( box => {
    // enable the data manager component for each box in the group
    box.setDataEnabled();
    // initialize a data variable to each box to see if it's been hit yet
    box.data.set('hit', false);
  });

  // Inside your 'impact' collider callback function
  impact(player, cuestion) {
    if (
      player.body.touching.up &&
      !player.body.touching.down &&
      !cuestion.data.get('hit')  // Add a check to see if this box has been hit
    ) {
      this.tweens.add({
        targets: cuestion,
        y: 220,
        duration: 100,
        yoyo: true,
        onStart: () => { console.log("IMPACT") },
        onComplete: () => { box.data.set('hit', true) }, // set box as hit when first hit
      });
    }
  }
}

You could also change the texture of the box to a blank “used” box in the tween’s onComplete callback function.

1 Like

Thanks, that’s exactly what I wanted :slight_smile: