Egret First Experience – Dodge class games

Source: Internet
Author: User
Tags addchild export class

Here is a brief introduction to my game:
Basically 3 images (ready to add a winning interface)
Start screen, a button, click into the game
Game screen, scrolling background, touching the moving eagle, flying from the sky, and the upper right corner of the time bar
End screen, show results, follow button and replay button

Game main file: Gamecontainer.ts (game logic)
4 class Files: Gameutil.ts (feature set), Bgmap.ts (background scrolling), shit.ts (Cheung's creation and recycling), scorepanel.ts (results show)

/**gameutil.ts*//** based on the center of the collision Detection *        /public static HITTESTP (Obj1:egret. Displayobject,obj2:egret. Displayobject): Boolean        {            var rect2x:number;            var Rect2y:number;            rect2x = obj2.x + obj2.width/2;            rect2y = Obj2.y + obj2.height/2;            Return Obj1.hittestpoint (rect2x, rect2y);        } /** creates a bitmap object based on the name keyword. Refer to the contents of the Resources/resource.json configuration file for the Name property. *    /Export function Createbitmapbyname (name:string): Egret. Bitmap {        var result:egret. Bitmap = New Egret. Bitmap ();        var texture:egret. Texture = res.getres (name);        Result.texture = texture;        return result;    }


/**bgmap.ts*//** Scrollable Background */export class Bgmap extends Egret. Displayobjectcontainer {/** Picture reference */Private Bmparr:egret.        Bitmap[];        /** Number of pictures */private rowcount:number;        /**stage Width */private stagew:number;        /**stage High * * Private stageh:number;        /** the height of the texture itself */private textureheight:number;            /** Control Scrolling Speed */private Speed:number = 4;  Public Constructor () {super (); This.addeventlistener (Egret.        Event.added_to_stage,this.onaddtostage,this); }/** Initialize */private Onaddtostage (Event:egret. Event) {This.removeeventlistener (Egret.            Event.added_to_stage,this.onaddtostage,this);            This.stagew = This.stage.stageWidth;            This.stageh = This.stage.stageHeight; var texture:egret.            Texture = Res.getres ("Bgimage"); This.textureheight = texture.textureheight;//retains the height of the original texture for subsequent calculations This.rowcount = Math.ceil (this.stageh/this.textu ReheigHT) +1;//calculate the number of images needed in the current screen This.bmparr = []; Create these pictures and set the y-coordinate to connect them for the for (Var i:number=0;i<this.rowcount;i++) {var bgbmp:egret.                Bitmap = Avoidshit.createbitmapbyname ("Bgimage");                BGBMP.Y = this.textureheight*i-(This.textureheight*this.rowcount-this.stageh);                This.bmpArr.push (bgbmp);            This.addchild (bgbmp); }}/** starts scrolling */public start (): void {This.removeeventlistener (Egret).            Event.enter_frame,this.enterframehandler,this); This.addeventlistener (Egret.        Event.enter_frame,this.enterframehandler,this); }/**-Frame motion */Private Enterframehandler (Event:egret. Event): void {for (var i:number=0;i<this.rowcount;i++) {var bgbmp:egret.                Bitmap = This.bmparr[i];                Bgbmp.y+=this.speed;                  Judging beyond the screen, go back to the team first, so that you can implement loops repeatedly if (Bgbmp.y > This.stageh) {  BGBMP.Y = This.bmparr[0].y-this.textureheight;                    This.bmpArr.pop ();                This.bmpArr.unshift (bgbmp); }}}/** pause scrolling */public pause (): void {This.removeeventlistener (Egret).        Event.enter_frame,this.enterframehandler,this); }    }
  
/**shit.ts*//** stool, using the object pool */export class Shit extends Egret.        Bitmap {private static cachedict:object = {}; /** Production */public static produce (texturename:string): avoidshit.shit {if (avoidshit.shit.cachedict[te            Xturename]==null) Avoidshit.shit.cachedict[texturename] = [];            var dict:avoidshit.shit[] = Avoidshit.shit.cachedict[texturename];            var shit:AvoidShit.Shit;            if (dict.length>0) {shit = Dict.pop ();            } else {shit = new Avoidshit.shit (Res.getres (texturename));            } shit.texturename = TextureName;        return shit; }/** Recycle */public static reclaim (shit:avoidshit.shit,texturename:string): void {if (Avoidshi            T.shit.cachedict[texturename]==null) Avoidshit.shit.cachedict[texturename] = [];            var dict:avoidshit.shit[] = Avoidshit.shit.cachedict[texturename]; if (Dict.indexof (Shit) ==-1) dict.push (shit); }  public texturename:string;  Public constructor (Texture:egret.        Texture) {super (Texture); }    }
/**scorepanel.ts*//** results show */    Export class Scorepanel extends Egret. Sprite    {        private txt:egret. TextField;        Public constructor () {            super ();            var g:egret. Graphics = This.graphics;            G.drawrect (0,0,100,100);            G.endfill ();            This.txt = New Egret. TextField ();            This.txt.width = +;            This.txt.height = +;            this.txt.textAlign = "center";            This.txt.textColor = 0x00dddd;            This.txt.size =;            This.txt.y =;            This.addchild (this.txt);            This.touchchildren = false;            this.touchenabled = false;        } Public        Showscore (value:number): void {            var msg:string = value+ "";            This.txt.text = msg;        }    }

The above 4 sources are based on the official demo, the following is the code of the Game logic: (This code is too long to stick to the main part)
The initial screen of the game is to put the title, buttons are stacked on top, relatively simple, to add a click on the button to monitor the event on the line

This.icon.addEventListener (Egret. Touchevent.touch_tap, This.gamestart, this);

Game interface, there are three main points: time axis, Xiang, collision detection (Gameutil did, direct use on the line)
The timeline has three parts: a background frame, an intermediate axis, and a matte layer.

/** Background box *            /THIS.TIMELINEBG = Avoidshit.createbitmapbyname ("Tlbgimage");            this.timelinebg.x = this.stagew-this.timelinebg.width-10;            This.timelinebg.y = ten;            This.addchild (THIS.TIMELINEBG);/** Middle Axis */            This.timeline = Avoidshit.createbitmapbyname ("Tlimage");            this.timeline.x = this.stagew-this.timeline.width-13;            This.timeline.y = +;            This.addchild (this.timeline);/** Mask layer */            this.timelinemask = New Egret. Rectangle (0, 0, this.timeline.width, this.timeline.height);            This.timeline.mask = this.timelinemask;/* Timer increases the Mask layer Refresh method */            This.gametimer = New Egret. Timer (this.timedelay,this.timecount);            This.gameTimer.addEventListener (Egret. Timerevent.timer, This.timelinedown, this);
The/** mask layer moves */private timelinedown (evt:egret. TimerEvent): void {            this.timelinemask.y + = THIS.TIMELINE.HEIGHT/20;        }

The generation and destruction of shit.ts are called methods in the

/** Create stool *        /private Createshit (Evt:egret. timerevent): void{            var shit:AvoidShit.Shit = AvoidShit.Shit.produce ("Shitimage");            Shit.x = Math.random () * (this.stagew-shit.width);            Shit.y =-shit.height-math.random () *300;            This.addchildat (shit,this.numchildren-1);            This.shits.push (shit);        }
Stool movement            var theShit:AvoidShit.Shit;            var enemyfightercount:number = this.shits.length;            for (i=0;i<enemyfightercount;i++) {                theshit = this.shits[i];                Theshit.y + = This.downtimes * Speedoffset;                if (Theshit.y>this.stageh)                    Delarr.push (theshit);            }            for (i=0;i<delarr.length;i++) {                theshit = delarr[i];                This.removechild (theshit);                AvoidShit.Shit.reclaim (Theshit, "shitimage");                This.shits.splice (This.shits.indexOf (Theshit), 1);            }

Game end there are two conditions, one is the time, and the second is met Xiang
The end of the screen is to show the results and provide two buttons
The main thing is to share features:
Need to use two files Libs/weixinapi.d.ts and launcher/weixinapi.js
and add the call to the HTML page

Private Doshare (n:number,m:number) {            weixinapi.ready (function (API:WEIXINAPI) {                var info:weixinshareinfo = New Weixinshareinfo ();                Info.title = "I sold the bird, where to provoke dust?" ";                if (M = = 0) {                    Info.desc = "I sold the bird, where to provoke the dust?" ";                } else {                    Info.desc = "I sold the bird, where to provoke the dust?" Excrement Open, excrement open ~ ~ I escaped the "+ n +" Lump stool, you can? ";                }                Info.link = "http://games.11wj.com/fly/launcher/release.html";                Info.imgurl = "Http://games.11wj.com/fly/resource/assets/icon.png";                Api.sharetofriend (info);                Api.sharetotimeline (info);            })        }

Egret First Experience – Dodge class games

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.