在Cocos2dx中加载动画中使用的同一SpriteFrame时减慢速度
Slowdown when loading the same SpriteFrame used in an animation in Cocos2dx
我目前在游戏中遇到了一些严重的减速。我已经将它缩小到与纹理动画相关的内容。
在我的游戏中,有一些角色沿着四个可能的方向中的一个走,他们会走到一个点,然后改变方向继续走(有点像塔防游戏)。
首先,我正在加载类似于的精灵帧缓存
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("characters.plist");
在我的应用程序生命周期中,此代码只运行一次。
当角色被加载到屏幕上时,他们的动画将使用以下代码进行设置:
int direction = 0;
int number = 0;
if (this->to_x < 0) // Left
{
direction = 1;
number = 1;
}
else if(this->to_x > 0) // Right
{
direction = 2;
number = 1;
}
if (this->to_y < 0) // Down
{
direction = 0;
number = 0;
}
else if(this->to_y > 0) // Up
{
direction = 3;
number = 2;
}
int s = 0; //skin
// Set the animation
Animation *animation = Animation::create();
for (int i = 0; i < INT16_MAX; i++)
{
string frame_sprite_name = StringUtils::format("%s_%d_%d_%d.png",parameters[name].image_name.c_str(),s,number,i);
auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frame_sprite_name);
if (frame) {
animation->addSpriteFrame(frame);
} else {
break;
}
}
// Invert the sprite when they go right
if (direction == 2) {
setFlippedX(true);
}else{
setFlippedX(false);
}
// Set the pace of the animation based on the type
if (name=="runner") {
animation->setDelayPerUnit(0.15f);
} else{
animation->setDelayPerUnit(0.3f);
}
Animate *animate = Animate::create(animation);
this->stopAllActions();
this->runAction(RepeatForever::create(animate));
这个代码的作用是:
- 检查方向
- 根据方向从缓存中获取精灵帧
- 永远重复这个动作
但是,每次更改方向以设置活动角色的新动画时,都会运行此代码。此外,在一个时间里,我可以让大约40-50个这样的角色四处走动。
我注意到,在游戏中几分钟后,一旦创建了一个新的"角色",速度就会开始放缓(因为它们是以波浪形式快速连续创建的)。当角色改变方向时,速度也会减慢。所以这让我相信我用错了纹理。
如果有人知道如何解决这个问题,请告诉我。
PD:我在考虑预加载所有动画的可能性,然后让代表角色的每个精灵运行相应的动画。
您肯定应该使用addAnimation
和getAnimation
方法将动画缓存在AnimationCache中。
相关文章:
- 为什么在读取文件大小时文件IO速度会发生变化
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 文件系统:复制功能的速度秘诀是什么
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- 两个连续的 OpenMP 并行区域会相互减慢速度
- 查找标准::hash_map与标准::矢量的速度
- 加快在C++中读取/处理日志文件的速度
- 为什么这些算法的运行速度比它们应该的要快?
- 如何提高文件的读取速度?
- 通过libpqxx提高PostgreSQL数据库的更新速度
- 使用 IMFSinkWriter 编码的视频的播放速度会根据宽度而变化
- 计算车辆之间的距离并设置速度,使距离保持不变,例如 5 米
- 如何加快大字符串的解析速度?
- 如何比较两个函数的速度和性能
- 线程相互减慢速度
- 多线程减慢程序速度:无错误共享,无互斥锁,无缓存未命中,无小工作量
- 使用 Unity 构建加快C++构建速度,并减少标头依赖项
- 从 OpenCV 3 切换到 OpenCV 4 会导致网络摄像头以最大 5 fps 的速度录制,而不是通常的 30 f
- 在Cocos2dx中加载动画中使用的同一SpriteFrame时减慢速度