Jump to content

Using existing object across different states


myth
 Share

Recommended Posts

I have a group object which i use to display a menu. I'm trying to use states like views, to which i could add existing objects that might have been created or altered in different states. So now i introduce a menu group object in the beginning of code like "var menuGroup". Then i create it in a preload phase (add the pictures,text and actions to it). After this i switch to title phase, which displays the menu correctly using "game.add.existing(menuGroup)". However if i move to a different state, "game.add.existing(menuGroup)" won't work there anymore. The menu is not displayed and i can't see any error in the console. Any ideas what am i doing wrong, or is my approach just terribly bad?

Link to comment
Share on other sites

 Here's a code sample of what i'm trying to do. Basically the second "game.add.existing(menuGroup);" does nothing in "Shop"-state.

var game;var menuGroup;window.onload = function() {	          game = new Phaser.Game(640, 960);    game.state.add("Preload", preload);    game.state.add("GameTitle", gameTitle);    game.state.add("Shop", shop);    game.state.start("Preload");}////////////////////////////////////////////////////////////////////////////////var preload = function(game){};preload.prototype = {	preload: function(){        game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;        game.scale.setScreenSize = true;        game.scale.pageAlignHorizontally = true;        game.scale.pageAlignVertically = true;        game.stage.backgroundColor = "#020028";        game.load.image("gametitle", "assets/sprites/gametitle.png");        game.load.image("gridedition", "assets/sprites/gridedition.png");        game.load.image("playbutton", "assets/sprites/playbutton.png");        game.load.image("menubutton", "assets/sprites/menubutton.png");        game.load.image("resetgame", "assets/sprites/resetgame.png");        game.load.image("thankyou", "assets/sprites/thankyou.png");        },  	create: function(){        menuGroup = game.add.group(null);        var menuButton = game.add.button(game.width / 2, game.height - 30, "menubutton", toggleMenu);        menuButton.anchor.set(0.5);        menuGroup.add(menuButton);        var marketButton = game.add.button(game.width / 2, game.height + 50, "resetgame", function(){        game.state.start("Shop");        });        marketButton.anchor.set(0.5);        menuGroup.add(marketButton);        var thankYou = game.add.button(game.width / 2, game.height + 130, "thankyou", function(){});        thankYou.anchor.set(0.5);        menuGroup.add(thankYou);        game.state.start("GameTitle");	}}////////////////////////////////////////////////////////////////////////////////var gameTitle = function(game){}gameTitle.prototype = {     create: function(){        title = game.add.sprite(game.width / 2, 60, "gametitle");        title.anchor.set(0.5);         var grid = game.add.sprite(game.width / 2, 130, "gridedition");        grid.anchor.set(0.5);        var playButton = game.add.button(game.width / 2, game.height / 2 + 100, "playbutton", function(){});        playButton.anchor.set(0.5);        playButton.events.onInputUp.add(function () {        game.state.start("Shop");});        game.add.existing(menuGroup);     }}function toggleMenu(){     if(menuGroup.y == 0){          var menuTween = game.add.tween(menuGroup).to({               y: -180               }, 500, Phaser.Easing.Bounce.Out, true);     }     if(menuGroup.y == -180){          var menuTween = game.add.tween(menuGroup).to({               y: 0              }, 500, Phaser.Easing.Bounce.Out, true);          }}var shop = function(game) {};shop.prototype = {     create: function(){         console.log("shop");         game.add.existing(menuGroup);     }}
Link to comment
Share on other sites

Hi!

 

You can use the same variable name or global variable, but the objects must be recreated on each state.

In my code i create the menuGroup in the "Preload"-state. However it displays correctly in "GameTitle"-state without recreating it there. So it at least looks like that is not required.

Link to comment
Share on other sites

Indeed that seems to help, thanks. What would be the best way to change object visiblity during state changes, as i don't want everything to be visible from state to state? Is using kill() and revive() the best way?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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