Jody Thai Posted December 23, 2013 Share Posted December 23, 2013 Hi all, I have a group and want to reuse it in multiple states because the content inside this group will be the same, but currently it won't work in the second state, the group content is existed in this second state but it just never show up. Here is my codes: I have two variables in my main.js as below:(function (DaoMai) { var width = 640, height = width/(16/9); DaoMai.sceneBg; DaoMai.timeLeft = 60; DaoMai.countDownTimer = null; DaoMai.gameTime = 60; DaoMai.totalItem = 10; // all Tet items, including incorrect items DaoMai.selectedTree = null; DaoMai.mobileWidth = 0; DaoMai.mobileHeight = 0; DaoMai.gameRatio = 16/9; DaoMai.items = []; DaoMai.effectWrongItem; DaoMai.countDownTimer = null; DaoMai.timerInterval = null; DaoMai.bloomingTree; DaoMai.finalTree; DaoMai.finalTreeGroup; DaoMai.chosenTree = null; // 0 is cay Dao, 1 is cay Mai DaoMai.correctItem = 0; DaoMai.checkIcon; // the check icon at state choose tree DaoMai.checkIconTween; DaoMai.game = new Phaser.Game(width, height, Phaser.CANVAS, 'game'); DaoMai.game.state.add('Boot', DaoMai.Boot, true); DaoMai.game.state.add('Preloader', DaoMai.Preloader); DaoMai.game.state.add('MainMenu', DaoMai.MainMenu); DaoMai.game.state.add('StateChooseTree', DaoMai.StateChooseTree); DaoMai.game.state.add('StateTreeBlooming', DaoMai.StateTreeBlooming); DaoMai.game.state.add('StateMain', DaoMai.StateMain); // Now start the Boot state. DaoMai.game.state.start('Boot');}(window.DaoMai = window.DaoMai || {}));Then I create a group and a sprite in my state_tree_blooming.js.DaoMai.finalTreeGroup = this.game.add.group();DaoMai.finalTree = DaoMai.finalTreeGroup.create(0, 0, 'cayDaoBlooming');And then I want to re-use this group in another state, in this case state_I just want to re-locate this group in main.js as below:DaoMai.finalTreeGroup.x = 285;DaoMai.finalTreeGroup.y = 5;And nothing shows up. So I don't know whether a group can be used in multiple state or not, any help would be greatly appreciate. Thanks,Jody Link to comment Share on other sites More sharing options...
haden Posted December 24, 2013 Share Posted December 24, 2013 When you start another state all object inside the game's world are removed including the groups you created. You need to create the group in each state you want to use it. Link to comment Share on other sites More sharing options...
feiss Posted December 25, 2013 Share Posted December 25, 2013 What Haden said. Maybe in your case you should not use different states, but just one state where all groups and sprites are preserved, and use some variable 'my_state' for handling an internal states machine. Link to comment Share on other sites More sharing options...
Recommended Posts