开发者

Frame Rate per Second (FPS) starts to gdecrease in Coco2d game iPhone?

开发者 https://www.devze.com 2023-01-28 11:50 出处:网络
i have three simple images each for 2 hens (6 images) which I am trying to animate (a hen walking) using a very good tutorial by Ray Wenderlich:

i have three simple images each for 2 hens (6 images) which I am trying to animate (a hen walking) using a very good tutorial by Ray Wenderlich:

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

The animation works fine when I start the game but after 2-3 mins the Frame Rate begins to drop and slowly it drops to below 10 and the application hangs.. btw I am using iPhone 3G with iOS 4.1 ... can that be the reason for the FPS drop or just the iPhone becomes idle after some time?

[[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 *walkAnimFramesMo开发者_JAVA技巧nkey = [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]; 


Sounds like you might have a memory leak. My advice would be to run the Leaks and ObjectAlloc Instruments on the device. Also, you should post the relevant code if you want more detailed assistance.


A memory leak seems most likely.

If that's not it then I'd look at things like data structures you're traversing, any kind of decision-tree-like elements you're building over time, that kind of thing. Anything where there isn't necessarily a memory leak but something might be legitimately getting big or complex, requiring increasing processing time.

If the app has to work through this several-to-many times a second, everything would slow down. It could be the kind of thing where it's getting twice as complicated or large ever few minutes, and the early doublings are still fast and easy to calculate. When it doubles from, say, 256 times as complex as the original to 512 times, the slowdown starts to become apparent.

0

精彩评论

暂无评论...
验证码 换一张
取 消