满意答案
sa0ztsd1p
2017.01.16
采纳率:52% 等级:7
已帮助:609人
Tankgamefunction Hero (x,y,direct){
this.y=y;
this.x=x;
this.speed=1;
this.direct=direct;
this.moveUp=function(){
this.y-=this.speed;
};
this.moveRight=function(){
this.x+=this.speed;
};
this.moveDown = function(){
this.y+=this.speed;
};
this.moveLeft =function(){
this.x-=this.speed;
};
}
var canvasl=document.getElementById("tankMap");
var cxt=canvasl.getContext("2d");
var hero=new Hero(140,40,0);
function drawTank(tank){
cxt.fillStyle="#ded284";
cxt.fillRect(tank.x,tank.y,5,30);
cxt.fillRect(tank.x+15,tank.y,5,30);
cxt.fillRect(tank.x+6,tank.y+5,8,18);
cxt.fillStyle="#ffd972";
cxt.arc(tank.x+10,tank.y+15,4,360,0,true);
cxt.fill();
cxt.strokeStyle="#ffd972";
cxt.lineWidth=2;
cxt.beginPath();
cxt.moveTo(tank.x+10,tank.y+15);
cxt.lineTo(tank.x+10,tank.y-5);
cxt.closePath();
cxt.stroke();
}
drawTank(hero);
function getCommand(){
var code =event.keyCode;
switch (code){
case 87:
hero.moveUp();
break;
case 68:
hero.moveRight();
break;
case 83:
hero.moveDown();
break;
case 65:
hero.moveLeft();
break;
}
cxt.clearRect(0,0,400,300);
drawTank(hero);
}
你的代码有两个问题:
1、case后面应该用冒号":"而不是分号“;” 如:case 83:
2、方法后面应带小括号
更改后在电脑上测试过,是完全没有问题的。
10分享举报