当前位置:网站首页>Spin animation of Cocos performance optimization
Spin animation of Cocos performance optimization
2022-07-07 15:39:00 【Vegetable chicken on the road】
Recently, I was working on a tower defense project , How to put it? , Battery , Bullet attack effect plus hit effect, etc. Let's take a look at the code dc Stable at 200+, This is just the core game logic , If you add ui That's got it. , Ordinary mobile phones hang up directly after a few minutes , Then I think I can only optimize it first , At this time, I thought I had seen a sliding layer optimization code before , So I thought about taking his thoughts directly to use according to the gourd painting ladle , After practice, it is found that it is indeed feasible , Don't sigh big guy or big guy .
Let's take a look at what happened before optimization dc, There are only two kinds of guns 120 了 :
Look now ,n Kind of cannon plus bullet effect plus hit effect :
Let's talk about ideas directly , First in the main scene , Hang a node , Something specially used for rendering layers , For example, the cannon in this project , I'll screw it out and make a node for storage , And hang up the script turretRender.ts, It mainly does two things ,
1. Clone one that needs to be used spine Node and add it to this render node
2. When not in use destroy fall 
Code :
addRenderNodeToRoot(type,name:string, node:cc.Node){
let baseNode:cc.Node = this._turretRoot;
let parentNode:cc.Node = baseNode.getChildByName(name);
if(parentNode){
parentNode.addChild(node);
return;
}
parentNode = new cc.Node;
parentNode.name = name;
baseNode.addChild(parentNode);
parentNode.addChild(node);
}
removeSpineNodeFromRoot(node:cc.Node){
if(node){
node.destroy();
}
}
After we load spine Copy the original node , This copied node will exist as the node we need to render later and add it to the above creation turrtRender in , The code is as follows :
load spine Animation nodes , Give him a callback after loading :
loadSpineImg(callback: Function) {
if (this.bulletSpine) {
this.bulletSpine.skeletonData = null;
}
this.loadSpine("spine/turret/" + this.bulletData.type + "/bullet/" + this.bulletData.name, sp.SkeletonData, (error, sp: sp.SkeletonData) => {
if (error) {
cc.warn(error);
return;
}
this.bulletSpine.skeletonData = sp;
callback && callback();
})
}
Loading complete spine Handle cloning logic in the callback after the node , What we need to pay attention to here is , We need to hide the original node , Shows the cloned nodes , Position set , Later, the operations of the original interface are all changed to the cloned node operations , It should be noted that , Remember to judge whether the cloned node exists , Also, ensure that the location is updated in real time
this.loadSpineImg(() => {
if (this.cloneBulletSpine) {
turretRender.inst.removeSpineNodeFromRoot(this.cloneBulletSpine);
this.cloneBulletSpine = null;
}
this.cloneBulletSpine = cc.instantiate(this.bulletSpine.node);
this.cloneBulletSpine.removeAllChildren();
this.cloneBulletSpine.active = true;
this.bulletSpine.node.active = false;
let name: string = this.bulletData.type+"_paodan";
turretRender.inst.addRenderNodeToRoot(RenderNodeType.renderBulletSpine,name, this.cloneBulletSpine);
let wPos = this.bulletSpine.node.parent.convertToWorldSpaceAR(this.bulletSpine.node.getPosition());
let lPos = this.cloneBulletSpine.parent.convertToNodeSpaceAR(wPos);
this.cloneBulletSpine.setPosition(lPos);
// Callback
if (this.bulletData.Spine == 1) {
this.cloneBulletSpine.getComponent(sp.Skeleton).setAnimation(0, this.bulletData.animationName, this.bulletData.loop == "1");
this.isMoment = false;
} else {
this.cloneBulletSpine.getComponent(sp.Skeleton).clearTracks();
this.playAni();
}
});
Monitor the location to update the code in real time , Otherwise, the position will shift :
start() {
this.node.on(cc.Node.EventType.POSITION_CHANGED, this.updatePos, this);
this.bulletSpine.node.on(cc.Node.EventType.ROTATION_CHANGED, this.updatePos, this);
}
updatePos() {
if (this.bulletSpine && this.cloneBulletSpine) {
let wPos = this.bulletSpine.node.parent.convertToWorldSpaceAR(this.bulletSpine.node.getPosition());
let lPos = this.cloneBulletSpine.parent.convertToNodeSpaceAR(wPos);
this.cloneBulletSpine.setPosition(lPos);
this.cloneBulletSpine.angle = this.node.angle;
}
}
In fact, the main purpose of doing this is to jointly approve , If you add it one by one ,spine Batch operation will be interrupted in some places , It leads to the failure of the approval ,dc It will be quite high .
---------------------------------------------------------- Split line ----------------------------------------------------------------------
Two easy-to-use plug-ins are recommended :
1. see dc The drawing situation of :spector.js, How to use Baidu by yourself , Can detect after optimization dc Whether the drawing is correct
Pictured , Click on the blue part every time dc What are you drawing :
2.ccc-devtools plug-in unit , You can see the whole cocos Hierarchical structure , See which node a node is under , Here you can monitor cloned spine Whether the node is added turretRender Under the node 
边栏推荐
- 一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
- 2022年5月互联网医疗领域月度观察
- 居然从408改考自命题!211华北电力大学(北京)
- Window环境下配置Mongodb数据库
- Typescript release 4.8 beta
- 【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
- [server data recovery] data recovery case of raid failure of a Dell server
- 【目标检测】YOLOv5跑通VOC2007数据集
- Ctfshow, information collection: web12
- #HPDC智能基座人才发展峰会随笔
猜你喜欢

Ctfshow, information collection: web9

The download button and debug button in keil are grayed out

简述keepalived工作原理

Guangzhou Development Zone enables geographical indication products to help rural revitalization

#HPDC智能基座人才发展峰会随笔

Steps to create P8 certificate and warehousing account

众昂矿业:萤石继续引领新能源市场增长

Use cpolar to build a business website (2)

TypeScript 发布 4.8 beta 版本

【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
随机推荐
How to release NFT in batches in opensea (rinkeby test network)
[target detection] yolov5 Runtong voc2007 data set
Oracle control file loss recovery archive mode method
Runnable是否可以中断
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
2. 堆排序『较难理解的排序』
Getting started with webgl (4)
2.Golang基础知识
Monthly observation of internet medical field in May 2022
Do not use memset to clear floating-point numbers
What is Base64?
2. Basic knowledge of golang
Ctfshow, information collection: web9
使用Scrapy框架爬取网页并保存到Mysql的实现
Detailed explanation of Cocos creator 2.4.0 rendering process
Jacobo code coverage
Ctfshow, information collection: web12
How to build your own super signature system (yunxiaoduo)?
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
Getting started with webgl (3)