Md Nasir Uddin
Md Nasir Uddin

Reputation: 2180

"scheduledTimerWithTimeInterval:" problem in cocos2d?

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

}

Upvotes: 0

Views: 4191

Answers (5)

EightyEight
EightyEight

Reputation: 3460

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

Upvotes: 2

Genericrich
Genericrich

Reputation: 4641

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.

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187024

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.

Upvotes: 2

August
August

Reputation: 12187

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

Upvotes: 0

Georg Schölly
Georg Schölly

Reputation: 126105

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…)

Upvotes: 0

Related Questions