iphone - making random objects with NSTimer in cocos2d -


hi i'm making game cocos2d.i want create random objects nstimer. objects stored in nsarray.i'm using following way, every time timer initializes same thing happening. if random wouldn't work. i'm weird. happy if tell me wrong. or tell me how access random objects of array.

   -(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]; } 

with cocos2d, nstimer isn't recommended because unaffected build-in pause/resume functionality. instead, schedule method follows:

[self schedule:@selector(randomobjects:) interval:1.0f]; 

you have change randomobjects definition to:

-(void)randomobjects:(cctime)dt 

where dt actual time passed.

lastly, instead of using rand(), use:

arc4random() 

that function auto-seeds, don't need worry seeding every time.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -