微信飞机大战,只为熟悉cocos creator的属性方法,做了个简单的雏形
1.取节点的子节点用【getChillByName()】,也可以用【cc.find()】,取节点上的组件用【getComponent()】
2.用对象池可以减少性能损耗,比如在hero.js定义一个子弹的对象池
createBulletPool: function() {
this.bulletPool = new cc.NodePool();
let initCount = 7;
for (let i = 0; i < initCount; i++) {
let bullet = cc.instantiate(this.bulletPrefab);
this.bulletPool.put(bullet);
}
},
这样在bullet.js中如果要讲不用的子弹放回对象池,记得要停止子弹的行为
removeSelf: function() {
this.node.stopAllActions();
this.node.getParent().getChildByName("hero").getComponent('hero').bulletPool.put(this.node);
},
由于子弹会动,所以放回对象池的子弹位置什么的属性会变化,所以在制造子弹的时候要初始化子弹
hero.js:
createBullet: function() {
let bullet = null;
if (this.bulletPool.size() > 0) {
bullet =