Egret learning - Battle City Development (c)

Tanks for mobile gamers

The basic flow

1. mouse click target locations

2. tank moves to the specified position, can not move obliquely, only horizontal or vertical movement

3. You can simply find its way, if she encounters an obstacle, automatic steering,

 

Mouse click target position, the position of the record in the tank, moved to the target position in the cycle, and determine whether obstacles,

 Background to add a mouse click event

1 let bg = this.createBitmapByName("bg_jpg");
2 this.addChild(bg);
3 bg.touchEnabled = true;
4 bg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBgTouch, this);

Event handling, increase targetX in the tank, targetY record mouse click position

1      / * in response to a click event background * / 
2      Private onBgTouch (E) {
 . 3          the let X = e.stageX;
 . 4          the let Y = e.stageY;
 . 5  
. 6          the this .player1.targetX = the parseInt (X);
 . 7          the this .player1 = .targetY the parseInt (Y); 
. 11 }

Tanks move methods increase

 1     public move() {
 2         if(this.x == this.targetX && this.y == this.targetY) {
 3             return;
 4         }
 5 
 6         let xory = false;// true y, false x
 7         let nextx = this.x;
 8         let nexty = this.y;
 9 
10         let osx = Math.abs(this.x - this.targetX);
11         let osy = Math.abs(this.y - the this .targetY);
 12 is  
13 is          // moved to a small distance 
14          IF ! (OSX OSX = 0 && <18 is ) {
 15              the console.log ( 'the first mobile widget X' );
 16              IF ( the this .x > the this .targetX) {
 . 17                  nextx = the this .x -. 1 ;
 18 is                  the this .turn ( 'left' )
 . 19                  xory = to false ;
 20 is              } the else  IF ( the this .x < the this .targetX) {
 21 is                 nextx = this.x + 1;
22                 this.turn('right')
23                 xory = false;
24             }
25         } else if(osy !=0 && osy < 18) {
26             console.log('先移动小的y');
27             if(this.y > this.targetY) {
28                 nexty = this.y - 1;
29                 this.turn('up')
30                 xory = true;
31             } else if(this.y < this.targetY) {
32                 nexty = this.y + 1;
33                 this.turn('down')
34                 xory = true;
35             }
36         } else if(this.x > this.targetX) {
37             nextx = this.x - 1;
38             this.turn('left')
39             xory = false;
40         } else if(this.x < this.targetX) {
41             nextx = this.x + 1;
42             this.turn('right')
43             xory = false;
44         } else if(this.y > this.targetY) {
45             nexty = this.y - 1;
46             this.turn('up')
47             xory = true;
48         } else if(this.y < this.targetY) {
49             nexty = this.y + 1;
50             this.turn('down')
51             xory = true;
52         }
53 
54         console.log('x,y --- targetX, targetY', this.x, this.y, this.targetX, this.targetY)
55 
56         // 判断是否到边界
57         if(nextx <|| 18 is nextx> 640 - 18 is ) {
 58              the console.log ( 'reaches the boundary' );
 59              the this .targetX = nextx;
 60          }
 61 is  
62 is          IF (nexty <|| 18 is nexty> 640 - 18 is ) {
 63 is              console.log ( 'reaches the boundary' );
 64              the this .targetY = nexty;
 65          }
 66  
67          IF ( the this .main.hitQian (nextx, nexty)) {
 68              console.log ( 'face wall' );
 69              IF ( xory) {
 70                  the this .targetY =this.y;
71             } else {
72                 this.targetX = this.x;
73             }
74         } else {
75             this.x = nextx;
76             this.y = nexty;
77         }
78     }

// determine if you are experiencing brick or stone

1      / * * are experiencing Wall * / 
2      public hitQian (X, Y) {
 . 3          // check whether the next point in the wall 
. 4          the let = HIT to false ;
 . 5          the let the startx = X - 18 is ;
 . 6          the let EndX 18 is = X + ;
 . 7          the let startY = Y - 18 is ;
 . 8          the let Endy = Y + 18 is ;
 . 9          // the above line 
10          for (the let X = the startx; X <EndX; X ++ ) {
 . 11              the let the tile: = tiled.TMXTile the this .layerZhuan.getTile (X, startY);
 12 is              IF (the tile) {
13                 hit = true;
14                 break;
15             } else {
16                 tile = this.layerShitou.getTile(x, starty);
17                 if(tile) {
18                     hit = true;
19                     break;
20                 } 
21             }
22 
23         }
24         // 下面的线
25         for(let x = startx; x<endx; x++) {
26             let tile:tiled.TMXTile = this.layerZhuan.getTile(x, endy);
27             if(tile) {
28                 hit = true;
29                 break;
30             } else {
31                 tile = this.layerShitou.getTile(x, endy);
32                 if(tile) {
33                     hit = true;
34                     break;
35                 } 
36             }
37         }
38         // 左面的线
39         for(let y = starty; y<endy; y++) {
40             let tile:tiled.TMXTile = this.layerZhuan.getTile(startx, y);
41             if(tile) {
42                 hit = true;
43                 break;
44             } else {
45                 tile = this.layerShitou.getTile(startx, y);
46                 if(tile) {
47                     hit = true;
48                     break;
49                 } 
50             }
51         }
52         // 右面的线
53         for(let y = starty; y<endy; y++) {
54             let tile:tiled.TMXTile = this.layerZhuan.getTile(endx, y);
55             if(tile) {
56                 hit = true;
57                 break;
58             } else {
59                 tile = this.layerShitou.getTile(endx, y);
60                 if(tile) {
61                     hit = true;
62                     break;
63                 } 
64             }
65         }
66 
67         return hit;
68     }

 

Guess you like

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