- objective c large random method, am i doing this right?
- -(void)spawnStuff{
- CGPoint position;
- CGPoint position1;
- int chance = random()%10;
- switch (chance) {
- case 0:
- [self spawnWall];
- Position.y = 580;
- Position.x = 160;
- wall.center = Position;
- [self spawnWall];
- Position1.y = 480;
- Position1.x = 80;
- wall.center = Position1;
- }
- -(void)spawnWall{
- UIImage* myImage = [UIImage imageNamed:@"Wall.png"];
- Wall = [[Sprite alloc] initWithImage:myImage];
- CGRect rect = CGRectMake(0, 0, 90, 50);
- more initilization stuff }
- // If we want to perform some action randomly, then we build an array
- // of possibly actions (blocks), and then randomly pick one and execute it.
- // Figure out what type of block you want...
- typedef void(^ActionBlock)(void);
- NSMutableArray *possibleActions = [NSMutableArray array];
- [possibleActions addObject:^{
- NSLog(@"Doing Action Foo");
- }];
- [possibleActions addObject:^{
- NSLog(@"Doing Action Bar");
- }];
- [possibleActions addObject:^{
- NSLog(@"Doing Action Baz");
- }];
- [possibleActions addObject:^{
- NSLog(@"Doing Action FooBar");
- }];
- [possibleActions addObject:^{
- NSLog(@"Doing Action Blarg");
- }];
- [possibleActions addObject:^{
- NSLog(@"Doing Action Zip");
- }];
- // Now, when I want to pick an action to perform...
- for (int i = 0; i < 100; ++i) {
- ActionBlock block = [possibleActions objectAtIndex:arc4random_uniform(possibleActions.count)];
- block();
- }
- typedef void(^ActionBlock)(void);
- typedef int IntType;
- typedef void const * OpaqueType;
- IntType someInteger;
- OpaqueType someOpaqueValue;
- int functionOne(int arg1, double arg2) {
- // do whatever...
- }
- int functionTwo(int arg1, double arg2) {
- // do whatever...
- }
- int result = functionOne(42, 3.14159);
- int(*function)(int,double) = functionOne;
- int result = function(42, 3.14159); // Calls functionOne
- function = functionTwo;
- result = function(42, 3.14159); // calls functionTwo
- typedef int(*MyFunction)(int, double);
- typedef void(^ActionBlock)(void);
- [possibleActions addObject:^{
- NSLog(@"Doing Action Foo");
- }];
- for (int i = 0; i < 100; ++i) {
- // We have a bunch of blocks stored in the array.
- // We want to pick one at random, so we will get a random number
- // between [0, size of our array).
- uint32_t index = arc4random_uniform(possibleActions.count);
- // Now, we have a random index into our array. Get the object in that position.
- id randomObject = [possibleActions objectAtIndex:index];
- // We have an opaque id type, but we know it's a block. Cast it to a block type.
- ActionBlock block = randomObject;
- // Now, we have our block, in a type that the compiler will let us invoke.
- // Call it like any other function.
- block();
- }
- - (void)dealloc {
- [_timer invalidate];
- }
- typedef void(^ActionBlock)(void);
- - (void)callRandomFunction:(NSTimer*)timer {
- // Our userInfo is actually our array of blocks
- NSArray *actions = timer.userInfo;
- ActionBlock block = [actions objectAtIndex:arc4random_uniform(actions.count)];
- block();
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSMutableArray *possibleActions = [NSMutableArray array];
- // Create the list of possible functions that can be called...
- // Now setup a timer that performs callRandomFunction: every second
- // Pass the array of blocks as the userInfo of the timer.
- _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(callRandomFunction:) userInfo:possibleActions repeats:YES];
- // Whatever else you need in here...
- }
- - (void)viewDidUnload
- {
- [_timer invalidate];
- // Any other unload stuff...
- [super viewDidUnload];
- }