Issue
I am trying to flick whenever i click the mouse but the variable on input.on
is returning undefined upon clicking the mouse down here is the snippet. Phaser is new to me
w : number;
h : number;
velocity:number;
sprite = null;
totalDelta = null;
constructor() {
super({ key: 'Init' });
}
preload(){
this.w = Number(this.game.config.width);
this.h = Number(this.game.config.height);
this.velocity = Number(this.game.config.physics.arcade.gravity.y);
this.createBG();
}
createBG(){
this.add.image(0,0,'background').setOrigin(0);
}
createSprite(){
var x = this.w * 0.1;
this.sprite = this.physics.add.sprite(x ,this.h/2,'sprite').setOrigin(0);
}
create(){
this.createSprite();
this.input.on("pointerdown",this.flick);
}
flick(){
this.sprite.body.velocity.y = -this.velocity;
}
Solution
The problem is, that you have to pass the correct context, to the on
eventlistener. Here a link to the documentation
// pass the context, as third parameter
this.input.on("pointerdown", this.flick, this);
P.s.: or you can use a arrow function, if you don't want to pass the context. I don't recommend doing this, but just to show an alternative option, and many is good to know since many people use this way of coding. Here a link to the documentation for arrow functions
With other words, this also works:
this.input.on("pointerdown", () => this.flick());
Answered By - winner_joiner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.