Cannot read property '0' of 这个怎么搞 各位大神
html5吧
全部回复
仅看楼主
level 9
13506072314 楼主
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body onkeydown="getCommand()">
<canvas id="tankmap" width="500px" height="500" style="background-color:black;"></canvas>
<script type="text/javascript" src="tankgame.js"></script>
<script type="text/javascript">
var mycanvas=document.getElementById("tankmap")
var cxt=mycanvas.getContext("2d")
//我的坦克
var hero=new Hero(40,40,0,heroColor);
//定义敌人的坦克
var enemyTanks=new Array();
for(var i=0;i<4;i++)
{
//创建坦克
var enemyTank=new EnemyTank((i+1)*50,0,2);
enemyTanks[i]=enemyTank;
//画出坦克
// drawTank(enemyTanks[i]);
}
//颜色数组
var heroColor=new Array("#ded284","yellow");
//坦克类
function Tank(x,y,direct,color){
this.x=x;
this.y=y;
this.speed=1;
this.direct=direct;
this.color=color;
//上
this. moveUp= function(){
this.y-=this.speed;
this.direct=0;
}
//又
this.moveRight=function(){
this.x+=this.speed;
this.direct=1;
}
//下
this.moveDown=function(){
this.y+=this.speed;
this.direct=2;
}
//左
this.moveLeft=function(){
this.x-=this.speed;
this.direct=3;
}
}
//定义一个Hero类
function Hero(x,y,direct,color){
this.tank=Tank;
this.tank(x,y,direct,color);
}
//定义一个EnemyTank类
function EnemyTank(x,y,direct,color){
this.tank=Tank;
this.tank(x,y,direct,color);
}
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,500,500);
drawTank(hero);
}
</script>
</body>
</html>
2017年08月23日 13点08分 1
level 9
13506072314 楼主
js代码:
function drawTank(tank){
switch(tank.direct){
case 0:
case 2:
cxt.fillStyle=tank.color[0];
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,20);
cxt.arc(tank.x+10,tank.y+15,4,0,2*Math.PI);
cxt.fillStyle=tank.color[1];
cxt.fill();
cxt.strokeStyle=tank.color[1];
//cxt.lineWidth=2;
cxt.beginPath();
cxt.moveTo(tank.x+10,tank.y+15);
if(tank.direct==0){
cxt.lineTo(tank.x+10,tank.y);
}
else if(tank.direct==2){
cxt.lineTo(tank.x+10,tank.y
+3
0);
}
cxt.closePath();
cxt.stroke();
break;
case 1:
case 3:
cxt.fillStyle="#ded284";
cxt.fillRect(tank.x,tank.y,30,5);
cxt.fillRect(tank.x,tank.y+15,30,5);
cxt.fillRect(tank.x+5,tank.y+6,20,8);
cxt.arc(tank.x+15,tank.y+10,4,0,2*Math.PI);
cxt.fillStyle="yellow";
cxt.fill();
cxt.strokeStyle="yellow";
//cxt.lineWidth=2;
cxt.beginPath();
cxt.moveTo(tank.x+15,tank.y+10);
if(tank.direct==1){
cxt.lineTo(tank.x+30,tank.y+10);
}
else if(tank.direct==3){
cxt.lineTo(tank.x,tank.y+10);
}
cxt.closePath();
cxt.stroke();
break;
}
}
2017年08月23日 13点08分 2
level 9
13506072314 楼主
Uncaught TypeError: Cannot read property '0' of undefined
at drawTank (tankgame.js:6)
at tankgame.html:73
2017年08月23日 13点08分 3
1