So I’m trying to kill my player & restart the scene but for some reason I can’t freeze my sprite then do what I need to do in order to ‘kill’ my player.
this is my Player class :
class Player {
create ( __objData ) {
this.__objData = __objData;
this.__scene = this.__objData.scene;
this.__input = this.__objData.input;
this.__physics = this.__objData.physics;
this.__anims = this.__objData.anims;
this.__add = this.__objData.add;
this.__x = this.__objData.x;
this.__y = this.__objData.y;
this.__keyList =
{
w : Phaser.Input.Keyboard.KeyCodes.W,
a : Phaser.Input.Keyboard.KeyCodes.A,
s : Phaser.Input.Keyboard.KeyCodes.S,
d : Phaser.Input.Keyboard.KeyCodes.D,
}
// Get Keyboard Cursor{s} Input
this.cursors = this.__input.keyboard.createCursorKeys ( );
// Get Keyboard Key{s} Input
this.keys = this.__input.keyboard.addKeys ( this.__keyList );
// The movable character
this.sprite = this.__physics.add.sprite (
275, 575, "hero", 0
);
// Set default `facing` to `right`
this.sprite.direction = 'right';
// Set default `X-Position`
this.sprite.x = this.__x;
// Set default `Y-Position`
this.sprite.y = this.__y;
// Check whether or not the Player is touching the ground
this.__onGround = ( this.sprite.body.onFloor ( ) );
this.__minVelocityX = ( __PLAYER_MIN_MOVESPEED );
this.__maxVelocityX = ( __PLAYER_MAX_MOVESPEED );
this.__minVelocityY = ( __PLAYER_MIN_JUMPSPEED );
this.__maxVelocityY = ( __PLAYER_MAX_JUMPSPEED );
this.__minAccelerationX = ( __PLAYER_MIN_MOVESPEED );
this.__maxAccelerationX = ( __PLAYER_MAX_MOVESPEED );
this.__minAccelerationY = ( __PLAYER_MIN_JUMPSPEED );
this.__maxAccelerationY = ( __PLAYER_MAX_JUMPSPEED );
playerAnims.CreatePlayerAtlasAnimations ({
scene : this.__scene,
anims : this.__anims,
entName : 'hero',
});
// The state machine managing the hero
this.stateMachine = new StateMachine
(
'idle',
{
idle : new IdleState ( ),
move : new MoveState ( ),
jump : new JumpState ( ),
movejump : new MoveJumpState ( ),
fall : new FallState ( ),
},
[ {
scene : this,
entity : this.sprite,
minVelocityX : this.__minVelocityX,
maxVelocityX : this.__maxVelocityX,
minVelocityY : this.__minVelocityY,
maxVelocityY : this.__maxVelocityY,
minAccelerationX : this.__minAccelerationX,
maxAccelerationX : this.__maxAccelerationX,
minAccelerationY : this.__minAccelerationY,
maxAccelerationY : this.__maxAccelerationY,
} ]
);
this.destroyed = false;
this.__scene.events.on ( "update", this.update, this );
this.__scene.events.once ( "shutdown", this.destroy, this );
this.__scene.events.once ( "destroy", this.destroy, this );
}
update ( ) {
if ( this.destroyed ) { return; }
// ... our existing update code would be here
}
destroy ( ) {
this.destroyed = true;
// Event listeners
this.__scene.events.off ( "update", this.update, this );
this.__scene.events.off ( "shutdown", this.destroy, this );
this.__scene.events.off ( "destroy", this.destroy, this );
this.sprite.destroy ( );
}
}
KillPlayer ( )
function :
KillPlayer ( __objData ) {
this.__objData = __objData;
this.__scene = this.__objData.scene;
this.__entity = this.__objData.entity;
// Flag that the player is dead so that we can stop update
// from running in the future
this.__isPlayerDead = true;
// Get the Camera's `ID`
const cam = this.cameras.main;
// `Shake` the Camera to show `death`
cam.shake ( 100, 0.05 );
// `Fade` the Camera out with a 250 millisecond delay
cam.fade ( 250, 0, 0, 0 );
// Freeze the player to leave them on screen while fading but remove the marker immediately
console.log ( this.__entity.sprite );
this.__entity.sprite.freeze ( );
// Once the `Camera` is done `fading`,
cam.once ( "camerafadeoutcomplete", ( ) => {
// Destroy the `Player`'s memory from Game
this.__entity.destroy ( );
// Restart the Scene & Play again
this.__scene.restart ( );
} );
}
Inside my update ( )
function :
update ( ) {
if ( this.hero.sprite.body.y > this.__groundLayer.height ) {
this.KillPlayer ({
scene : this.scene,
entity : this.hero,
});
}
}
but I get the error :
Uncaught TypeError: this.__entity.sprite.freeze is not a function
and if I remove this.__entity.sprite.freeze
, I get :
Uncaught TypeError: Cannot read property 'y' of undefined
Any help is gratefully appreciated!