簡體   English   中英

函數參數中的Math.random,其中函數保存在變量中-答案始終相同

[英]Math.random in a Functions parameters where the function is held within a variable - answer always the same

首先,請原諒我這個令人困惑的標題,我一直在努力尋找如何正確表達我的問題的方式。

我一直在用JS開發基於文本的瀏覽器游戲,並創建了一些“類”(玩家,技能,物品,咒語,敵人)。 我創建了一個治療功能,可以將其用於治療技能,法術或物品。

然后,我將函數存儲在對象內的變量中,如下所示(添加了注釋以幫助您遵循):

// Skill: name, description, effect
// heal(amt, cost) <-- amount of HP to heal, cost in MP or Stamina
var healingWind = new Skill('Healing Wind', 'A gust of wind that refreshes the 
user.', heal(15, 10));

我的修復功能很好用,而且uringingWind.effect()完全可以正常工作。 我現在想做的就是根據玩家的統計信息(在我們的Player類中定義)恢復15點生命值+隨機數量的能力。

我嘗試這樣做:

// player is an instance of our Player class
var healingWind = new Skill('Healing Wind', 'A gust of wind that refreshes 
the user.', heal(15 + Math.floor(Math.random() * (player.agility * 2)), 10));

一切都按我期望的方式運行,盡管每次使用該技能時,數量都完全相同。 然后,我嘗試創建一個返回Math.random()的函數,並在上面的代碼中用它代替Math.random()。結果是相同的。

然后,我嘗試讓我的隨機函數返回一個返回Math.random()的函數,其結果為NaN。

我不確定我在這里缺少什么,並且已經有一段時間了,我一直在嘗試自己解決此問題,並且大量Google搜索都無濟於事。 我不確定我對自己的解釋是否足夠好,但希望您能理解我的意思。 如果有人需要任何進一步的信息,請告訴我,我將盡力進行描述。

編輯:

heal()函數-使用HealingWind.effect()時,玩家的HP會提高amt的值,而玩家的Stamina的成本會降低。

function heal(amt, cost){
  return function(){
    if(this.constructor.name == 'Item'){
      game.innerHTML += '<br>Using ' + this.name + '!';
      player.currentHp += amt;
    } else if(this.constructor.name == 'Spell'){
      if(player.currentMp >= cost){
        game.innerHTML += '<br>Casting ' + this.name + '!';
        player.currentHp += amt;
        player.currentMp -= cost;
      } else {
        game.innerHTML += '<br>You don\'t have enough Mana to cast this!';
      }
    } else if(this.constructor.name == 'Skill'){
      if(player.currentStamina >= cost){
        game.innerHTML += '<br>Using skill - ' + this.name + '!<br>Healed ' + amt + ' HP!';
    player.currentHp += amt;
    player.currentStamina -= cost;
  } else {
    game.innerHTML += '<br>You don\'t have enough Stamina to use this ability!';
      }
    }
  }
}

請注意-該函數尚未實現if(currentHP> = maxHp)的邏輯,但我會對此進行討論。

-已解決-:這是我為heal()函數編寫的新代碼,如果任何人有興趣或嘗試進行類似操作時都覺得有用。

function heal(base_amt, cost, attribute, multiply){
  return function(){
    var amt = base_amt;
    if(attribute === 'AGI'){
      amt = Math.floor(amt +(Math.random() * (player.agility * multiply)));
    } else if(attribute === 'STR'){
      amt = Math.floor(amt + (Math.random() * (player.strength * multiply)));
    } else if(attribute === 'END'){
      amt = Math.floor(amt + (Math.random() * (player.endurance * multiply)));
    } else if(attribute === 'INT'){
      amt = Math.floor(amt + (Math.random() * (player.intellect * multiply)));
    } else if(attribute === 'WIL'){
      amt = Math.floor(amt + (Math.random() * (player.willpower * multiply)));
    }

    if(this.constructor.name == 'Item'){
      game.innerHTML += '<br>Using ' + this.name + '!';
      player.currentHp += amt;
      if(player.currentHp >= player.maxHp){
        player.currentHp = player.maxHp;
      }
    } else if(this.constructor.name == 'Spell'){
      if(player.currentMp >= cost){
        game.innerHTML += '<br>Casting ' + this.name + '!';
        player.currentHp += amt;
        player.currentMp -= cost;
        if(player.currentHp >= player.maxHp){
          player.currentHp = player.maxHp;
        }
      } else {
        game.innerHTML += '<br>You don\'t have enough Mana to cast this!';
      }
    } else if(this.constructor.name == 'Skill'){
      if(player.currentStamina >= cost){
        game.innerHTML += '<br>Using ' + this.name + '!<br>Healed ' + amt + ' HP!';
        player.currentHp += amt;
        player.currentStamina -= cost;
        if(player.currentHp >= player.maxHp){
          player.currentHp = player.maxHp;
        }
      } else {
        game.innerHTML += '<br>You don\'t have enough Stamina to use this ability!';
      }
    }
  }
}

變量:

// All of these work :D note the extra params of the Skill.
var cure = new Spell('Cure', 'Heals a small amount of HP.', heal(40, 15), 'Restoration');
var healingWind = new Skill('Healing Wind', 'A gust of wind that refreshes the user.', heal(15, 11, 'AGI', 2));
var weakPotion = new Item('Weak Healing Potion', 'A weak healing potion.', 5, heal(20));

正如Jaromanda X所說的,heal函數創建了一個閉包,其中amt從不改變。 因此,您必須將閉包內的amt隨機化。

function heal(base_amt, cost){
  return function(){
    var amt = base_amt + Math.floor(Math.random() * (player.agility * 2))

    [...]

  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM