- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先请原谅我的标题相当困惑,我一直在努力弄清楚如何正确表达我的问题。
我一直在用 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));
我的heal 函数运行良好,healingWind.effect() 完全按照其应有的方式运行。我现在想做的是获得治愈 15 HP + 基于玩家统计数据(在我们的 Player 类中定义)的随机数量的能力。
我尝试过这样做:
// 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。
我不确定我在这里缺少什么,并且已经尝试自己解决这个问题很长一段时间以及大量的谷歌搜索但无济于事。我不确定我是否已经解释得足够好,但我希望您能理解我的意思。如果有人需要任何进一步的信息,请告诉我,我会尽力进行描述。
编辑:
heal()函数 - 当使用healingWind.effect()时,玩家的HP会增加amt,而玩家的耐力会减少cost。
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))
[...]
}
}
关于javascript - 函数参数中的 Math.random ,其中函数保存在变量中 - 答案始终相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910280/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!