Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 3.66 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. objective c large random method, am i doing this right?
  2. -(void)spawnStuff{
  3. CGPoint position;
  4. CGPoint position1;
  5.  
  6. int chance = random()%10;
  7. switch (chance) {
  8.     case 0:
  9.             [self spawnWall];
  10.         Position.y = 580;
  11.         Position.x = 160;
  12.         wall.center = Position;
  13.  
  14.         [self spawnWall];
  15.         Position1.y = 480;
  16.         Position1.x = 80;
  17.         wall.center = Position1;
  18.  
  19. }
  20. -(void)spawnWall{
  21. UIImage* myImage = [UIImage imageNamed:@"Wall.png"];
  22. Wall = [[Sprite alloc] initWithImage:myImage];
  23.  
  24. CGRect rect = CGRectMake(0, 0, 90, 50);
  25. more initilization stuff }
  26.        
  27. // If we want to perform some action randomly, then we build an array
  28. // of possibly actions (blocks), and then randomly pick one and execute it.
  29. // Figure out what type of block you want...
  30.  
  31. typedef void(^ActionBlock)(void);
  32. NSMutableArray *possibleActions = [NSMutableArray array];
  33. [possibleActions addObject:^{
  34.     NSLog(@"Doing Action Foo");
  35. }];
  36. [possibleActions addObject:^{
  37.     NSLog(@"Doing Action Bar");
  38. }];
  39. [possibleActions addObject:^{
  40.     NSLog(@"Doing Action Baz");
  41. }];
  42. [possibleActions addObject:^{
  43.     NSLog(@"Doing Action FooBar");
  44. }];
  45. [possibleActions addObject:^{
  46.     NSLog(@"Doing Action Blarg");
  47. }];
  48. [possibleActions addObject:^{
  49.     NSLog(@"Doing Action Zip");
  50. }];
  51.  
  52. // Now, when I want to pick an action to perform...
  53. for (int i = 0; i < 100; ++i) {
  54.     ActionBlock block = [possibleActions objectAtIndex:arc4random_uniform(possibleActions.count)];
  55.     block();
  56. }
  57.        
  58. typedef void(^ActionBlock)(void);
  59.        
  60. typedef int IntType;
  61. typedef void const * OpaqueType;
  62.        
  63. IntType someInteger;
  64. OpaqueType someOpaqueValue;
  65.        
  66. int functionOne(int arg1, double arg2) {
  67.     // do whatever...
  68. }
  69. int functionTwo(int arg1, double arg2) {
  70.     // do whatever...
  71. }
  72.        
  73. int result = functionOne(42, 3.14159);
  74.        
  75. int(*function)(int,double) = functionOne;
  76. int result = function(42, 3.14159); // Calls functionOne
  77. function = functionTwo;
  78. result = function(42, 3.14159); // calls functionTwo
  79.        
  80. typedef int(*MyFunction)(int, double);
  81.        
  82. typedef void(^ActionBlock)(void);
  83.        
  84. [possibleActions addObject:^{
  85.     NSLog(@"Doing Action Foo");
  86. }];
  87.        
  88. for (int i = 0; i < 100; ++i) {
  89.     // We have a bunch of blocks stored in the array.
  90.     // We want to pick one at random, so we will get a random number
  91.     // between [0, size of our array).
  92.     uint32_t index = arc4random_uniform(possibleActions.count);
  93.  
  94.     // Now, we have a random index into our array.  Get the object in that position.
  95.     id randomObject = [possibleActions objectAtIndex:index];
  96.  
  97.     // We have an opaque id type, but we know it's a block.  Cast it to a block type.
  98.     ActionBlock block = randomObject;
  99.  
  100.     // Now, we have our block, in a type that the compiler will let us invoke.
  101.     // Call it like any other function.
  102.     block();
  103. }
  104.        
  105. - (void)dealloc {
  106.     [_timer invalidate];
  107. }
  108.  
  109. typedef void(^ActionBlock)(void);
  110.  
  111. - (void)callRandomFunction:(NSTimer*)timer {
  112.     // Our userInfo is actually our array of blocks
  113.     NSArray *actions = timer.userInfo;
  114.     ActionBlock block = [actions objectAtIndex:arc4random_uniform(actions.count)];
  115.     block();
  116. }
  117.  
  118. - (void)viewDidLoad
  119. {
  120.     [super viewDidLoad];
  121.  
  122.     NSMutableArray *possibleActions = [NSMutableArray array];
  123.  
  124.     // Create the list of possible functions that can be called...
  125.  
  126.     // Now setup a timer that performs callRandomFunction: every second
  127.     // Pass the array of blocks as the userInfo of the timer.
  128.     _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(callRandomFunction:) userInfo:possibleActions repeats:YES];
  129.  
  130.     // Whatever else you need in here...
  131. }
  132.  
  133. - (void)viewDidUnload
  134. {
  135.     [_timer invalidate];
  136.  
  137.     // Any other unload stuff...
  138.  
  139.     [super viewDidUnload];
  140. }