我正在尝试创建一种碰撞检测方法,检查我的两个物体(2个椭圆形的uiimages)是否相互碰撞。如果他们这样做,应将BOOL值设置为YES,否则应将其设置为NO。
到目前为止,它只是进入IF语句,检查Ruby1是否高于Ruby2。它不关心ruby1是否低于ruby2,甚至不在其左侧或右侧。它只是继续进行IF声明。反之亦然。
-(void)CollisionDetect{
//can't be colliding because Ruby1 is too far left of Ruby2
if( rubin1.frame.origin.x + rubin1.bounds.size.width < rubin2.frame.origin.x){
CollisionRubies = NO;
NSLog(@"Collision left");
}
//can't be colliding because Ruby1 is too far right of Ruby2
if( rubin1.frame.origin.x > rubin2.frame.origin.x + rubin2.bounds.size.width){
CollisionRubies = NO;
NSLog(@"Collision right");
}
//can't be colliding because Ruby1 is too far below Ruby2
if( rubin1.frame.origin.y + rubin1.bounds.size.width < rubin2.frame.origin.y){
CollisionRubies = NO;
NSLog(@"Collision below");
}
//can't be colliding because Ruby1 is too far above Ruby2
if( rubin1.frame.origin.y < rubin2.frame.origin.y + rubin2.bounds.size.width){
CollisionRubies = NO;
NSLog(@"Collision above");
}
if( rubin2.frame.origin.x + rubin2.bounds.size.width < rubin1.frame.origin.x){
CollisionRubies = NO;
NSLog(@"Collision left");
}
//can't be colliding because Ruby2 is too far right of Ruby1
if( rubin2.frame.origin.x > rubin1.frame.origin.x + rubin1.bounds.size.width){
CollisionRubies = NO;
NSLog(@"Collision right");
}
//can't be colliding because Ruby2 is too far below Ruby1
if( rubin2.frame.origin.y + rubin2.bounds.size.width < rubin1.frame.origin.y){
CollisionRubies = NO;
NSLog(@"Collision below");
}
//can't be colliding because Ruby2 is too far above Ruby1
if( rubin2.frame.origin.y < rubin1.frame.origin.y + rubin1.bounds.size.width){
CollisionRubies = NO;
NSLog(@"Collision above");
}
//if we get here, the two rects MUST be colliding
CollisionRubies = YES;
NSLog(@"Collision detected");
}
如果BOOL值为NO,那么ruby1将移动到该位置,如下面的代码所示,否则它将停止移动,直到它检测到ruby2不在它前面。反之亦然。
-(void)dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position{
// Check to see which view, or views, the point is in and then move to that position.
if (CGRectContainsPoint([rubin1 frame], position)) {
if(!CollisionRubies){
rubin1.center = position;
}
if (CGRectContainsPoint([rubin2 frame], position)) {
if(!CollisionRubies){
rubin2.center = position;
}
}
从touchesmoved方法
调用碰撞检测方法 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self CollisionDetect];
NSLog(@"touchesMoved");
if(!GamePaused){
// Enumerates through all touch objects
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
}
我做错了什么?
的 的 ** * ** * ** * 的* 更新的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ***
-(void)CollisionDetect{
//不能碰撞,因为Ruby1离Ruby2太远了
if(rubin1.frame.origin.x + rubin1.bounds.size.width&lt; rubin2.frame.origin.x){
CollisionRubies = NO;
NSLog(@"Collision left");
}
//不能发生冲突,因为Ruby1对Ruby2来说太过分了
else if(rubin1.frame.origin.x&gt; rubin2.frame.origin.x + rubin2.bounds.size.width){
CollisionRubies = NO;
NSLog(@"Collision right");
}
//不能发生冲突,因为Ruby1远远低于Ruby2
else if(rubin1.frame.origin.y + rubin1.bounds.size.width&lt; rubin2.frame.origin.y){ CollisionRubies = NO; NSLog(@“下面的碰撞”); }
//不能发生冲突,因为Ruby1远远超过Ruby2
else if(rubin1.frame.origin.y&lt; rubin2.frame.origin.y + rubin2.bounds.size.width){ CollisionRubies = NO; NSLog(@“碰撞上面”); }
if( rubin2.frame.origin.x + rubin2.bounds.size.width < rubin1.frame.origin.x){
CollisionRubies = NO;
NSLog(@"Collision left");
}
//不能发生碰撞,因为Ruby2对Ruby1来说太过分了
else if(rubin2.frame.origin.x&gt; rubin1.frame.origin.x + rubin1.bounds.size.width){
CollisionRubies = NO;
NSLog(@"Collision right");
}
//不能碰撞,因为Ruby2远远低于Ruby1
else if(rubin2.frame.origin.y + rubin2.bounds.size.width&lt; rubin1.frame.origin.y){ CollisionRubies = NO; NSLog(@“下面的碰撞”); }
//不能碰撞,因为Ruby2远远高于Ruby1
else if(rubin2.frame.origin.y&lt; rubin1.frame.origin.y + rubin1.bounds.size.width){ CollisionRubies = NO; NSLog(@“碰撞上面”); }
否则{ //如果我们到了这里,那两个地方必然会发生碰撞 CollisionRubies = YES; NSLog(@“碰撞检测到”); }
答案 0 :(得分:1)
正如旁注,如果您使用了其他代码,如果您的代码可能会更高效,特别是如果您经常调用CollisionDetect,我认为您是。这样,如果早期的if语句之一为真,则可以忽略其余的。
答案 1 :(得分:0)
在CollisionDetect
方法中,您要在各个点设置CollisionRubies = NO
,但无论刚刚发生任何测试,您都会在最后设置CollisionRubies = YES
。您可能已经将方法设为一行
-(void)CollisionDetect{
CollisionRubies = YES;
}
此外,您计算碰撞时似乎存在问题(在计算上方或下方时,宽度与y混合)
我还建议你阅读Objective-C编码风格指南。例如,对变量和方法名称使用小写的第一个字母。例如,collisionRubies
和-(void)collisionDetect
如果以标准方式编写,这有助于其他人更快地阅读您的代码。
答案 2 :(得分:0)
而不是四个检查四个方向碰撞的函数,为什么不使用
if(CGRectIntersectsRect(ruby1.frame,ruby2.frame)
{
//your code
}
这个,当然,只有当你不需要知道一个物体接近另一个物体的方向时。