【问题标题】:Save health,fun,coin after change state更改状态后保存健康,乐趣,硬币
【发布时间】:2016-10-22 21:18:22
【问题描述】:

//编辑

我想在切换状态和返回状态后保存健康,硬币,乐趣值。

我创建了游戏对象/数组:

game.yourGameData = {};
game.yourGameData.health = 100;
game.yourGameData.coin = 100;
game.yourGameData.fun = 100;

接下来我定义参数宠物的值:

//设置健康

var health, fun, coin;


    health = this.game.yourGameData.health;
    fun = this.game.yourGameData.fun;
    coin = this.game.yourGameData.coin;

    //custom properties of the pet
    this.pet.customParams = {health, fun, coin};

并且值显示没有错误。 现在我通过函数打开子游戏:

showtry: function()
{
    this.game.state.start('brick_destro_game');
},

当我完成子游戏并切换回主状态时,健康值为 100。

状态切换功能:

clickOnActionExit: function()
    {
        this.game.state.start('GameState');
    },

我想看的: 宠物盯着100生命值,接下来他什么都不吃。生命值降至 70。 我将状态切换到子游戏并播放。我完成游戏并回到主要状态。 宠物应该有 70 生命值,但它会重置为 100。

All project code here

game

【问题讨论】:

  • 你在哪里设置yourGameData对象?如果设置在 GameState 状态,每次启动都会重置
  • 检查文件:游戏我只是添加到帖子中
  • 好的,你有this.pet...但是它有定义吗? start 方法内的状态? thisgame 还是状态或其他什么?
  • pet 在创建函数中定义。 this.pet = .......... 我将在其他地方定义它。我会检查;D
  • 好的,我把宠物定义的地方改成“全局变量”。还是一样。

标签: javascript android phaser-framework


【解决方案1】:

就像您提到的那样,您可以将数据保持在带外(全局),或者我喜欢使用的一种我觉得比全局变量更好的方法是将一个新对象附加到“游戏”以保存您的数据。这将在游戏中随处可见。

例如

this.game.yourGameData = {};
this.game.yourGameData.health = 70;
this.game.yourGameData.cons = 100;

【讨论】:

  • 我试试你的方法,还是不行。再次每次从 100 开始。
【解决方案2】:

我认为您的问题是每次执行 create 函数时这些值都会被状态重置,您应该实现以下内容:

var state = {
  create: function () {
    if (!game.pet) {
      var health, fun, coin;

      health = this.game.yourGameData.health;
      fun = this.game.yourGameData.fun;
      coin = this.game.yourGameData.coin;

      //custom properties of the pet
      game.pet={customParams : {health, fun, coin}};
    }
  },
  update: {...}
}

确保在宠物“死亡”时删除属性 game.pet 以便在重新开始时重置值。

【讨论】:

  • 我试试这个,但现在我有错误消息:main.js:84 Uncaught TypeError: Cannot set property 'customParams' of undefinedmain.js:199 Uncaught TypeError: Cannot read property 'health' of undefinedgithub 中的代码更新
  • 修复,问题是宠物不存在
  • 我还有其他错误:D main.js:183 Uncaught TypeError: Cannot read property 'health' of undefined refreshStats main.js:201 Uncaught TypeError: Cannot read property 'health' of undefined 这无法读取健康和其他; /
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-07
  • 2016-07-10
  • 1970-01-01
相关资源
最近更新 更多