如何防止屏幕上某个区域的点击

时间:2012-02-20 01:23:14

标签: iphone ios game-physics

我设计了一个应用程序,用户可以在30秒内尽可能多地轻击球,称为iTapping。在游戏中,用户能够点击屏幕上的任何地方以点击球。我想到了编辑应用程序,以便用户无法点击球的“死点”。例如,如果球位于右上角(假设在大约100平方英尺的区域内),并且用户轻击球没有任何反应。我该如何编码呢?如果不够清楚,请告诉我。

这是.m文件:

CGPoint Destination;
CGFloat xamt, yamt; 
CGFloat speed21 = 40;
CGFloat xMin21 = 24;
CGFloat xMax21 = 297;
CGFloat yMin21 = 74;
CGFloat yMax21 = 454;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view]; 
if ([self Intersecting:location :Ball]) {
    number21++;

    int xRange = xMax21 - xMin21;
    int yRange = yMax21 - yMin21;

    int xRand = (arc4random() % xRange) + xMin21;
    int yRand = (arc4random() % yRange) + yMin21;

    Destination = CGPointMake(xRand, yRand);
    xamt = ((Destination.x - Ball.center.x) / speed21);
    yamt = ((Destination.y - Ball.center.y) / speed21);


    if (number21 == 65) { 
        [timer invalidate];
      }
   }
}   

-(BOOL)Intersecting:(CGPoint)loctouch:(UIImageView *)enemyimg {
    CGFloat x1 = loctouch.x;
    CGFloat y1 = loctouch.y;

    CGFloat x2 = enemyimg.frame.origin.x;
    CGFloat y2 = enemyimg.frame.origin.y;
    CGFloat w2 = enemyimg.frame.size.width;
    CGFloat h2 = enemyimg.frame.size.height;

    if ((x1>x2)&&(x1<x2+w2)&&(y1>y2)&&(y1<y2+h2))
        return YES;
    else
        return NO;
}

1 个答案:

答案 0 :(得分:2)

我建议将触摸管理重新放回球本身(参见this answer)。因此,无论何时收到触摸,您都可以确定它点击了球。您需要做的就是检查球当前是否处于死区,从而忽略触摸。