测了一下,直接调dosomething(event)是没问题的,而且move(event)不可能调到,应该是hero.move(event)
(突然发现你这个是WASD的keyCode)
下面是代码
<body onkeydown=dosomething(event)>
<script>
function MyTank(x,y,direct){
this.x = x
this.move = function move(event){
console.log(event.keyCode+'被按下')
}
}
var hero =new MyTank(300, 300, 0)
function dosomething(event) {
if(event.keyCode==38|| event.keyCode==40 ||event.keyCode==37|| event.keyCode==39) {
hero.move(event)
}
}
</script>
var boss = [{name:"校长",energy:10000},
{name:"教务主任",energy:8000},
{name:"系主任",energy:6000},
{name:"辅导员",energy:5000},
{name:"任课老师",energy:12000}
]
var hero = [
{name:"小张",energy:7000},
{name:"小王",energy:7000},
{name:"小强",energy:7000}
]
var n = prompt("选择一个英雄(0:小张,1:小王, 2:小强)","")
var h = hero[n]
var b = boss[Math.floor(Math.random() * boss.length)]
var i = 1
while(true){
var hit = Math.floor(Math.random() * 800)
var bit = Math.floor(Math.random() * 1000)
h.energy -= bit
b.energy -= hit
if(b.energy > 0 && h.energy <= 0){
alert("winner is boss")
break
}else if(h.energy > 0 && b.energy <= 0){
alert("winner is hero")
break
}else if(h.energy <= 0 && b.energy <= 0){
alert("DRAW GAME")
break
}
alert("第" + i++ + "次回合, 英雄: " + h.name + "的能量剩余: " + h.energy + ", 怪兽"+ b.name +"的能量剩余: " + b.energy)
}