Jump to content

cant get states to work please help


callidus
 Share

Recommended Posts

I have been trying to separate my code into states but no matter what I try the game comes up with a black screen, and an error saying "No state found with the key: playTest " . Here is my code: 

Thanks for any help this has been bothering me for a while

 

*Note: each colour is a different file (index,load and playTest). 

 

Index:

<!DOCTYPE html>
<html> 
<head>
<meta charset="utf-8" />
<script src="phaser.js"></script>
<script src="load.js"></script>
<script src="playTest.js"></script>
</head> 
<body> 
<script> 
function create() {
    var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'game');
    game.state.add("load",load);
game.state.add("playTest",playTest);
game.state.start("playTest");
};
</script>
</body

</html>  

 

 

load:

 

var load = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create});
 
function preload() {
cursors = this.game.input.keyboard.createCursorKeys(); //allows keyboard input
this.game.load.image("sky","assets/sky.png");
}
 
function create() {
this.game.state.start("playTest")
}
 
play: 
 
var playTest = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update,});
 
function create() {
this.game.add.image(0,0,"sky");
this.game.add.text(16,16,"test");
this.game.scale.pageAlignHorizontally = true; //centers canvas
this.game.scale.pageAlignVertically = true;
this.game.scale.refresh();
 
}
 
function update() {
 
}
 
 
 
Link to comment
Share on other sites

http://jsfiddle.net/e80atnum/

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example');var LoadState = function() {};var PlayState = function() {};LoadState.prototype = {  preload: function() {    this.load.image('sky', 'assets/sky.png');  },   create: function() {    this.game.state.start('Play')  }};PlayState.prototype = {  create: function() {    this.cursors = game.input.keyboard.createCursorKeys();    this.add.image(0, 0, 'sky');    this.add.text(16, 16, 'test');    this.scale.pageAlignHorizontally = true; //centers canvas    this.scale.pageAlignVertically = true;    this.scale.refresh();  },   update: function() {  }};game.state.add('Load', LoadState, true );game.state.add('Play', PlayState);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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