[英]Random implementation of methods in cocos2d-iphone?
I have some "void" methods in my project using cocos2d-iphone and would like make them random. 我在项目中使用cocos2d-iphone有一些“无效”方法,并希望使它们随机。 Unfortunately, I'v found only little information about genareting random numbers.
不幸的是,我发现关于genareting随机数的信息很少。 Any help is appreciated!
任何帮助表示赞赏!
Try this
.h
-(int) randomGenarete;
.mm
-(int) randomGenarete
{
int random_number = arc4random() % 100; // return random 0 to 99
return random_number;
}
If I understand correctly you want to call a random method, right? 如果我理解正确你想要一个随机的方法,对吧?
Get your method signatures in an array: 在数组中获取方法签名:
NSArray* methods = @[@"myMethod1", @"myMethod2", @"myMethod3"];
Pick a method name: 选择一个方法名称:
NSString* method = methods[arc4random()%method.count];
Call it: 称它为:
[self performSelector:NSSelectorFromString(method)];
It would be also wise to check that self can respond to that selector first. 检查self是否可以首先响应该选择器也是明智之举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.