2014-10-28 44 views
0

在我創建的遊戲中,我創建了一個遊戲模式,玩家有30秒鐘可以擊中儘可能多的目標。我在 - (void)更新中添加了一個30秒倒數計時器:(CFTimeInterval)currentTime方法。倒計時開始時,幀速率顯着下降,每當物體與目標發生碰撞時,都會使用99%的CPU。計時器不運行時不會發生這種情況。有另一種方法來添加倒數計時器嗎?如果不是,我如何減少CPU使用率並保持幀率不變?我使用的是Xcode 6.1,代碼如下。這也是iOS 8的問題。在iOS 7.1上它運行良好。iOS 8如何防止Spritekit幀速下降

-(void)update:(CFTimeInterval)currentTime { 
/* Called before each frame is rendered */ 
//reset counter if starting 
int highscoret = [[NSUserDefaults standardUserDefaults] integerForKey: @"highScoret"];  
if (startGamePlay){ 
startTime = currentTime; 
startGamePlay = NO; 
} 
countDown.text = [NSString stringWithFormat:@"Start!"]; 

int countDownInt = 30.0 -(int)(currentTime-startTime); 
if(countDownInt>0){ //if counting down to 0 show counter 
countDown.text = [NSString stringWithFormat:@"%i", countDownInt]; 
}else if(countDownInt==0) { //if not show message, dismiss, whatever you need to do. 
[email protected]"Time's Up!"; 

self.highScoret.text = [NSString stringWithFormat:@"%d Baskets",highscoret]; 
SKScene *endGameScene = [[GameOverT alloc] initWithSize:self.size]; 
SKTransition *reveal = [SKTransition fadeWithDuration:0.5]; 
[self.view presentScene:endGameScene transition:reveal]; 
[self saveScore2]; 

if (self.points > highscoret) { 
    self.highScoret.text = [NSString stringWithFormat:@"%d Points",highscoret]; 
    [self saveScore]; 
    self.points = 0; 
    self.scoreLabel.text = [NSString stringWithFormat:@"%d Points",_points]; 
} 

else { 
    self.points = 0; 
    self.scoreLabel.text = [NSString stringWithFormat:@"%d Points",_points]; 
    self.highScoret.text = [NSString stringWithFormat:@"%d Points",highscoret]; 
} 


} 
} 

這裏還有其他相關的代碼更新功能裏面

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
/* Called when a touch begins */ 
for (UITouch *touch in touches) { 
CGPoint location = [touch locationInNode:self]; 
if (CGRectContainsPoint(countDown.frame, location)){ 
    startGamePlay = YES; 
    self.baskett = 0; 
    [self reset]; 
} 
} 

UITouch * touch = [touches anyObject]; 
CGPoint location = [touch locationInNode:self]; 

SKSpriteNode * object = [SKSpriteNode spriteNodeWithImageNamed:@"object.png"]; 
object.position = self.object2.position; 

object.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2]; 
object.physicsBody.dynamic = YES; 
object.physicsBody.categoryBitMask = objectCategory; 
object.physicsBody.contactTestBitMask = targetCategory; 
object.physicsBody.collisionBitMask = 0; 
object.physicsBody.usesPreciseCollisionDetection = YES; 
CGPoint offset = rwSub(location, object.position); 

if (offset.y <= 0) return; 

[self addChild:object]; 

CGPoint direction = rwNormalize(offset); 

CGPoint shootAmount = rwMult(direction, 1000); 

CGPoint realDest = rwAdd(shootAmount, object.position); 

float velocity = 200/1.0; 
float realMoveDuration = self.size.width/velocity; 
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration]; 
SKAction * actionMoveDone = [SKAction removeFromParent]; 
[object runAction:[SKAction sequence:@[actionMove, actionMoveDone]]]; 
} 

-(void)object:(SKSpriteNode *) object didCollideWithTarget:(SKSpriteNode *) target { 
[object removeFromParent]; 

[[self scene] runAction:[SKAction playSoundFileNamed:@"effect2.wav" waitForCompletion:NO]]; 
self.points++; 
self.scoreLabel.text = [NSString stringWithFormat:@"%d Points",_points]; 

if (self.points == 3) { 
[self obstacle]; 
} 

} 
+0

用儀器找出一個壞主意究竟是在吃掉CPU時間,否則這只是猜測,並可能需要更長時間才能弄清楚,而不是學習使用儀器將花費的時間。 – LearnCocos2D 2014-10-28 10:36:44

回答

1

定時器用於skaction與waitForDuration這樣可以解決售後服務的問題

SKAction *updateTime= [SKAction sequence:@[ 
               ///fire function after one second 
               [SKAction waitForDuration:1.0f], 
               [SKAction performSelector:@selector(callFunction) 
                   onTarget:self] 

               ]]; 
    //where 30 is number of time you want’s to call your function 
    [self runAction:[SKAction repeatAction:updateTime count:30] ]; 




-(void) callFunction 
{ 

    //what ever you want to do 
} 
+0

這樣做效果會更好!無論如何要讓計時器可見嗎? – master248 2014-10-28 13:27:23

+0

創建一個變量,它保存你的定時器執行的時間(NSinteger t = 100)的時間也創建一個SKLabelNode指定它並每次減少一個t值 - (void)callFunction excute – dragoneye 2014-10-29 05:07:35

+0

我試過類似的東西,但是LabelNodes出現在彼此的頂部。我試圖創建刪除和添加新LabelNodes的SKActions,但它不能很好地工作,所以我決定不使用。謝謝你的幫助! – master248 2014-10-30 16:16:37

相關問題