views:

2297

answers:

5

hello, I am trying to developed a iPhone app by using cocos2d. I using "scheduledTimerWithTimeInterval" for calling a method that called a fixed time interval. But now time interval increases in gradually. for this reason the time is slow in gradually. here is my code:

- (void) methodTime: (id) sender{

    NSTimer *rat =[NSTimer scheduledTimerWithTimeInterval:(.5) target:self selector:@selector(rotation:) userInfo:nil repeats:YES]; 
}

- (void) rotation:(NSTimer *)theTimer{

    NSLog(@"I m  # %i", i);
    i=i+10;   // Here , i is a global int variable.
    i=i % 1440;
     if(i==0){
      [theTimer invalidate];

     }
     else {
      int rotationNum=i;
     Sprite *sp = [Sprite spriteWithFile: @"1.png"];
     sp.position=cpv(220,180.5);
     sp.rotation=rotationNum;
     [self add:sp];  
     }

}
A: 

I had trouble understanding your question. You haven't even used one quotation mark in your 'question'.

Is it possible that your code needs more than 0.5 seconds to execute? Especially because the amount of work done in the background increases each iteration. (More sprites.)

Try to make this function faster, for example load that PNG into memory and don't load it each time from the file. (That's better for memory anyway.)

Btw: That loop looks dangerously like an infinite loop. (Though could be that it works…)

Georg
A: 

If all you're doing is continually rotating the object, CATransforms and Core Animation might be a better bet.

August
+2  A: 

It looks like every 0.5 seconds you are adding a sprite to some list of sprites. Eventually the list is getting very large and all that data causes your method to take longer than 0.5 seconds to execute. This causes the timer to fire fast as it can, which is not all that fast since its always waiting for your method to be finished with.

Without know more about your code, that's my best guess.

Squeegy
A: 

I would use Cocos2d-iphone's built in intervals.

Go through the demo cocos project and you will see how things like this "should" be done in the cocos framework.

Genericrich
+2  A: 

Don't use NSTimer. Check out cocos2d best practices.

EightyEight