0

In a game I have created I created a game mode where the player has 30 seconds to hit as many targets as they can. I added a 30 second countdown timer in the -(void)update:(CFTimeInterval)currentTime method. When the countdown is initiated, the frame rate drops significantly, and 99% of the cpu is used whenever the object collides with the target. This doesn't happen when the timer is not running. Is there another way to add a countdown timer? If not, how can I lessen the cpu usage and keep the frame rate constant? I am using Xcode 6.1 and the code is below. Also this is an iOS 8 problem. On iOS 7.1 it runs fine.

-(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.
countDown.text=@"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];
}


}
}

And here's other relevant code

-(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];
}

}
master45
  • 50
  • 1
  • 9
  • use Instruments to find out what exactly is eating up the CPU time, otherwise it's just guessing and will possibly take you longer to figure out than learning to use Instruments will take – CodeSmile Oct 28 '14 at 10:36

1 Answers1

1

timer inside a update function is a bad idea used skaction with waitForDuration like this may be slove your problem

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
}
dragoneye
  • 703
  • 6
  • 14
  • That works much better! Is there anyway to make the timer visible? – master45 Oct 28 '14 at 13:27
  • create a variable which holds number of time your timer execute (NSinteger t=100) also create a SKLabelNode assign it t and reduce t value by one every time -(void) callFunction excute – dragoneye Oct 29 '14 at 05:07
  • I tried something like that already, but the LabelNodes appear on top of each other. I tried to create SKActions that remove and add new LabelNodes, but it doesn't work well, so I decided to do without. Thanks for your help! – master45 Oct 30 '14 at 16:16
  • why you creating serval synodes just create one update its skiable.text property inside callFunction – dragoneye Oct 31 '14 at 05:14