Jump to content

Help with Data in Game


Appolos
 Share

Recommended Posts

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

@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

 Share

  • Recently Browsing   0 members

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