Reputation: 63
So i have a block moving with the help of iPhones built-in accelerometer, and another block that randomly appears on the screen. I'm trying to use an if statement to determine if the moving block taps or touches the immobile target-block, in which if it does it'll relocate to another position on the screen randomly. Everything works BESIDES determining if the two coordinates are equal at any given point. Here's so far..
edit: *so I removed the xx & yy variables and replaced them with self.xVar and self.yVar which seemed to work for a bit, but was very sketchy and stopped
edit2: **so it removing the xx and yy did help, but it only works for 2-3 taps, and then stops.
edit3 **realized that having the same x OR y variable isn't right way to go about this..
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
...
//Make the new point
CGPoint buttonNewCenter = CGPointMake(newX,newY);
Button.center = buttonNewCenter;
int xx = self.xVar;
int yy = self.yVar;
if (newX == xx || newY == yy) {
int randX = arc4random() % 320;
int randY = arc4random() % 548;
CGPoint randNewPlace = CGPointMake(randX, randY);
Rand.center = randNewPlace;
}
}
... ...
- (void)viewDidAppear:(BOOL)animated
{
[self awakeaccelerometer];
int randX = arc4random() % 320;
int randY = arc4random() % 548;
CGPoint randNewPlace = CGPointMake(randX, randY);
Rand.center = randNewPlace;
self.xVar = (randX+15);
self.yVar = (randY+15);
}
So the opening function determines randomly where the target-block is places, whilst the other block moves freely on the screen based on the accelerometer. I'm trying to determine if self.xVar || self.yVar == newX || newY
at any given time. Thanks in advance!
Upvotes: 2
Views: 103
Reputation: 506
I am not sure why you are trying to see if two points are in the same place rather than seeing if the two rectangles intersect. Points are very small. If what you want to do is to see whether a point is within a rectangle, you can use CGRectContainsPoint
. (See the discussion on apple's website here.) If you want to see whether two rectangles intersect (which I think is what you are trying to do), use CGRectIntersectsRect
. (You may need to call CGRectMake first.)
If you really want to do the math, then check if (self.x+self.width<new.x or new.x+new.width<self.x or self.y+self.height<new.y or new.y+new.height<self.y)
Upvotes: 1