Write an enemy class first.
/*Create enemy Aircraft:*/functionEnemy (blood,speed,imgs) {//enemy aircraft left This. left = 0; //top of enemy aircraft This. top = 0; //enemy's blood volume This. Blood =Blood; //Enemy aircraft Speed This. Speed =Speed ; //Enemy Pictures Collection This. IMGs = IMGs;//before the explosion and after the explosion}enemy.prototype={constructor:enemy, init:function(){ //Create a single element varimg = document.createelement (' img ')); //assign a picture path to itImg.src= This. imgs[0]; //inserting into the gameEngine.game.appendChild (IMG); //initial image assigned to enemy aircraft This. Self =img; //Get the height and width of the picture when the picture is finished loading var_this = This;//This point in the function will change, so we'll save it in advance.Img.onload =function() {_this.left= parseint (Math.random () * (320-img.offsetwidth)); _this.top= -Img.offsetheight; Img.style.left= _this.left+ ' px '; Img.style.top= _this.top+ ' px '; }; //generate the enemy number and put it into the bullet of the engine. This. ID =Math.random (); engine.enemy[ This. id]= This; }, //The bullets move, the timers are given to the engines .Movefunction(){ This. Top+=this.Speed ; This. Self.style.top = This. top+ ' px '; //Cross Judgment if( This. top>568+ This. Self.offsetwidth) { This. Destroy (); }}, Destroy:function(){ //destroyed //from page hours This. Self.remove (); //disappear from memory Deleteengine.bullet[ This. Id]; }}
is going to create a small medium sized large enemy aircraft
/*create all types of aircraft*/functionSmallenemy () {vars = parseint (Math.random () *3+3); Enemy.call ( This, 1,s,[' image/enemy1.png ', ' image/enemy1-bang.gif '])}smallenemy.prototype={constructor:smallenemy; __proto__:enemy.prototype;}functionMiddleenemy () {vars = parseint (Math.random () *3+2); Enemy.call ( This, 5,s,[' image/enemy2.png ', ' image/enemy2-bang.gif '])}middleenemy.prototype={constructor:middleenemy; __proto__:enemy.prototype;}functionLargeenemy () {vars = parseint (Math.random () *2+1); Enemy.call ( This, 10,s,[' image/enemy3.png ', ' image/enemy3-bang.gif '])}largeenemy.prototype={constructor:largeenemy; __proto__:enemy.prototype;}
Go to the engine and create the enemy planes uninterrupted.
/*Game engine*/varEngine = { //the first game stateGameStatus:false, //so the enemy .enemy:{},//Bulletsbullet:{},//scorescore:0, //Background ImageGame:document.querySelector ('. Game ')), //InitializeInit:function(){ This. Gamestart (); }, //Game StartGamestart:function(){ var_this = This; //Click on the image to determine the status of the game This. Game.onclick =function(){ if(!_this.gamestatus) {_this.gamestatus=true; //Mobile Mobile_this.bgmove (); _this.handlemove (); _this.createplane (); } } }, //Background MoveBgmove:function(){ varY=0; var_this = This; This. Bgtimer = SetInterval (function() {y+=2; _this.game.style[' Background-position-y ']=y+ ' px '; },50)}, Createplane:function(){ //create enemy aircraft and hero machinesHero.init (); //Create enemy aircraft var timer = setinterval (function () {var num = parseint (math.random () *15) +1; Switch (num) {case 1:case 3:case 5:case 7: Case 9:new Smallenemy (). Init (); Break Case 2:case 4:case 6:new middleenemy (). Init (); Case 8:case 12:new largeenemy (). Init (); }},500) }, //all the enemy planes and bullets are moving .Handlemove:function(){ var_this= This; varTimer = SetInterval (function(){ //Create all Bullets for(varIinch_this.bullet) {_this.bullet[i].move ()}//c Create all enemy maneuver for (var i in _this.enemy) {_this.enemy[i].move ()} },30) }}; Engine.init ();
JavaScript Aircraft Wars-----006 Create enemy aircraft