LostRoutes项目日志——玩家飞机精灵Fighter解析

Fighter类的定义在Fighter.js中,Fighter类继承与PhysicsSprite。

原版的Fighter.js:

 1 var Fighter = cc.PhysicsSprite.extend({
 2         hitPoints: true,        //当前的生命值
 3         space: null,            //所在物理空间
 4         ctor: function (spriteFrameName, space) {
 5             this._super(spriteFrameName);
 6             this.space = space;
 7 
 8             var verts = [
 9                 -94, 31.5,
10                 -52, 64.5,
11                 57, 66.5,
12                 96, 33.5,
13                 0, -80.5];
14 
15             this.body = new cp.Body(1, cp.momentForPoly(1, verts, cp.vzero));
16             this.body.data = this;
17             this.space.addBody(this.body);
18 
19             var shape = new cp.PolyShape(this.body, verts, cp.vzero);
20             shape.setElasticity(0.5);
21             shape.setFriction(0.5);
22             shape.setCollisionType(Collision_Type.Fighter);
23             this.space.addShape(shape);
24 
25             this.hitPoints = Fighter_hitPoints;
26 
27             var ps = new cc.ParticleSystem(res.fire_plist);
28             //在飞机下面.
29             ps.x = this.getContentSize().width / 2;
30             ps.y = 0;
31             //ps.setRotation(180.0);
32             ps.setScale(0.5);
33             this.addChild(ps);
34         },
35 
36         //重写setPosition
37         setPosition: function (newPosition) {
38 
39             var halfWidth = this.getContentSize().width / 2;
40             var halfHeight = this.getContentSize().height / 2;
41             var pos_x = newPosition.x;
42             var pos_y = newPosition.y;
43 
44             if (pos_x < halfWidth) {
45                 pos_x = halfWidth;
46             } else if (pos_x > (winSize.width - halfWidth)) {
47                 pos_x = winSize.width - halfWidth;
48             }
49 
50             if (pos_y < halfHeight) {
51                 pos_y = halfHeight;
52             } else if (pos_y > (winSize.height - halfHeight)) {
53                 pos_y = winSize.height - halfHeight;
54             }
55 
56             this.body.setPos(cc.p(pos_x, pos_y));
57 
58         }
59     })  ;

第5~23行:设置飞机的物理引擎特性,这里使用物理引擎的目的是进行精确碰撞检测。
第27~32行:创建飞机后面(飞机的尾部/尾巴这里)喷射烟雾例子效果。
第29~30行:设置烟雾粒子在飞机的下面(尾部)。
第32行:由于粒子设计人员设计的粒子比较大,通过第32行代码ps.setScale(0.5)缩小一半。
第33行:this.addChild(ps)是将粒子系统添加到飞机精灵上。


这里附加一点子弹精灵Bullet及shape相关的信息:

Bullet的shape定义就比较简单(直接一个矩形)。
圆形的定义可参见Enemy.js中陨石和行星的定义;
多边形的定义可参见Enemy.js或Fighter.js中飞机的定义;
简单矩形的定义可参见Bullet.js中子弹的定义。

原文地址:https://www.cnblogs.com/moonlightpoet/p/5715647.html