★ TouchArcade needs your help. Click here to support us on Patreon.

Call function with timer

10-12-2009, 06:02 AM
#1
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
Call function with timer

Hey guys,
Is it possible to call a function like

Code:
[self moveImage:image];
With a timer?

I've tried

Code:
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveImage:image) userInfo:nil repeats:YES];
and

Code:
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector([self moveImage:image]) userInfo:nil repeats:YES];
But none succeeded.

Is there a way to do this?

Thanks.
10-12-2009, 10:29 AM
#2
You're missing a step. MoveImage shouldn't be in the timer call. You need to call a timer action, with MoveImage inside it. Something like this:

Code:
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(TimerAction:) userInfo:nil repeats:YES];

-(void)TimerAction:(NSTimer*)pTimer
{
   [self moveImage:image];

}
10-12-2009, 03:52 PM
#3
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
That's right, and if image is local to the method that fired the timer you'll need to pass it as the userInfo parameter when you create the timer. Then in TimerAction: it can be accessed by pTimer.userInfo.

You might also want to look up performSelector:withObject:afterDelay: it does the same thing but the SDK deals with the timer instead of you For example:

Code:
[self performSelector:@selector(moveImage:) withObject:image afterDelay:0.05];

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz

Last edited by PixelthisMike; 10-12-2009 at 03:55 PM. Reason: Added code snippet
10-12-2009, 06:05 PM
#4
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu