setVelocityY error

Hello everyone,

Been trying to figure this out for days and just can’t seem to understand why this is happening, the error I get is: Uncaught TypeError: this.player.setVelocityY is not a function

From this code:

class mainScene {
preload() {
this.load.image(‘player’, ‘assets/player.png’);
this.load.image(‘pipe’, ‘assets/pipe.png’)
}

create() {
this.player = new Player(125, 300);
this.physics.add.sprite(100, 100, ‘player’);

this.upButton = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);

}

update() {
if (Phaser.Input.Keyboard.JustDown(this.upButton)) {
this.movePlayer();
}
}

movePlayer() {
this.player.setVelocityY(-200);
//this.player.angle = -15;
}
}

class Player {
constructor(speed, power) {
this.speed = speed;
this.power = power;
}
}

I tried moving the movePlayer() to the player class, but this wasn’t working either.

Any help would be appreciated guys, for real!

You haven’t saved the sprite. You need something like

this.playerSprite = this.physics.add.sprite(100, 100, 'player');
// …
this.playerSprite.setVelocityY(-200);

Thanks for the reply!

This doesn’t give any errors but makes the sprite disappear as soon as I click the up key?