StuffBySpencer Posted March 30, 2014 Share Posted March 30, 2014 I'm currently having troubles with states in my game. I just started using it so I'm certain I'm making a dumb mistake, but if you could help I would really appreciate it.The problem I'm having is that when I hit the Space Button, it triggers the start function in stateMENU again and again. Shouldn't the stateMENU end and not respond/react anymore?If you need any more information, let me know. Here's My Code This is in LOAD.js:var stateLOAD = { preload: function(){ this.game.stage.backgroundColor = "#FF0921"; this.game.load.spritesheet('player','redThief.png',50,95); }, create: function(){ this.game.state.start('menu'); }}; This is in MENU.js: var stateMENU = { create: function(){ game.stage.backgroundColor = "#8A8A8A"; var spaceBttn = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); spaceBttn.onDown.add(this.start,this); }, start: function(){ console.log('start'); this.game.state.start('play'); }};This is in PLAY.js:var bgColor = "#678543";var statePLAY = { create: function(){ console.log('CREATE'); game.stage.backgroundColor = bgColor; var moveKeys = game.input.keyboard.createCursorKeys(); }};^ When you hit spacebar during the 'play' phase, it triggers the code in the start function in stateMENU and the console logs 'start' along with 'create'. This is in GAME.js:var game = new Phaser.Game(600,400,Phaser.AUTO,'gameDIV');game.state.add('load',stateLOAD);game.state.add('menu',stateMENU);game.state.add('play',statePLAY);game.state.start('load'); Link to comment Share on other sites More sharing options...
Dream Of Sleeping Posted March 30, 2014 Share Posted March 30, 2014 Since Phaser 2.0 you have to actually remove the key listener when you exit that state. So spaceBttn.onDown.remove(this.start,this); StuffBySpencer 1 Link to comment Share on other sites More sharing options...
StuffBySpencer Posted March 30, 2014 Author Share Posted March 30, 2014 Thank you so much, it's a little annoying you have to do that, but it works so that's all I care about now haha Link to comment Share on other sites More sharing options...
rich Posted April 1, 2014 Share Posted April 1, 2014 https://github.com/photonstorm/phaser/commit/a4ed94e039619f8a29e915cadf33a20708e37a78 Link to comment Share on other sites More sharing options...
Recommended Posts