【问题标题】:Slowdown when loading the same SpriteFrame used in an animation in Cocos2dx在 Cocos2dx 中加载动画中使用的相同 SpriteFrame 时减速
【发布时间】:2015-07-02 08:58:48
【问题描述】:

我目前的游戏速度严重下降。我已将其范围缩小到与纹理动画相关的内容。

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

首先我像这样加载精灵帧缓存

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));

这段代码的作用是:

  1. 检查方向
  2. 根据方向从缓存中获取精灵帧
  3. 永远重复运行该动作。

但是,每次更改方向以设置活动角色的新动画时,都会运行此代码。此外,一次我可以让大约 40-50 个这样的角色出现。

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

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

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

【问题讨论】:

    标签: c++ cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    您绝对应该使用 addAnimationgetAnimation 方法将动画缓存在 AnimationCache 中。

    【讨论】:

    • 听从您的建议,我现在使用动画缓存,性能有了很大提高。我仍然想知道为什么使用 SpriteFrameCache 时性能如此糟糕,因为它应该对精灵帧做同样的事情......
    • Well SpriteFrameCache 提高了从 TexturePacker PLIST 或其他方式加载的精灵帧的加载时间或使用率。它不应该提高每帧或每 0.1 秒创建动画的性能。
    • 例如,您也可以尝试通过从缓存中获取精灵帧然后在每次更新或每个 .1 时设置精灵帧来运行动画。本质上是 Animate 动作在内部执行的操作。
    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多