Jump to content

Callback function returns undefined


Recommended Posts

Hi, can someone tell me why stateNum in changeState() is undefined?


var demo = {};

demo.state0 = function(){};
demo.state0.prototype = {
  preload: function(){},
  create: function(){
    game.stage.backgroundColor = '#DDD';
    console.log('State: 0');
      .onDown.add(changeState, null, null, 1);
      .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

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…} 


Phaser.StateManager - No state found with the key: stateundefined - phaser.min.js:6 


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


<!DOCTYPE html>
<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>

<script src="main.js"></script>


Link to comment
Share on other sites

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


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...