こんにちは、cocos2d でゲームを作っています。でランダム オブジェクトを作成したいと考えていますNSTimer
。オブジェクトはNSArray
.I'mに次の方法で保存されますが、タイマーが初期化されるたびに同じことが起こっています。ランダムが機能しない場合と同じように。私は変わり者です。何がおかしいのか教えていただけると幸いです。または、配列のランダム オブジェクトにアクセスする方法を教えてください。
-(id)init{
if (self = [super init]) {
....
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(randomObjects) userInfo:nil repeats:NO];
....
}
return self;
}
-(void)randomObjects{
if (!isPaused) {
srand(time(NULL));
int randomObjectIndex = rand()%self.ObjectCount_forThisLevel;
NSLog(@"randomObjectIndex = %d",randomObjectIndex);
int randomObjectID = [[self.arrayForObjects objectAtIndex:randomObjectIndex] intValue];
NSLog(@"randomObjectID = %d",randomObjectID);
switch (randomObjectID) {
case kBoxObject:{
srand(time(NULL));
int x1 = rand()%200+100;
[self createBoxAtLocation:CGPointMake(x1, 500)];
break;
}
case kBrickObject:{
srand(time(NULL));
int x2 = rand()%200+100;
[self createBrickAtLocation:CGPointMake(x2, 500)];
break;
}
case kOrangeObject:{
srand(time(NULL));
int x3 = rand()%200+100;
[self createOrangeAtLocation:CGPointMake(x3, 500)];
break;
}
case kBoardObject:{
srand(time(NULL));
int x4 = rand()%200+100;
[self createBoardAtLocation:CGPointMake(x4, 500)];
break;
}
default:
break;
}
}
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(randomObjects) userInfo:nil repeats:NO];
}