Sign in to follow this  
callidus

cant get states to work please help

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() {
 
}
 
 
 

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites

You aren't creating a new state, you're creating a new game.

Here's a simple code used to create a game with multiple states:

var game = new Phaser.Game(320, 480, Phaser.AUTO, 'game_div');var load_state = {   preload: function() {  }  create: function() {  }  update: function() {  }};var play_state = {  preload: function() {  }  create: function() {  }  update: function() {  }};

Hope this helps  :)

hmm, Dumtard beated me :/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.