Objective-C - the start method several times when the start of another method is completed

I have two methods that create a random node sprite and add it to the scene. Name them spriteMethod1and spriteMethod2.

I would like to have a method of looping, which starts spriteMethod1, 5 timesand then spriteMethod2once. There should also be a delay between each call spriteMethods.

I thought the following might work, but it is not:

-(void) addObjects {
    for (int i = 0; i < 5; i++) {
        [self performSelector:@selector(spriteMethod1) withObject:nil afterDelay:2];
    }
    [self performSelector:@selector(spriteMethod2) withObject:nil afterDelay:3];
    [self performSelector:@selector(addObjects) withObject:nil afterDelay:5];
}
+3
source share
5 answers

I know this may not be the best solution, but it works for me:

-(void)addObjects {
    [self performSelector:@selector(spriteMethod1) withObject:nil afterDelay:2];
    [self performSelector:@selector(spriteMethod1) withObject:nil afterDelay:4];
    [self performSelector:@selector(spriteMethod1) withObject:nil afterDelay:6];
    [self performSelector:@selector(spriteMethod1) withObject:nil afterDelay:8];
    [self performSelector:@selector(spriteMethod1) withObject:nil afterDelay:10];
    [self performSelector:@selector(spriteMethod2) withObject:nil afterDelay:13];
    [self performSelector:@selector(addObjects) withObject:nil afterDelay:18];
}
+1
source

Add a timer to your interface:

@property (nonatomic, weak) NSTimer *timer;

timer schedule somewhere in your code

self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(spriteMethod1) userInfo:nil repeats:YES];

do it

int count = 0;

- (void)spriteMethod1 {
        count ++;
        // do your work here

        if(count == 5) {
            // stop the timer
            [self.timer invalidate];

            // call the other methods
            [self performSelector:@selector(spriteMethod2) withObject:nil afterDelay:3];
            [self performSelector:@selector(addObjects) withObject:nil afterDelay:5];
        }
    }
+1
source

, , . documentation:

aSelector . (NSDefaultRunLoopMode). , . , ; , .

, .

[self spriteMethod1];
[NSThread sleepForTimeInterval:2.0f];

, .

0

. , :

- (void)startAddingObjects 
{
    NSNumber *counter = @(0);
    [self performSelector:@selector(addMoreUsingCounter:) 
               withObject:counter 
               afterDelay:2];
}

- (void)addMoreUsingCounter:(NSNumber *)counter
{
    int primCounter = [counter intValue];
    if (primCounter < 5)
    {
        [self spriteMethod1];
        [self performSelector:@selector(addMoreUsingCounter:) 
                   withObject:@(primCounter++) 
                   afterDelay:2];
    }
    else if (primCounter < 7)
    {
        [self spriteMethod2];
        [self performSelector:@selector(addMoreUsingCounter:) 
                   withObject:@(primCounter++) 
                   afterDelay:3];
    }
}

, , - , , .

0
source

I think your version does not work, because the parameter "afterDelay" refers to the time of the call. You will need to multiply it by "i" in the for loop, and then use 13 and 18 respectively for the last two selectors.

Take a look at using NSOperationQueue. You can set its maxConcurrentOperationCount to 1 to ensure that it performs its actions sequentially. For instance.

NSOperationQueue * opQueue = [[NSOperationQueue alloc] init];
opQueue.maxConcurrentOperationCount = 1; // this ensures it'll execute the actions sequentially

NSBlockOperation *spriteMethod1Invoker = [NSBlockOperation blockOperationWithBlock:^{
    for (int i = 0; i < 5; ++i)
    {
        [self spriteMethod1];
        sleep(2);  // sleep 2 secs between invoking spriteMethod1 again
    }
}];

NSInvocationOperation *spriteMethod2Invoker = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(spriteMethod2) object:nil];
[opQueue addOperation:spriteMethod1Invoker];
[opQueue addOperation:spriteMethod2Invoker];
0
source

All Articles