سؤال

I want to schedule these two sprites at random time during game play, then keep changing the time so the sprites appear randomly

I believe that something like this has to be done but it has not worked

int minDuration = 5.0;
int maxDuration = 30.0;
int rangeDuration = maxDuration - minDuration;
int randomDuration = (arc4random() % rangeDuration) + minDuration;

if (randomDuration == randomDuration) {
[self schedule:@selector(addshieldICON:) interval:10];
[self schedule:@selector(addspeedICON:) interval:10];
}

Any help or suggestions? Thank you

هل كانت مفيدة؟

المحلول

I would try something like this to start. It's difficult to tell from your question whether you want these two sprites to show up at the same time or if you want two separate schedules. Either way, you can modify this code to get the job done.

-(void)scheduleWithRandomInterval {
    int minDuration = 5.0;
    int maxDuration = 30.0;
    int rangeDuration = maxDuration - minDuration;
    int randomDuration = (arc4random() % rangeDuration) + minDuration;

    [self scheduleOnce:@selector(showIcons) delay:randomDuration];
}

-(void)showIcons {
    [self addshieldICON];
    [self addspeedICON];
    [self scheduleWithRandomInterval];
}

A word of caution though: I've had some issues with v2.1 adding a new scheduler from its own selector. If you notice that the scheduleWithRandomInterval is not called more than once, then you may need to do a bit of a hack job to get around it. In the past I've done it like this.

Instead of

    [self scheduleWithRandomInterval];

I would use

    [self delayedScheduleWithRandomInterval];

with a new function to handle a double delay

-(void)delayedScheduleWithRandomInterval {
    [self scheduleOnce:@selector(scheduleWithRandomInterval) 0.01];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top