将新按钮应用于游戏
Applying new buttons to the game
在我和朋友进行的一次游戏文本冒险中,一旦玩家穿过某条路径,他/她就会在途中发现一根棍子和一些石头。
当你找到这些石头和棍子时,我想在声明下面添加两个按钮,说:"Grab the sticks and rocks"
。
然后,旁边的另一个按钮显示"Leave it there"
。然而,我在做这件事时遇到了一些麻烦,我想得到一些帮助。仍然对如何在..下应用这些新按钮感到困惑。。
这是当前的Javascript代码:
<script>
function one()
{
var newButton1 = '<button id="btnTwo" onclick="two()" >Pick up stick</button>';
var newButton2 = '<button id="btnThree" onclick="three()">Leave it there</button>';
document.getElementById("a").innerHTML="You feel something on the ground, and you think it's a stick."+newButton1+newButton2;
var myButton = document.getElementById('btnOne');
myButton.onclick = four;
}
function two()
{
document.getElementById("b").innerHTML="You pick up the stick. It might be useful for
something.";
document.getElementById("btnTwo").style.display = 'none';
document.getElementById("btnThree").style.display = 'none';
}
function three()
{
document.getElementById("c").innerHTML="You leave the stick on the ground and continue on.";
document.getElementById("btnTwo").style.display = 'none';
document.getElementById("btnThree").style.display = 'none';
}
function four()
{
document.getElementById("d").innerHTML="You feel a stick stuck to the wall with something like honey. Next to it is a few rocks.";
}
</script>
HTML代码:
<div style="margin-left:15px; width:200px; margin-top:100px;">
<button id="btnOne" onclick="one()">Feel around the cave</button>
</div>
<div style="margin-left:255px; width:200px; margin-top:-15px;">
</div>
<div id="entire" style="margin-left:490px; margin-top:-22px; width:400px;height:600px;">
<div id="d"></div>
<div id="c"></div>
<div id="b"></div>
<div id="a"></div>
</div>
根据我的理解,使用以下Javascript代码:
function one()
{
var newButton1 = '<button id="btnTwo" onclick="two()" >Pick up stick</button>';
var newButton2 = '<button id="btnThree" onclick="three()">Leave it there</button>';
document.getElementById("a").innerHTML="You feel something on the ground, and you think it's a stick."+newButton1+newButton2;
var myButton = document.getElementById('btnOne');
myButton.onclick = four;
}
function two()
{
document.getElementById("b").innerHTML="You pick up the stick. It might be useful for
something.";
document.getElementById("btnTwo").style.display = 'none';
document.getElementById("btnThree").style.display = 'none';
}
function three()
{
document.getElementById("c").innerHTML="You leave the stick on the ground and continue
on.";
document.getElementById("btnTwo").style.display = 'none';
document.getElementById("btnThree").style.display = 'none';
}
function four()
{
var newButton1 = '<button id="btnFour" onclick="five()" >Grab the sticks and rocks</button>';
var newButton2 = '<button id="btnFive" onclick="three()">Leave it there</button>';
document.getElementById("d").innerHTML="You feel a stick stuck to the wall with something like honey. Next to it is a few rocks.";
}
function five()
{
document.getElementById("e").innerHTML="You did grab the sticks and rocks. It might be useful for something.";
document.getElementById("btnFour").style.display = 'none';
document.getElementById("btnFive").style.display = 'none';
}
function six()
{
document.getElementById("f").innerHTML="You leave the stick on the ground and continue
on.";
document.getElementById("btnFour").style.display = 'none';
document.getElementById("btnFive").style.display = 'none';
}
并使用此entire
div:
<div id="entire" style="margin-left:490px; margin-top:-22px; width:400px; height:600px;">
<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>
请注意,我在HTML中添加了两个div,div f
和div e
,在javascript中添加了函数five()
和函数six()
。
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何将返回的值应用于多个不同位置的多个选择器
- ThreeJS将画布中的文本渲染为纹理,然后应用于平面
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 如何将jquery函数仅应用于大屏幕
- 将jQuery事件应用于所有类元素
- 在将绑定应用于控制器之后,是否会发出Angular$scope事件
- 如何在ExtJs中获得将应用于给定类列表的样式
- 将 CSS 应用于禁用的输入按钮
- 如何从应用于正文的css中排除特定的表单
- jQuery+Android.将功能(Touchstart、touchmove)应用于实时加载的内容
- 将敲除绑定应用于模板,并将结果作为字符串获取
- 如何将Angular2管道应用于动态内容
- 动画功能不应用于每个元素
- 仅将JavaScript应用于部分代码(例如菜单)
- 将iframe中的css类应用于主文档
- jQuery:如何在没有for循环的情况下将事件处理程序应用于$('#text'+'任意整数
- 如果使用jQuery验证器插件找到单选按钮,则将不同的addMethod应用于字段
- 将新按钮应用于游戏
- Facebook Canvas应用:这种方法只适用于游戏