predator Posted January 7, 2017 Share Posted January 7, 2017 Chrome is throwing this error when I run the following code: Uncaught ReferenceError: player is not defined These are my files. index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Phaser</title> <script src="js/phaser.js"></script> <script type="text/javascript" src="js/Boot.js"></script> <script type="text/javascript" src="js/Preload.js"></script> <script type="text/javascript" src="js/Game.js"></script> <style type="text/css"> body { padding: 0; margin: 0; } </style> </head> <body> <script src='js/main.js'></script> </body> main.js var game = game || {}; game.game = new Phaser.Game(800, 600, Phaser.AUTO, ''); game.game.state.add('Boot', game.Boot); game.game.state.add('Preload', game.Preload); game.game.state.add('Game', game.Game); game.game.state.start('Boot'); Preload.js var game = game || {}; game.Preload = function(){}; game.Preload.prototype = { preload: function() { this.preloadbg = this.add.sprite(this.game.world.centerX, this.game.world.centerY, 'loadbg'); console.log('reached'); this.preloadbg.anchor.setTo(0.5); this.preloadbg.scale.setTo(8); this.preloadbar = this.add.sprite(this.game.world.centerX, this.game.world.centerY, 'loadbar'); this.preloadbar.anchor.setTo(0.5); this.preloadbar.scale.setTo(10); this.load.setPreloadSprite(this.preloadbar); this.load.image('armor', 'assets/armor.png'); this.load.image('tile1', 'assets/tile1.png'); this.load.image('sky', 'assets/sky.png'); this.load.spritesheet('dude', 'assets/dude.png', 24, 33); this.load.spritesheet('armordude', 'assets/dudewarmor.png', 24, 33); this.load.spritesheet('armorbaddie', 'assets/baddiewarmor.png', 20, 26); }, create: function() { this.state.start('Game'); } }; Boot.js var game = game || {}; game.Boot = function(){}; game.Boot.prototype = { preload: function() { this.load.image('loadbg', 'assets/loadingbackground.png'); this.load.image('loadbar', 'assets/loadingbar.png'); }, create: function() { this.stage.smoothed = false; this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; this.scale.pageAlignHorizontally = true; this.scale.pageAlignVertically = true; this.scale.setScreenSize = true; this.game.physics.startSystem(Phaser.Physics.ARCADE); this.state.start('Preload'); } }; Game.js var game = game || {}; game.Game = function(){}; game.Game.prototype = { preload: function() { console.log('passed'); var direction; var tile; }, create: function() { console.log('reached'); this.add.sprite(0, 0, 'sky'); this.add.sprite(0, 0, 'armor'); var player = this.add.sprite(0, 0, 'dude'); this.game.physics.arcade.enable(player); player.animations.add('left', [2, 3], 5, true); player.animations.add('right', [4,5], 5, true); player.body.gravity.y = 300; player.body.bounce.y = 0.2; player.collideWorldBounds = true; var platforms = this.add.group(); platforms.enableBody = true; for (var i = 0; i < this.world.width / 64; i ++) { tile = platforms.create(i * 64, this.world.height - 64, 'tile1'); tile.scale.setTo(2, 2); } }, update: function() { var playerCollision = this.physics.arcade.collide(player, platforms); var cursors = this.input.keyboard.createCursorKeys(); player.body.velocity.x = 0; // Left if (cursors.left.isDown) { player.body.velocity.x = -150; player.animations.play('left'); direction = 'left'; } // Right else if (cursors.left.isDown) { player.body.velocity.x = 150; player.animations.play('right'); direction = 'right'; } // Idle else { player.animations.stop() if (direction = 'left') { player.frame = 0; } else if (direction = 'right') { player.frame = 1; } } // Jumping if (cursors.up.isDown && player.body.touching.down && playerCollision) { player.body.velocity.y = -350; } //Smashing if (cursors.down.isDown && !player.body.touching.down && !playerCollision) { player.body.velocity.y = 350; } } }; The error is on this line of the code: var playerCollision = this.physics.arcade.collide(player, platforms); It claims the player doesn't exist when I have it defined right on line 15! Could someone please tell me how to fix this error, and how to avoid it next time? Thanks in advance! Link to comment Share on other sites More sharing options...
LTNGames Posted January 7, 2017 Share Posted January 7, 2017 You're using a variable to define your player, the variable is local to the function only and not available outside of it, you must declare the player as a property of the prototype in order to access it in the prototypes functions. var game = game || {}; game.Game = function(){}; game.Game.prototype = { preload: function() { console.log('passed'); var direction; var tile; }, create: function() { console.log('reached'); this.add.sprite(0, 0, 'sky'); this.add.sprite(0, 0, 'armor'); this.player = this.add.sprite(0, 0, 'dude'); this.game.physics.arcade.enable(player); this.player.animations.add('left', [2, 3], 5, true); this.player.animations.add('right', [4,5], 5, true); this.player.body.gravity.y = 300; this.player.body.bounce.y = 0.2; this.player.collideWorldBounds = true; var platforms = this.add.group(); platforms.enableBody = true; for (var i = 0; i < this.world.width / 64; i ++) { tile = platforms.create(i * 64, this.world.height - 64, 'tile1'); tile.scale.setTo(2, 2); } }, update: function() { var playerCollision = this.physics.arcade.collide(this.player, platforms); var cursors = this.input.keyboard.createCursorKeys(); this.player.body.velocity.x = 0; // Left if (cursors.left.isDown) { this.player.body.velocity.x = -150; this.player.animations.play('left'); direction = 'left'; } // Right else if (cursors.left.isDown) { this.player.body.velocity.x = 150; this.player.animations.play('right'); direction = 'right'; } // Idle else { this.player.animations.stop() if (direction = 'left') { this.player.frame = 0; } else if (direction = 'right') { this.player.frame = 1; } } // Jumping if (cursors.up.isDown && this.player.body.touching.down && playerCollision) { this.player.body.velocity.y = -350; } //Smashing if (cursors.down.isDown && !this.player.body.touching.down && !playerCollision) { this.player.body.velocity.y = 350; } } }; predator 1 Link to comment Share on other sites More sharing options...
predator Posted January 7, 2017 Author Share Posted January 7, 2017 Ok I'll try that. Thanks! LTNGames 1 Link to comment Share on other sites More sharing options...
predator Posted January 7, 2017 Author Share Posted January 7, 2017 Thank you so much! It works! I also had to define platforms in a global scope, though. LTNGames 1 Link to comment Share on other sites More sharing options...
LTNGames Posted January 7, 2017 Share Posted January 7, 2017 Good, glad it works for you. Scoping is the first thing you will battle with when programming in javascript, youll catch on fairly quick though. Link to comment Share on other sites More sharing options...
Recommended Posts