Cannot kill player / reset scene?

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!

Phaser’s Sprite doesn’t have a freeze() method. You could add that method to Player instead.

If that’s coming from this.hero.sprite.body.y then probably the sprite has already been destroyed and body is empty. You could check for that or else change:

// Restart the Scene & Play again
this.__scene.setActive(false);
this.__scene.restart ( );

@samme : Yes, I do destroy this.__entity but I thought in order to wipe it clean to reset it, I had to.

@samme: so when i tried the above, for some reason my camera shakes & my game justs sits there frozen. nothing is looping. why won’t it fade out & reset the scene & the player back to proper positions?

Oh, remove this then:

this.__scene.setActive(false);

Add something like this to start of scene update():

if ( this.hero.destroyed ) return;

@samme : So why is it when I fall through the bounds of the level, it waits for 250 milliseconds, then fades out then automatically goes back to showing where I was before instead of restarting immediately after killing the player :

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 );

    }

    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 );

    }

    // `Freeze` our `Player` in place

    freeze ( ) {

        // Sets the `Player` to not be able to move

        this.sprite.body.moves = false;

    }

    update ( )

    {

        if ( this.destroyed ) { return; }

        // ... our existing update code would be here

    }

}

KillPlayer ( ) :

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

    this.__entity.freeze ( );

    // Destroy the `Player`'s memory from Game

    this.__entity.destroy ( );

    // Once the `Camera` is done `fading`, 

    cam.once ( "camerafadeoutcomplete", ( ) => {
	    // Restart the Scene & Play again
	    this.__scene.restart ( );
    } );

}