C++ 加载Cocos2dx中动画中使用的同一SpriteFrame时速度减慢

C++ 加载Cocos2dx中动画中使用的同一SpriteFrame时速度减慢,c++,cocos2d-x,cocos2d-x-3.0,C++,Cocos2d X,Cocos2d X 3.0,我目前正在经历一些严重的游戏减速。我已经把范围缩小到与纹理动画相关的东西 在我的游戏中,有些角色会朝4个可能的方向中的1个走,他们会走到一个点,然后改变方向继续走(有点像塔防游戏) 首先,我像这样加载sprite帧缓存 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("characters.plist"); 此代码在应用程序的生命周期内只运行一次 当角色加载到屏幕时,将使用以下代码设置其动画: int direction

我目前正在经历一些严重的游戏减速。我已经把范围缩小到与纹理动画相关的东西

在我的游戏中,有些角色会朝4个可能的方向中的1个走,他们会走到一个点,然后改变方向继续走(有点像塔防游戏)

首先,我像这样加载sprite帧缓存

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));
int方向=0;
整数=0;
如果(此->到_x<0)//左
{
方向=1;
数字=1;
}
否则,如果(此->到\u x>0)//正确
{
方向=2;
数字=1;
}
如果(此->到y<0)//向下
{
方向=0;
数字=0;
}
否则如果(此->to_y>0)//Up
{   
方向=3;
数量=2;
}
int s=0//皮肤
//设置动画
动画*动画=动画::创建();
对于(inti=0;igetSpriteFrameByName(frame\u sprite\u name);
如果(帧){
动画->添加SpriteFrame(帧);
}否则{
打破
}
}
//当雪碧向右移动时,将其反转
如果(方向==2){
setFlippedX(真);
}否则{
setFlippedX(假);
}
//根据类型设置动画的速度
如果(名称==“跑步者”){
动画->setDelayPerUnit(0.15f);
}否则{
动画->setDelayPerUnit(0.3f);
}
动画*动画=动画::创建(动画);
这->停止所有操作();
这->运行操作(RepeatForever::创建(动画));
此代码的作用是:

  • 检查方向
  • 根据方向从缓存中获取精灵帧
  • 使用repeat forever运行操作
  • 但是,每次更改方向以设置活动角色的新动画时,都会运行此代码。同时,我可以有大约40-50个这样的角色

    我注意到,在游戏中几分钟后,当一个新的“角色”被创造出来时,减速就开始发生了(因为它们是在波浪中快速连续地创造出来的)。当角色改变方向时,也会发生减速。这让我相信我用错了纹理

    如果有人知道如何解决这个问题,请告诉我


    PD:我在考虑预加载所有动画的可能性,然后让代表角色的每个精灵运行相应的动画。

    你一定应该使用
    addAnimation
    getAnimation
    方法将动画缓存在AnimationCache中。

    按照你的建议,我现在正在使用动画缓存,性能有了很大提高。我仍然想知道为什么使用SpriteFrameCache时性能如此差,因为它应该对sprite帧执行相同的操作…SpriteFrameCache可以提高加载时间或从TexturePacker PLIST或其他方式加载的sprite帧的使用率。它不应该提高每帧或每.1秒创建一个动画的性能。例如,您也可以尝试通过从缓存中获取一个精灵帧,然后在每次更新或每.1次更新时设置精灵帧来运行动画。本质上,动画动作在内部做什么。