如何在cocos2d iphone中以随机时间间隔调度或调用方法 [英] How to schedule or call a method at random time interval in cocos2d iphone
本文介绍了如何在cocos2d iphone中以随机时间间隔调度或调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在不规则时间间隔调用方法意味着它应该是随机时间加上我想要在一些定义范围内。
喜欢:它应该调用
[NSTimer scheduledTimerWithInterval:1.0 target:self selector:@selector(myMethod :) userInfo:nil repeats:YES];
void mymethod()
{
if(arc4random()%10 == 1)
{
//在这里调用我的方法;
}
}
这样,我不会得到随机化。
任何人都可以请帮我这个!
解决方案
在这里您可以创建一个随机间隔调用的调度程序。
- (void)randomTimeScheduler {
int time = arc4random()%5;
int nextTimeOfCall = 3 + time;
NSLog(它将在后调用:%d,nextTimeOfCall);
[self performSelector:@selector(randomTimeScheduler)withObject:self afterDelay:nextTimeOfCall];
}
您必须从课程中那么它将作为调度程序工作。它有有限的间隔时间3-8。
I want to call a method at Irregular time interval means it should be random time plus i want it in some define range too.
Like : it should call at any second between 3 to 8 .
I tried this one :
[NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(myMethod:) userInfo:nil repeats: YES];
void mymethod()
{
if(arc4random() % 10 == 1)
{
// calling my method here;
}
}
This way , i am not getting randomization which i want.
Any one can please help me on this !!!
解决方案
Here you can make a scheduler which will get called at random interval.
-(void)randomTimeScheduler{
int time = arc4random()%5;
int nextTimeOfCall = 3+time;
NSLog("it will be called after:%d",nextTimeOfCall);
[self performSelector:@selector(randomTimeScheduler) withObject:self afterDelay:nextTimeOfCall];
}
You have to call it from your class and then it will work as a scheduler. And it has finite interval time 3-8.
这篇关于如何在cocos2d iphone中以随机时间间隔调度或调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文