Collision at the first "pointerdown"

Hi all…

I want to make a collision when there is “pointerdown” event… but the collision of the codes here will occur at the second click. I want to the collision occur from the first click of pointerdown, but I dont know how to.

here my codes

this.input.on('pointerdown', function (pointer) {

        console.log('down');

        

        this.tweens.add({

            targets: this.sprite,

            y: { value: 3000, duration: 1500}

        });


        // collision

        this.physics.add.existing(this.sprite)

        this.physics.add.existing(this.tower)

        this.physics.add.existing(this.tower2)

        this.col = this.physics.add.collider(this.tower, this.sprite, this.onCollideHandler, null, this)

        this.col2 = this.physics.add.collider(this.tower2, this.sprite, this.onCollideHandler, null, this)

        

        this.onCollideHandler = () => {

        }

    }, this);

Please help me, thank you :slight_smile:

this.onCollideHandler is undefined when you pass it to add.collider because you initialize it to a function at the very end of your event listener. On the second click, this.onCollideHandler exists because the first click defined it. Move this.onCollideHandler to the start of the pointerdown handler to fix this.

Depending on what functionality you intend, I’d recommend using this.input.once, which will make sure that your listener only runs once. Currently, each click will add a new collider.

1 Like

Wah, it works, thank you :slight_smile: