我正在制作一个带弹出框的基本游戏。
我的问题是,如果你看一下goblinAttack
函数,它会重复,直到精灵死了,但伤害是随机的。
例如,我每次都会击中地精6点伤害,直到它死亡,而不是1到25之间的随机数,每次击中时精灵对我造成4点伤害。这很无聊。我希望每次点击都是随机的,但重复这个功能似乎每次都不会给出随机数。
//VARIABLES
var dmgMultiply = Math.floor(Math.random() * 10 + 1);
var dmg = 10;
var armour = 0;
var hp = 100;
//ENEMY VARIABLES
var dmgGoblin = Math.floor(Math.random() * 10 + 1);
var goblinHP = 100;
//ARRAYS
var choiceArray = ["Type 'sword' equip your sword.",
"Type 'run' if you're scared.",
"Type 'stats' to see your stats."];
var answerArray = ["sword", "run", "stats"];
var outcomeArrayOne = ["You equip your sword.",
"You run away. Game Over."];
var outcomeArrayTwo = ["Sword Equipped."]
//GAME CHOICE
function choice(a, b, c, x, y, z, aa, bb)
{
var answer = prompt(a + "\n" + b + "\n" + c);
if(answer == x)
{
alert(aa);
swordEquipped(outcomeArrayTwo[0]);
}
if(answer == y)
{
alert(bb);
}
if(answer == z)
{
displayStats();
}
}
//EQUIPPED SWORD
function swordEquipped(a)
{
dmg = 25;
dmgMultiply = Math.floor(Math.random() * 25 + 1);
alert(a + "\n" + "DMG : " + dmg);
goblinCombatStart("goblin");
}
//GOBLIN COMBAT START
function goblinCombatStart(g)
{
alert("A wild " + g + " appears!" + "\n" + "The " + g + " has 100 HP!");
goblinAttack("goblin")
}
function goblinAttack(g)
{
alert("The " + g + " swings his axe at you!" + "\n" + "The " + g + " does " + dmgGoblin + " damage to you!");
hp -= dmgGoblin;
var attack = prompt("Type 'attack' to swing your sword at the " + g + "\n" + "HP : " + hp);
if(attack == "attack")
{
alert("You swing your sword at the " + g + "\n" + "You did " + dmgMultiply + " to the " + g);
goblinHP -= dmgMultiply;
alert("The " + g + " is on " + goblinHP + "HP");
if(goblinHP < 0)
{
goblinDead();
}
if(goblinHP > 0)
{
goblinAttack("goblin");
}
}
else
{
alert("You dropped your sword an shouted " + "'" + attack + "'" + " in the goblins face. It wasn't very effective and the goblin choppped your head off");
}
}
function goblinDead()
{
alert("You have slain the puny goblin with only a few scratches!");
}
//GAME START
choice(choiceArray[0], choiceArray[1], choiceArray[2],
answerArray[0], answerArray[1], answerArray[2],
outcomeArrayOne[0], outcomeArrayOne[1]);
//STATISTICS
function displayStats()
{
var statCheck = confirm("HP : " + hp + "\n" +
"ARMOUR : " + armour + "\n" +
"DMG : " + dmg + "\n" +
"Press OK to continue");
if(statCheck == true)
{
choice(choiceArray[0], choiceArray[1], choiceArray[2],
answerArray[0], answerArray[1], answerArray[2],
outcomeArrayOne[0], outcomeArrayOne[1]);
}
}
答案 0 :(得分:2)
不要在脚本开头只计算一次的随机损坏值,而是在需要时计算它们。这可能是你的攻击功能。
也许你在这里误解了一些东西:Math.random
没有返回一些特殊的魔法值,每次读取它都会改变。它返回数字,该数字不会改变。
如果您想要多个随机值,则必须多次调用Math.random
。如果你在地精攻击时想要一个不同的随机值,你必须在攻击时调用Math.random
。
答案 1 :(得分:0)
这里的问题是你在开始时将变量初始化为随机变量。
这意味着,在每次移动中,它将使用在开头生成的相同随机数。
如果您希望每次随机,则必须在每次移动时拨打Math.random()
。
目前,dmgMultiply
和dmgGoblin
是随机计算一次并在每个回合使用相同的值,因此每次都会获得相同数量的伤害和处理。
在开头声明你的变量:
var dmgMultiply;
var dmgGoblin;
然后,将它们设置为攻击函数中的随机数,以便每回合计算一个随机数:
function goblinAttack(g)
{
dmgMultiply = Math.floor(Math.random() * 10 + 1);
dmgGoblin = Math.floor(Math.random() * 10 + 1);
...
}