ios - Spawn UIImageView So That It Does Not Intersect With Existing UIImageViews -


basically, have app user dodges bombs , collects coins. when user collects coin, coin spawned. want each coin not spawn on bomb. (by way these uiimageviews). in code below, have array of bombs called bombarray , uiimageview called "one" coin uiimageview. know code below doesn't work, other method(s) use? thanks, , here code:

    uiimageview *one = [[uiimageview alloc]initwithimage:[uiimage imagenamed:@"goldcoin.png"]];     cgrect rectone = cgrectmake(arc4random() % (900), arc4random() % (700), 40, 40);     [one setframe:rectone];     [self.view addsubview:one];     (uiimageview* 2 in bombarray)     {         while (cgrectintersectsrect(two.frame, one.frame))              {                 one.center=cgpointmake(arc4random() % (900), arc4random() % (700));              }      } 

*note: understand why code above not work, cannot find solution problem.

first of make such thing i'd use new spritekit gives physics (and collision detection) out of box.

but if want standard uiviews can being absolutely sure images don't collide between them divide container in grid , loop through each cell of grid , "flip coin" randomly put coin or not in cell. if want fill more space instead of dividing rectangular cells can divide hexagonal cells, in way cover more space.

so fast , absolutely not checked version this:

int x = 0; int y = 0; int side = 40; int = 0; {     {         cgrect imageframe = cgrectmake(x, y, side, side);         uiimageview* imageview = [[uiimageview alloc] initwithframe:imageframe];         imageview.layer.bordercolor = [uicolor blackcolor].cgcolor;         imageview.layer.borderwidth = 1.0;         imageview.layer.cornerradius = side / 2;         imageview.layer.maskstobounds = yes;         [self.containerview addsubview:imageview];         bool flip = arc4random() & 0x1;         if (flip) {             [imageview setimage:[uiimage imagenamed:@"coin_image"]];         }     } while ((x+=side) < (self.containerview.frame.size.width - side / 2));     it++;     if (it % 2 != 0) {         x = side / 2;     } else {         x = 0;     } } while ((y+=(side * 0.85)) < (self.containerview.frame.size.height - side) ); 

mind not efficient, executed in main thread , doesn't take in account possible (actually probable) memory constraints.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -