Neels Posted January 11, 2017 Share Posted January 11, 2017 I made a mario game with the phaser tutoriel, everything is good, the gravity works. But when i want to enable the colision in the update function, it shows Uncaught ReferenceError: player is not defined 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); // NOT WORKING } } Link to comment Share on other sites More sharing options...
mattstyles Posted January 11, 2017 Share Posted January 11, 2017 `player` is a local variable inside the create function, which creates a closure/scope, the reference to which pops out of existence when the function ends. You can shove it out globally or tie it to `this` (or this.game) where it'll be available to the update function. Link to comment Share on other sites More sharing options...
Recommended Posts