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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.