2010-12-03 80 views
0

我一直在使用一個非常好的教程各2只母雞(6個圖像),而我試圖動畫(母雞行走)三個簡單的圖像由Ray Wenderlich:每秒幀速率(FPS)在Coco2d遊戲iPhone中開始下降?

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

動畫當我開始遊戲時工作正常,但在2-3分鐘後幀速率開始下降,慢慢下降到10以下,應用程序掛起..順便說一句,我正在使用iPhone 3G與iOS 4.1 ...可以這樣做的原因在一段時間之後,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]; 
+0

如果您需要幫助,可能需要發佈一些代碼。 – 2010-12-03 04:59:57

+0

我已經添加了代碼...當我嘗試用泄漏工具運行它時,它顯示沒有泄漏,但對象分配相當高,因爲我也嘗試每1秒用計時器調用此代碼? – hemant 2010-12-03 08:56:54

回答

1

聽起來像你可能有內存泄漏。我的建議是在設備上運行Leaks和ObjectAlloc儀器。此外,如果您需要更詳細的幫助,您應該發佈相關代碼。

+0

這也是我的假設。 – 2010-12-03 05:24:39

0

內存泄漏似乎最有可能。

如果不是這樣,那麼我會考慮一些事情,比如你正在遍歷的數據結構,你隨着時間的推移建立的任何類型的決策樹元素,這種事情。任何不一定存在內存泄漏的地方,但有些東西可能會合法地變得龐大或複雜,這就需要增加處理時間。

如果應用程序必須每秒處理數次這樣的事情,那麼一切都會變慢。它可能是這樣的一種情況,即它幾分鐘內變得複雜或者大得多,並且早期的加倍仍然是快速且容易計算的。當它從原來的256倍增加到原來的256倍時,減速開始變得明顯。