iamnotanumber10 Posted December 28, 2016 Share Posted December 28, 2016 Hi, can someone tell me why stateNum in changeState() is undefined? Thanks. var demo = {}; demo.state0 = function(){}; demo.state0.prototype = { preload: function(){}, create: function(){ game.stage.backgroundColor = '#DDD'; console.log('State: 0'); game.input.keyboard .addKey(Phaser.Keyboard.ONE) .onDown.add(changeState, null, null, 1); game.input.keyboard .addKey(Phaser.Keyboard.TWO) .onDown.add(changeState, null, null, 2); }, update: function(){}, }; function changeState(i, stateNum){ console.log(i, stateNum); game.state.start('state' + stateNum); } Link to comment Share on other sites More sharing options...
hamdirizal Posted December 28, 2016 Share Posted December 28, 2016 I tried and it works. Link to comment Share on other sites More sharing options...
iamnotanumber10 Posted December 28, 2016 Author Share Posted December 28, 2016 Oh, that's weird. Here is the other code, but here is the exact error first. b.Key {game: b.Game, event: KeyboardEvent, isDown: true, isUp: false, altKey: false…} undefined Phaser.StateManager - No state found with the key: stateundefined - phaser.min.js:6 main.js var game = new Phaser.Game(600, 400, Phaser.AUTO); game.state.add('state0', demo.state0); game.state.add('state1', demo.state1); game.state.add('state2', demo.state2); game.state.add('state3', demo.state3); game.state.add('state4', demo.state4); game.state.add('state5', demo.state5); game.state.add('state6', demo.state6); game.state.add('state7', demo.state7); game.state.add('state8', demo.state8); game.state.add('state9', demo.state9); game.state.start('state0'); // use this to switch states index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Phaser Demo</title> <script src="phaser/phaser.min.js"></script> <script src="state0.js"></script> <script src="state1.js"></script> <script src="state2.js"></script> <script src="state3.js"></script> <script src="state4.js"></script> <script src="state5.js"></script> <script src="state6.js"></script> <script src="state7.js"></script> <script src="state8.js"></script> <script src="state9.js"></script> </head> <body> <script src="main.js"></script> </body> </html> Link to comment Share on other sites More sharing options...
drhayes Posted December 28, 2016 Share Posted December 28, 2016 The third argument to Phaser.Signal.add is the priority. You're passing "null" for the priority (totally fine), but then only passing one argument to your listener function, not two. Add another null like this: ".onDown.add(changeState, null, null, null, 2);" That second to last null will be sent in as "i". That last number will be the statenumber. Link to comment Share on other sites More sharing options...
iamnotanumber10 Posted December 28, 2016 Author Share Posted December 28, 2016 Ok, so this is embarrassing. It turns out I was using an old version of Phaser v2.0.1. I had just linked it from the previous (first) tutorial I did. The original code works as expected now. Thanks. Link to comment Share on other sites More sharing options...
iamnotanumber10 Posted December 29, 2016 Author Share Posted December 29, 2016 I also wanted to add, the Google Chrome browser caches! This also could have been the problem. If anyone is here looking for a solution to a problem like this, check the browser is not caching your old code. Link to comment Share on other sites More sharing options...
drhayes Posted December 29, 2016 Share Posted December 29, 2016 You can tell Chrome to stop caching JS when the developer tools are open. It's one of the settings available from the "Settings" panel in the developer tools. Very helpful. ( = iamnotanumber10 1 Link to comment Share on other sites More sharing options...
Recommended Posts