Jump to content

Callback function returns undefined


iamnotanumber10
 Share

Recommended Posts

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

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

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

 Share

  • Recently Browsing   0 members

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