0

Everything is working, except in the update function, when I put back the var player, it doesn't recognize it:

"Uncaught ReferenceError: player is not defined"

(I'm also not sure for the animation) (Mario is the player, it's a mario level)

Here is my code:

var SandBox = {

    preload:function(){

        //this.game
        console.log("preload Sand Box");

        this.game.load.image('platform', 'assets/platform.png');
        this.game.load.spritesheet('mario', 'assets/mario.png', 32, 32 , 12);
        this.game.load.image('coinbox', 'assets/coinbox.png');
    },

    create:function(){
        console.log("create Sand Box");

        var imageHeight = this.game.cache.getImage("platform").height;
        this.game.physics.startSystem(Phaser.Physics.ARCADE);

        var worldHeight = this.game.world.height;


        var ground = this.game.add.sprite(0, worldHeight - imageHeight, 'platform');
        var platform = this.game.add.sprite(250, 285, 'platform');
        var platform = this.game.add.sprite(-250, 145, 'platform');
        var platform = this.game.add.sprite(280, 285, 'coinbox');
        var player = this.game.add.sprite(32, 100, 'mario');

        this.game.physics.enable(ground);
        this.game.physics.enable(platform);
        this.game.physics.arcade.enable(player);

        player.frame = 5;
        ground.body.immovable = true;
        platform.body.immovable = true;        
        player.body.bounce.y = 0.2;
        player.body.gravity.y = 300;
        player.body.collideWorldBounds = true;


        player.animations.add('left', [0, 1, 2, 3], 10, true);
        player.animations.add('right', [5, 6, 7, 8], 10, true);

    },

    update:function(){
        console.log("update Sand Box");
        this.game.physics.arcade.enable(player);

        var hitPlatform = this.game.physics.arcade.collide(player, ground); // << THESE FUNCTIONS
    }


}
2
  • player is out-of-scope. Declare it outside the function. Commented Jan 11, 2017 at 19:34
  • I am sorry, i dont know how to declare it outside the function. Can you tell me the line i need to put it please ? Commented Jan 11, 2017 at 20:45

1 Answer 1

2

The scope of variable player in thr snippet is local to the function it is in. One can move the declaration (with var) for player outside of the sandbox object:

var player;
var sandbox = {
    create: function() {
         //... skipping lines ...
         player = this.game.add.sprite(32, 100, 'mario');
    }, 
    Update: function() {
          //use player here
          var hitPlatform = this.game.physics.arcade.collide(player, ground); // << THESE FUNCTIONS

    }
};

Or make it part of the object:

 var sandbox = {
        create: function() {
             //... skipping lines ...
             this.player = this.game.add.sprite(32, 100, 'mario');
      }, 
      Update: function() {
            //... skipping lines ...
            //use this.player here
            var hitPlatform = this.game.physics.arcade.collide(this.player, ground);
       }
 };
Sign up to request clarification or add additional context in comments.

2 Comments

Hey ! It is working, but there is another error : Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined
okay - I don't see any calls to that method in your code so I suspect it is in the Phaser code... that may be something you ask in a separate question, unless you can create another example (e.g. in plunker, codepen or even a SO snippet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.