JavaScript Aircraft Wars-----003 Create a Hero Machine

Source: Internet
Author: User

/*Hero Machine: Because the hero machine only one, so do not need to use the constructor*/varHero = {    //Initial pictureSelfNULL,    //Initial Leftleft:0,    //Initial Toptop:0,    //Health ValueLife:3,    //Images loaded in and explodedimgs:[' image/hero.gif ', ' image/hero-bang.gif '],    //InitializeInit: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); //assign a value to the original image of the hero machine         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(){            //because the property above has this.left, we should assign it as a picture._this.left = (engine.game.offsetwidth-img.offsetwidth)/2;//the left center point of the hero machine equals (the width of the game-the width of the hero Machine) divided by 2 _this.top = Engine. game.offsetheight-Img.offsetheight; Img.style.left= _this.left+ ' px '; Img.style.top= _this.top+ ' px ';        }; //call Move when initializing         This. Move (); },    //The hero machine also moves when the mouse moves.Movefunction(){        //similar to Magnifier        var_this = This; Document.onmousemove=function(e) {varE = e| |event; varL = e.clientx-engine.game.offsetleft-_this.self.offsetwidth/2;vart = e.clienty-engine.game.offsettop-_THIS.SELF.OFFSETHEIGHT/2;//Boundary Processing            varLmax = Engine.game.offsetwidth-_this.self.offsetwidth;//Maximum Boundary            varBmax = Engine.game.offsetheight-_this.self.offsetheight;//Maximum BoundaryL = L < 0? 0: (L > Lmax?)lmax:l); T= T < 0? 0: (T > Bmax?)bmax:t); //Assign Value_this.self.style.left = l + ' px '; _this.self.style.top= t+ ' px '; }}}hero.init ();

JavaScript Aircraft Wars-----003 Create a Hero Machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.