01_敌方小飞机创建移动
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>*{margin: 0;padding: 0;}#main{width: 400px;height: 600px;background: url("images/background_1.png");margin: 20px auto;position: relative;overflow: hidden}</style>
</head>
<body><div id="main"></div><script>var mainObj=document.getElementById("main");var smallPlaneArray=[];// 创建敌方小飞机/*属性:图片节点图片x坐标y坐标速度行为:移动初始化 把图片节点添加到main里面*/function SmallPlaneProto(imgSrc,x,y,speed){this.imgNode=document.createElement("img");this.imgSrc=imgSrc;this.x=x;this.y=y;this.speed=speed;this.init=function(){this.imgNode.src=this.imgSrc;this.imgNode.style.position="absolute";this.imgNode.style.left=this.x+"px";this.imgNode.style.top=this.y+"px";mainObj.appendChild(this.imgNode);}this.init();this.move=function(){this.imgNode.style.top=parseInt(this.imgNode.style.top)+this.speed+"px";}}/*创建敌对小飞机*/function createSmallPlane(){var smallPlane=new SmallPlaneProto("images/enemy1_fly_1.png",parseInt(Math.random()*356),-parseInt(Math.random()*100+40),parseInt(Math.random()*10)+1);smallPlaneArray.push(smallPlane);}setInterval(createSmallPlane,1000);// 敌方小飞机移动function smallPlaneMove(){for(var i=0;i<smallPlaneArray.length;i++){smallPlaneArray[i].move();if(parseInt(smallPlaneArray[i].imgNode.style.top)>=600){mainObj.removeChild(smallPlaneArray[i].imgNode);smallPlaneArray.splice(i,1);}}}setInterval(smallPlaneMove,50);// 玩家飞机/*属性:图片节点图片x坐标y坐标速度行为:移动--->上下左右发射子弹初始化 把图片节点添加到main里面*/function playerPlaneProto(imgSrc,x,y,speed){this.imgNode=document.createElement("img");this.imgSrc=imgSrc;this.x=x;this.y=y;this.speed=speed;this.init=function(){this.imgNode.src=this.imgSrc;this.imgNode.style.position="absolute";this.imgNode.style.left=this.x+"px";this.imgNode.style.top=this.y+"px";mainObj.appendChild( this.imgNode);}this.init();this.moveLeft=function(){// 到时候根据判断玩家的按键 来执行此事件 进行移动}this.moveRight=function(){// 到时候根据判断玩家的按键 来执行此事件 进行移动}this.moveUp=function(){// 到时候根据判断玩家的按键 来执行此事件 进行移动}this.moveDown=function(){// 到时候根据判断玩家的按键 来执行此事件 进行移动}this.shoot=function(){// 根据按键 来执行发射子弹的事件}}var player=new playerPlaneProto("images/myplane.gif",50,500,10);</script>
</body>
</html>