在一个文字冒险游戏中,我正在和我的朋友一起编码,玩家会经历一个洞并遇到一个男人,他会提供他/她的帮助,但会花费所有玩家的物品,(棍棒,岩石)然而,我会想知道在玩家接受交易后如何使棍棒和岩石的价值回到0。这是当前的代码:
<script>
var y=1
var x=3
function stickAmount() {
var stickNo=document.getElementById("stickNumber")
stickNo.innerHTML="Sticks: "+y;
y++
}
function rockAmount() {
var rockNo=document.getElementById("rockNumber")
rockNo.innerHTML="Rocks: "+x;
x++
}
function one() {
var newButton1 = '<button id="btnTwo" onclick="two();stickAmount();" >Pick up
stick</button>'; var newButton2 = '<button id="btnThree" onclick="three()">Leave it there</button>';
document.getElementById("a").innerHTML="<p>You feel something on the ground, and you think it's a stick.</p>"+newButton1+newButton2;
var myButton = document.getElementById('btnOne');
myButton.onclick = four;
}
function two() {
document.getElementById("b").innerHTML="<p>You pick up the stick. It might be useful for something.</p>";
document.getElementById("inventory").style.display = "block";
document.getElementById("btnThree").style.display = 'none';
document.getElementById("btnTwo").style.display = 'none';
}
function three() {
document.getElementById("b").innerHTML="<p>You leave the stick on the ground and continue on.</p>";
document.getElementById("btnTwo").style.display = 'none';
document.getElementById("btnThree").style.display = 'none';
}
function four() {
var newButton1 = '<button id="btnFour" onclick="five();stickAmount();rockAmount();" >Take the items</button>';
var newButton2 = '<button id="btnFive" onclick="six()">Leave them</button>';
document.getElementById("c").innerHTML="<p>You feel a stick stuck to the wall with something like honey. Next to it is a few rocks.</p>"+newButton1+newButton2;
var myButton = document.getElementById("btnOne")
myButton.onclick = seven;
}
function five() {
document.getElementById("d").innerHTML="<p>You grab the stick and the rocks. Some of the sticky substance rubs off on your hand.</p>";
document.getElementById("rockNumber").style.display = "block";
document.getElementById("btnFive").style.display = 'none';
document.getElementById("btnFour").style.display = 'none';
}
function six() {
document.getElementById("d").innerHTML="<p>You leave the stick and rocks, but some of the sticky substance still rubs off on your hand.</p>";
document.getElementById("btnFive").style.display = 'none';
document.getElementById("btnFour").style.display = 'none';
}
function seven() {
var newButton1 = '<button id="btnSix" onclick="eight()" >Climb through</button>';
var newButton2 = '<button id="btnSeven" onclick="nine()">Ignore it</button>';
document.getElementById("e").innerHTML="<p>As you feel around the cave you find a hole, it seems just big enough for you to climb through.</p>"+newButton1+newButton2;
}
function eight() {
document.getElementById("f").innerHTML="<p>The hole leads to a room with small torches to light it up, and you jump when you hear a voice in the corner.</p>";
document.getElementById("conversations").style.display='block';
var newButton1 = '<button id="btnEight" onclick="ten()">Trust him</button>';
document.getElementById("ii").innerHTML="<p><strong>Man in corner:</strong> I think I can help you see the cave, but my work isn't free. It will cost all your items.
</p>"+newButton1;
document.getElementById("btnSix").style.display='none';
document.getElementById("btnSeven").style.display='none';
}
function nine() {
document.getElementById("f").innerHTML="<p>You walk right past the hole and feel a flashlight on the floor, and you bend over to pick it up. You can now see the cave.</p>";
document.getElementById("btnSix").style.display='none';
document.getElementById("btnSeven").style.display='none';
var elem = document.getElementById("btnOne");
if (elem.innerHTML=="Feel around the cave") {
elem.innerHTML = "Look around the cave";
} else {
elem.innerHTML = "Feel around the cave";
}
}
function ten() {
document.getElementById("g").innerHTML="The man created you a torch. Now you can see around the cave."
var elem = document.getElementById("btnOne");
if (elem.innerHTML=="Feel around the cave") {
elem.innerHTML = "Look around the cave";
} else {
elem.innerHTML = "Feel around the cave";
}
document.getElementById("btnEight").style.display='none';
}
</script>
<div style="margin-left:15px; width:200px; margin-top:100px;">
<button id="btnOne" onclick="one()">Feel around the cave</button>
</div>
<div id="entire" style="margin-left:250px; margin-top:-19px; width:200px; height:400px;">
<div id="g"></div>
<div id="f"></div>
<div id="e"></div>
<div id="d"></div>
<div id="c"></div>
<div id="b"></div>
<div id="a"></div>
</div>
<div style="height:400px;">
<div id="inventory" style="margin-left:550px; width:100px; height:100px; margin-top:-400px; border: 1px solid #ffffff; display:none;">
<div id="stickNumber" style="margin-left:10px; margin-top:10px;"></div>
<div id="rockNumber" style="margin-left:10px; display:none;"></div>
</div>
</div>
<div id="conversations" style="height:400px; margin-left:690px; width:200px; margin-top:-400px; display:none;">
<div id="ii"></div>
</div>
答案 0 :(得分:2)
看起来你通过使用变量x和y跟踪棍棒和岩石。除非我完全遗漏了某些东西,否则只需将它们设置为0。
x=0;
y=0;
如果能解决问题,我建议您将变量从x和y修改为更具可读性的变量。说,棍棒和岩石:))
所以你的功能如下:
function ten()
{
x=0;
y=0;
document.getElementById("g").innerHTML="The man created you a torch. Now you can see
around the cave."
var elem = document.getElementById("btnOne");
if (elem.innerHTML=="Feel around the cave") elem.innerHTML = "Look around the cave";
else elem.innerHTML = "Feel around the cave";
document.getElementById("btnEight").style.display='none';
}
答案 1 :(得分:1)
if (trusted){
sticks=0;
rocks=0;
}
答案 2 :(得分:1)
你的这个:?
sticks=0;
rocks=0;
这些变量更有意义。
答案 3 :(得分:-2)
如果你想要花哨,你可以随时这样做: 棒=枝枝; 岩石岩石=岩石; 这不适用于所有编程语言,自从我用JavaScript编程以来已经有一段时间了