当前位置:网站首页>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
边栏推荐
猜你喜欢
Unity之ASE实现全屏风沙效果
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
Create lib Library in keil and use lib Library
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
Unity's ASE realizes cartoon flame
MongoD管理数据库的方法介绍
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
Starting from 1.5, build a microservice framework link tracking traceid
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
Super signature principle (fully automated super signature) [Yun Xiaoduo]
随机推荐
从 1.5 开始搭建一个微服务框架链路追踪 traceId
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
Ida Pro reverse tool finds the IP and port of the socket server
Matlab experience summary
Ctfshow, information collection: web7
What are PV and UV? pv、uv
Wechat applet 01
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
Nacos conformance protocol cp/ap/jraft/distro protocol
[original] all management without assessment is nonsense!
Zhongang Mining: Fluorite continues to lead the growth of new energy market
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
避坑:Sql中 in 和not in中有null值的情况说明
Create lib Library in keil and use lib Library
Stm32f103c8t6 PWM drive steering gear (sg90)
[server data recovery] data recovery case of raid failure of a Dell server
Win10 or win11 taskbar, automatically hidden and transparent
【深度学习】图像超分实验:SRCNN/FSRCNN
What is data leakage