Fighter類的定義在Fighter.js中,Fighter類繼承與PhysicsSprite。this
原版的Fighter.js:spa
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相關的信息:code
Bullet的shape定義就比較簡單(直接一個矩形)。
圓形的定義可參見Enemy.js中隕石和行星的定義;
多邊形的定義可參見Enemy.js或Fighter.js中飛機的定義;
簡單矩形的定義可參見Bullet.js中子彈的定義。blog