Search code examples
ioscocos2d-iphoneint

Call event every random number


Im trying to work out how to do a event every random number. I am using the following to get a random number between 50 + 100

int x = (arc4random() % 50) + 50;

I then want to call a selector based on the returned value as so.

[_hero schedule:@selector(randomAnimation) interval:x];

Im trying to work out in my head how I would rerun the schedule after the random time is up with the New Random time.


Solution

  • in the _hero class randomAnimation method :

    -(void) randomAnimation {
    
        // do the animation stuff here
    
        // then
    
        int x = (arc4random() % 50) + 50;
        [self scheduleOnce:@selector(randomAnimation) delay:x];
    
    
    }
    

    and fire up the sequence with

    [_hero randomAnimation];
    

    edit : if you dont want to expose the randomAnimation method, fire the animation when the object is added to the scene like this :

    -(void) onEnter {
        [super onEnter];
        [self randomAnimation];
    }