Appolos Posted September 11, 2018 Share Posted September 11, 2018 Hello everyone ! I am new in Phaser, and I have some simple game on Phaser 2. And problem in what I have in game the target with some health, and the game end`s when targets health equal to zero, but then i go back to main menu and when I again go to game state the targets health already equal to zero. 1) Targets settings: class Target { constructor(health, viewPath) { this.health = health; this.viewPath = viewPath; } hit() { this.health -= 5; } } // creating Targets let wooden = new Target(10, "assets/targ.png"); let bear = new Target(20, "assets/targ1.png"); let celts = new Target(30, "assets/targ2.png"); let north = new Target(40, "assets/target3.jpg"); export const targets = { wooden:wooden, bear:bear, celts:celts, north:north }; 2) Game Preload: preload() { // Array of targets this.enemyGroup = [ targets.wooden, targets.bear, targets.celts, targets.north ]; this.enemy = null; this.levelGroup; this.levelData; // Set Level Settings this.levelGroup = [level.level1, level.level2, level.level3, level.level4]; this.levelData = this.levelGroup[+currentLevel]; // Get the current enemy this.enemy = Object.assign( this.enemyGroup[enemyCounter < 4 ? enemyCounter : 0] ); // loading assets this.load.image("target", this.enemy.viewPath); this.load.image( "weapon", UserWeapon.knife !== undefined ? UserWeapon.knife : "../../assets/knife3.png" ); this.load.spritesheet("coin", coin, 70, 96); } How to correctly save data between states and update data. Can you help with this problem, please!!) Link to comment Share on other sites More sharing options...
dude78 Posted September 14, 2018 Share Posted September 14, 2018 On 9/11/2018 at 11:48 AM, Appolos said: How to correctly save data between states and update data. See the post: Link to comment Share on other sites More sharing options...
Yehuda Katz Posted September 15, 2018 Share Posted September 15, 2018 @Appolos you should have global object which will be accessable from all states. I assume you have global variable game, so you can create another global object like profile = {health:100, coin:0} and store all data there. I personally prefer to store all global objects within game object. So it looks like game.profile = {health:100, coin:0} and I also have game.translate_manager, game.tween_manager and etc. Those objects are kind of libraries, they store data during the game and can be easily accessed because game object isaccessable everywhere probably I should use plugin property instead but official doc gives little guids regarding game structure. Link to comment Share on other sites More sharing options...
Recommended Posts