【发布时间】: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));
这段代码的作用是:
- 检查方向
- 根据方向从缓存中获取精灵帧
- 永远重复运行该动作。
但是,每次更改方向以设置活动角色的新动画时,都会运行此代码。此外,一次我可以让大约 40-50 个这样的角色出现。
我注意到,在游戏中几分钟后,一旦创建了新“角色”,就会开始减速(因为它们是在波浪中快速连续创建的)。当角色改变方向时,也会发生减速。所以这让我相信我使用的纹理是错误的。
如果有人知道如何解决这个问题,请告诉我。
PD:我在考虑是否可以预先加载所有动画,然后让代表角色的每个精灵运行相应的动画。
【问题讨论】:
标签: c++ cocos2d-x cocos2d-x-3.0