2012-02-20 82 views
0

我設計了一個應用程序,用戶可以在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; 
} 

回答

2

我建議把觸摸管理回球本身(見this answer)。所以無論何時接到一個接觸,你都可以確定它是在點擊球。所有你需要做的就是檢查球是否在死區,因此忽略觸摸。