ry106 Posted November 16, 2016 Share Posted November 16, 2016 I need some help with my Uni project that I made using Phaser. My game is a simple plat former. I have been working on the Player controls, but for some reason I keep getting :"Uncaught TypeError: Cannot read property 'isDown' of undefined" Game.level1 = function (game) {}; //variables var map; var layer; var player; var controls = {}; var playerSpeed = 150; var jumpTimer = 0; Game.level1.prototype = { create:function(){ this.stage.backgroundColor = '#e6ffff'; this.physics.arcade.gravity.y = 1400; map = this.add.tilemap('map', 32, 32); map.addTilesetImage('tileset'); layer = map.createLayer(0); layer.resizeWorld(); map.setCollisionBetween(0,2); player = this.add.sprite(100,561,'player'); player.anchor.setTo(0.5,0.5); player.animations.add('idle', [0,1], 1, true); player.animations.add('jump', [0,2], 1, true); player.animations.add('run', [3,4,5,6,7,8], 7, true); this.physics.arcade.enable(player); this.camera.follow(player); player.body.collideWorldBounds = true; var controls = { right : this.input.keyboard.addKey(Phaser.Keyboard.RIGHT), left : this.input.keyboard.addKey(Phaser.Keyboard.LEFT), up : this.input.keyboard.addKey(Phaser.Keyboard.UP), }; }, update : function () { this.physics.arcade.collide(player,layer); if(controls.up.isDown){ player.animations.play('jump'); } if(controls.right.isDown){ player.animations.play('run'); player.scale.setTo(1,1); player.body.velocity.x += playerSpeed; } if(controls.left.isDown){ player.animations.play('run'); player.scale.setTo(-1,1); player.body.velocity.x -= playerSpeed; } if(controls.up.isDown && (player.body.onFloor() || player.body.touching.down) && this.time.now > jumpTimer){ player.body.velocity.y = -600; jumpTimer = this.time.now +750; } } } Could anyone tell me what I am doing wrong ? Link to comment Share on other sites More sharing options...
Str1ngS Posted November 16, 2016 Share Posted November 16, 2016 Remove the var before controls in your create function controls = { right : this.input.keyboard.addKey(Phaser.Keyboard.RIGHT), left : this.input.keyboard.addKey(Phaser.Keyboard.LEFT), up : this.input.keyboard.addKey(Phaser.Keyboard.UP), }; Link to comment Share on other sites More sharing options...
icbat Posted November 16, 2016 Share Posted November 16, 2016 What Str1ngS says is correct. By saying "var" inside the "create" function, you're creating a local variable instead of adding your object of input keys to the global object you defined at the top of the file. Removing the "var" inside "create" will instead reference the one you created up top. Link to comment Share on other sites More sharing options...
Recommended Posts