Jump to content

how to display same sprites in different state


king
 Share

Recommended Posts

Hello everyone,
 
I'm trying to make a simple game with Phaser,but I don't know how to display same sprites in different state. 
 
I did:
game.state.start('levelMenu', true, true) ;
 
It didn't show the sprites in the levelMenu state, what's wrong with my code? 
       var mainMenu = {            preload: function () {                game.load.image('bg', 'assets/bg.png');		game.load.image('mainstart', 'assets/mainstart.png');            },            create: function () {		game.add.sprite(0, 0, 'bg');		mainstart = game.add.sprite(0, 0, 'mainstart');		mainstart.name = "mainstart";		mainstart.inputEnabled=true;		mainstart.events.onInputDown.add(listener,this);            }        }        var levelSelect = {            preload: function () {               // game.load.image('bg', 'assets/bg.png');            },            create: function () {                game.add.sprite(0, 0, 'bg');            }        }        var game = new Phaser.Game(640, 480);        game.state.add('menu', mainMenu, true);        game.state.add('levelMenu', levelSelect,false);	function listener () 	{	    game.state.start('levelMenu', true, true);	}

 

Link to comment
Share on other sites

Here, try this:

var mainMenu = {    preload: function () {        // game.load.image('bg', 'assets/bg.png');        // game.load.image('mainstart', 'assets/mainstart.png');        game.load.image('bg', 'assets/pics/louie-inga.png');        game.load.image('mainstart', 'assets/pics/contra2.png');    },    create: function () {        game.add.sprite(0, 0, 'bg');        var mainstart = game.add.sprite(0, 0, 'mainstart');        mainstart.name = "mainstart";        mainstart.inputEnabled = true;        mainstart.events.onInputDown.add(this.listener, this);    },    listener: function ()     {        game.state.start('levelMenu', true, true);    }    }var levelSelect = {    preload: function () {    },    create: function () {        game.add.sprite(0, 0, 'bg');    }}var game = new Phaser.Game(640, 480);game.state.add('menu', mainMenu, true);game.state.add('levelMenu', levelSelect);

A few things:

 

1) Keep things together. Your listener function shouldn't be global, it should be inside the mainMenu object.

2) mainstart is a local var, so you need to put 'var' in front of it

3) Try and keep your code tidy. It makes for easier reading in the long run imho.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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