Egret learning - Battle City Development (D)

Now added to the tanks firing bullets function

1. Create a class bullet, the bullet does not use pictures, draw a small circular direct egret.Shape

Bullets should have mobile features, is very simple, move in the original direction

 1 class Bullet extends egret.DisplayObjectContainer {
 2 
 3     dir: string;
 4     tank: Tank;
 5 
 6     public constructor() {
 7         super();
 8         this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
 9     }
10 
11     private onAddToStage(event: egret.Event) {
12         var shp:egret.Shape = new egret.Shape();
13         shp.graphics.beginFill( 0xffff00, 1);
14         shp.graphics.drawCircle(0, 0, 5);
15         shp.graphics.endFill();
16         this.addChild(shp);
17     }
18 
19     // 移动
20     public move() {
21         if(this.dir == 'up') {
22             this.y -= 1;
23         }
24 
25         if(this.dir == 'down') {
26             this.y += 1;
27         }
28 
29         if(this.dir == 'left') {
30             this.x -= 1;
31         }
32 
33         if(this.dir == 'right') {
34             this.x += 1;
35         }
36     }
37     
38 }

2. join to create bullets and tanks firing bullets methods

 1 // 发射子弹
 2     public shoot() {
 3         let bullet = this.createBullet();
 4         this.parent.addChild(bullet);
 5         Main.bulletList.push(bullet);
 6     }
 7 
 8     // 创建子弹
 9     public createBullet() {
10         let obj:Bullet = new Bullet();
11         obj.tank = this;
12         obj.dir = this.dir;
13 
14         if(this.dir == 'up') {
15             obj.x = this.x;
16             obj.y = this.y - 20;
17         }
18 
19         if(this.dir == 'down') {
20             obj.x = this.x;
21             obj.y = this.y + 20;
22         }
23 
24         if(this.dir == 'left') {
25             obj.x = this.x - 20;
26             obj.y = this.y;
27         }
28 
29         if(this.dir == 'right') {
30             obj.x = this.x + 20;
31             obj.y = this.y;
32         }
33         
34         return obj;
35     }

3. When you click the tanks, firing bullets

 1     private onAddToStage() {
 2         this.img = new egret.Bitmap();
 3         this.img.texture = RES.getRes(this.name_prefix + '_' + this.dir + '_png');
 4         this.addChild(this.img);
 5 
 6         this.touchEnabled = true;
 7         this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTankTouch, this);
 8     }
 9 
10     // 坦克被点击
11     private onTankTouch(e) {
12         this.shoot();
13     }

 

Guess you like

Origin www.cnblogs.com/woaitech/p/12241819.html