在Cocos2d游戏iPhone中,每秒帧速率(FPS)开始下降?

时间:2010-12-03 04:31:52

标签: iphone ios4 cocos2d-iphone

我有三张简单的图像,每张图片都有2只母鸡(6张图片),我正在尝试使用Ray Wenderlich的非常好的教程来制作动画(母鸡行走):

  

http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d

当我开始游戏时动画效果很好但是2-3分钟之后帧速率开始下降并慢慢下降到10以下并且应用程序挂起..顺便说一句我使用的是带有iOS 4.1的iPhone 3G ...这是导致FPS下降或者iPhone在一段时间后变得闲置的原因吗?

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"hen.plist"];        


CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"hen.png"];
[self addChild:spriteSheet];

// Load up the frames of our animation
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];

self.bear = [CCSprite spriteWithSpriteFrameName:@"1.png"];        
_bear.position = ccp(20,400);
self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[_bear runAction:_walkAction];
[spriteSheet addChild:_bear];

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"monkey.plist"];        


CCSpriteBatchNode *spriteSheetMonkey = [CCSpriteBatchNode batchNodeWithFile:@"monkey.png"];
[self addChild:spriteSheetMonkey];

NSMutableArray *walkAnimFramesMonkey = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
    [walkAnimFramesMonkey addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%d.png", i]]];
}
CCAnimation *walkAnimMonkey = [CCAnimation animationWithFrames:walkAnimFramesMonkey delay:0.1f];


self.monkey = [CCSprite spriteWithSpriteFrameName:@"1.png"];        
_monkey.position = ccp(40,80);
self.walkMonkey = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnimMonkey restoreOriginalFrame:NO]];
[_monkey runAction:_walkMonkey];
[spriteSheetMonkey addChild:_monkey];
float bearVelocity = 480.0/3.0;

CGPoint moveDifferenceHen = ccpSub(HenLoction, _bear.position);
float distanceToMoveHen = ccpLength(moveDifferenceHen);
float moveDurationHen = distanceToMoveHen / bearVelocity;

self.moveAction = [CCSequence actions:                          
                       [CCMoveTo actionWithDuration:moveDurationHen position:HenLoction],
                       [CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)],
                       nil
                       ];


[_bear runAction:_moveAction]; 

2 个答案:

答案 0 :(得分:1)

听起来你可能有内存泄漏。我的建议是在设备上运行Leaks和ObjectAlloc Instruments。此外,如果您需要更详细的帮助,则应发布相关代码。

答案 1 :(得分:0)

最有可能发生内存泄漏。

如果不是这样,那么我会看一些你正在遍历的数据结构,你正在构建的任何类似决策树的元素,这类事情。任何不一定存在内存泄漏但可能合法地变大或复杂的事情,需要增加处理时间。

如果应用程序必须每秒执行几次这样的操作,一切都会变慢。它可能是这样一种事情,它变得复杂两倍或几分钟,并且早期的倍增仍然快速且易于计算。当它比原来复杂的256倍增加到512倍时,减速开始变得明显。