Call function with timer

Discussion in 'Public Game Developers Forum' started by kohjingyu, Oct 12, 2009.

  1. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    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. :)
     
  2. Little White Bear Studios

    Little White Bear Studios Well-Known Member
    Patreon Silver

    Aug 27, 2008
    2,572
    0
    0
    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];
    
    }
    
     
  3. PixelthisMike

    PixelthisMike Well-Known Member

    #3 PixelthisMike, Oct 12, 2009
    Last edited: Oct 12, 2009
    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];
     
  4. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    Oh. Thanks, guys. :)
     

Share This Page