Tween not working in collision

hi,

i’m want to make a tween on my enemy when a bullet collide with him, but i have an error with the tweens, ‘this’ is not good interpretated. How could i do to fix it ? thanks for your reply.

   bullets = g.physics.add.group({
    		defaultKey: 'particles',
    		maxSize: 10
    });

    // in update
    this.physics.add.collider(enemys,bullets,on_hit,null,this)

    on_hit=(enemy,bullet)=>{
      //this works
      bullet.visible=false;

      //this not works
      this.tweens.add({
    	    targets: enemy,
    	    duration: 200,
    	    ease: 'Linear',
    	    scaleX : 2,
    	    scaleY : 2,
    	    yoyo: true,
    	    onComplete:()=>{enemy.flag=false}
       })
    }

Hi @espace,
You can get the scene object from enemy or bullet. Try something like this:

enemy.scene.tweens.add({
    	    targets: enemy,
    	    duration: 200,
    	    ease: 'Linear',
    	    scaleX : 2,
    	    scaleY : 2,
    	    yoyo: true,
    	    onComplete:()=>{enemy.flag=false}
       });

In arrow functions “this” works in a different way.
Regards.

Hi jjcapellan, thanks. Is it proper to phaser 3 this trick ? i didn’t know before…

Hi,

I don’t know if it is the best way. This is javascript, there are always a thousand ways to do something, and for someone who comes from java or c ++ is crazy :grinning:.
In this case, if you don’t want to introduce “tricks” in your code, you can replace the arrow function with a “traditional” one. It isn’t so cool but in this case it will work as expected :grinning:.
Regards.

1 Like