king Posted October 31, 2013 Share Posted October 31, 2013 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 More sharing options...
rich Posted October 31, 2013 Share Posted October 31, 2013 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 it3) Try and keep your code tidy. It makes for easier reading in the long run imho. Link to comment Share on other sites More sharing options...
king Posted October 31, 2013 Author Share Posted October 31, 2013 Thanks @rich!This will come in very usefull! Link to comment Share on other sites More sharing options...
Recommended Posts