之前游戏越玩越卡的问题发现了,是在使用prefab的时候在节点里添加了dragon组件,然后在使用对象池。每次拿出来时都会给骨骼动画设置图片。如下面的代码:
/* 创建对象池 /
createRolePool:function(){
this.rolePool = new cc.NodePool();
let initCount = 60;
for (let i = 0; i < initCount; ++i) {
let role = cc.instantiate(this.pro); // 创建节点
this.rolePool.put(role); // 通过 put 接口放入对象池
}
},
/ 创建role对象 */
createRole:function(position,aidPosition,index,zindex,userId){
let role = null;
if (this.rolePool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
role = this.rolePool.get();
} else { // 如果没有空闲对象,也就是对象池中备用对象不够时,我们就用 cc.instantiate 重新创建
role = cc.instantiate(this.pro);
}
for (let k = 0; k < this.roleAnimations[index].length; k++) { //this.roleAnimations保存的是骨骼动画的资源。。
if (this.roleAnimations[index][k] instanceof dragonBones.DragonBonesAsset) {
role.getComponent(dragonBones.ArmatureDisplay).dragonAsset = this.roleAnimations[index][k];
}
if (this.roleAnimations[index][k] instanceof dragonBones.DragonBonesAtlasAsset) {
role.getComponent(dragonBones.ArmatureDisplay).dragonAtlasAsset = this.roleAnimations[index][k];
}
role.getComponent(dragonBones.ArmatureDisplay).armatureName = parseInt(this.roleAnimations[index][0].name) + "";
role.getComponent(dragonBones.ArmatureDisplay).animationName = "run";
role.getComponent(dragonBones.ArmatureDisplay).playAnimation("run",0);
}
role.name = "role" + userId;
role.roleType = index;
role.zIndex = zindex;
role.stopAllActions();
role.parent = this.roleBox; // 将生成的角色加入节点树
role.setPosition(position);
role.runAction(cc.moveTo(0.5, aidPosition));
setTimeout(()=>{
this.rolePool.put(role)
},4000);
},
但是这样创建的话似乎资源没有释放。。而直接在预制体里放好骨骼动画则不会出现不会释放的问题,但是问题是每个角色都是不一样的动画,必须要在使用时才添加骨骼,那我该怎么释放骨骼资源呢,这些骨骼资源是在加载时就load下来并保存在this.roleAnimations中。我也是刚用coocs不久,求大佬们帮下。