我再次回到游戏寻求帮助。这次有了战斗系统。
问题是我找不到一次只运行一个功能的方法,而单击按钮后只能运行一个功能。
这是我到目前为止所拥有的:
对于HTML,我有...
<div id="battle" style="display: none;">
<div class="battle_banner">
<p>Battle!</p>
</div>
<div class="battle_body">
<p>
Creature's Name: <span id="cName">No One</span><br /><br />
Player HP: <span id="hHP">borked</span><br />
Creature HP: <span id="cHP">borked</span><br /><br /><br />
<button id="weapon" onclick="weapon()">Weapon</button>
<button id="kick" onclick="kick()">Kick</button>
<button id="block" onclick="block()">Block</button>
</p>
</div>
</div>
</div>
对于我拥有的jquery数据...
注意,我现在仅查看warrior()函数。如果我能使该工作正常进行,我想我可以自己解决其余的问题。
//Battle Stuff:
var hHP = HP;
document.getElementById("hHP").innerHTML = hHP;
var cHP = 100;
document.getElementById("cHP").innerHTML = cHP;
var magicPower = MP + Intel;
var strikePower = (HP + Str) / 2;
var defenseAbility = (HP + Def) / 2;
var sword = (strikePower);
var arrow = (Agil + strikePower) / .75;
var knife = (Agil + strikePower + Sneak) / 2;
var magic = (magicPower);
var kick = (strikePower * 2) / .5;
var block = (cAttack / 2);
var cAttack = 5;
var hTurn = 1;
var cTurn = 0;
while (hTurn == 1) {
if (hHP >= 1 && cHP >= 1) {
if (Warrior == true) {
function weapon() {
cHP = (cHP - sword);
document.getElementById("cHP").innerHTML = cHP;
$("<p>You slashed the enemy for " + sword + "!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
hTurn = 1;
eTurn = 0;
}
weapon();
function kick() {
cHP = (strikePower - cHP);
document.getElementById("cHP").innerHTML = cHP;
$("<p>You kicked the enemy for " + kick + "!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
hTurn = 1;
eTurn = 0;
}
kick();
function block() {
hHP = (defenseAbility - cAttack);
document.getElementById("hHP").innerHTML = hHP;
$("<p>You defended " + block + " from the enemies attack!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
hTurn = 1;
eTurn = 0;
}
block();
if (hHP <= 0) {
$("<p>You lost. You wake back up, having fainted from battle.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
} else if (cHP <= 0) {
$("<p>You Won!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
money = money + 99999;
document.getElementById("money").innerHTML = money;
level = level++
document.getElementById("level").innerHTML = level;
}
hTurn = 0;
eTurn = 1;
} else if (Archer == true) {
function weapon() {
cHP = cHP - arrow;
document.getElementById("Weapon").innerHTML = Weapon;
}
weapon();
function kick() {
cHP = cHP - strikePower;
document.getElementById("cHP").innerHTML = cHP;
}
kick();
function block() {
HP = defenseAbility - cAttack;
document.getElementById("hHP").innerHTML = hHP;
}
block();
if (hHP <= 0) {
$("<p>You lost. You wake back up, having fainted from battle.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
} else if (cHP <= 0) {
$("<p>You Won!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
hTurn = 0;
eTurn = 1;
} else if (Rogue == true) {
function weapon() {
Weapon = knife - cHP;
document.getElementById("Weapon").innerHTML = Weapon;
}
weapon();
function kick() {
cHP = strikePower - cHP;
document.getElementById("cHP").innerHTML = cHP;
}
kick();
function block() {
HP = defenseAbility - cAttack;
document.getElementById("hHP").innerHTML = hHP;
}
block();
if (hHP <= 0) {
$("<p>You lost. You wake back up, having fainted from battle.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
} else if (cHP <= 0) {
$("<p>You Won!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
hTurn = 0;
eTurn = 1;
} else if (Mage == true) {
function weapon() {
Weapon = magic - cHP;
document.getElementById("Weapon").innerHTML = Weapon;
}
weapon();
function kick() {
cHP = strikePower - cHP;
document.getElementById("cHP").innerHTML = cHP;
}
kick();
function block() {
HP = defenseAbility - cAttack;
document.getElementById("hHP").innerHTML = hHP;
}
block();
if (hHP <= 0) {
$("<p>You lost. You wake back up, having fainted from battle.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
} else if (cHP <= 0) {
$("<p>You Won!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
hTurn = 0;
eTurn = 1;
}
}
}
while (cTurn == 1) {
if (hHP >= 1 && cHP >= 1) {
function eAttack() {
hHP = cAttack - hHP;
document.getElementById("hHP").innerHTML = hHP;
$("<p>You were attacked for " + cAttack + "!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
hTurn = 1;
eTurn = 0;
}
eAttack();
if (hHP <= 0) {
$("<p>You lost. You wake back up, having fainted from battle.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
} else if (cHP <= 0) {
$("<p>You Won!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
}
}
因此,理想的结果是,如果您的职业=战士,那么如果您单击 [武器] 按钮,您将用剑及其伤害值进行攻击。如果单击 [kick] (踢)按钮,则将以踢和其伤害值进行攻击。最后, [block] 应该能够减少敌人可以攻击的力量(目前不知道该怎么做)。
最后发生的是该程序检查定义的类,然后它将自动运行列出的所有功能,从而结束连续循环攻击并导致浏览器崩溃的情况。这不是理想的结果。
是否没有办法以这种方式运行函数?我找不到像我在这里询问的内容,Google搜索,YouTube或W3Schools。任何帮助表示赞赏。