2011-09-06 8 views
0

기본적으로 사용자가 화면에서 볼을 탭할 수있는 아이폰 앱을 디자인하고 있으며 앱이 사용자가 공을 몇 번 터치했는지 계산하는 경우 문제는, 앱은 시작 버튼을 탭하기 전부터 도청 횟수를 계산합니다. 시작 버튼을 누르기 전에 도청이 점수에 반영되지 않도록하려면 어떻게해야합니까? 여기 xcode에서 타이머가 일시 중지되었을 때 탭 수를 멈추는 방법

내 코드입니다 :

.H :

@interface FlipsideViewController : UIViewController { 
    IBOutlet UIImageView *Ball1; 
    NSTimer *mainTimer1; 
    IBOutlet UIButton *startButton1; 
    IBOutlet UILabel *currentNumber1; 
    IBOutlet UIButton *pause1; 
    UIAlertView *alertStart1; 
} 

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate; 
@property (nonatomic, retain) UIImageView *Ball1; 
@property (nonatomic, retain) NSTimer *mainTimer1; 
@property (nonatomic, retain) UIButton *startButton1; 

- (IBAction)done:(id)sender; 
- (IBAction)startMove; 
- (void)moveBall; 
- (BOOL)Intersecting:(CGPoint)loctouch:(UIImageView *)enemyimg; 
- (IBAction)pause1:(id)sender; 
- (void)alertStart1; 

@end 

하는 .m 다음 있는지 확인하기 위해 검사를 넣어

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

방법에

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

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:self.view]; 
// if user touched ball, stop timer 
if ([self Intersecting:location :Ball1]) { 
    [mainTimer1 invalidate]; 
    mainTimer1 = nil; 
    startButton1.hidden = NO; 
    number++; 
    [currentNumber1 setText:[NSString stringWithFormat:@"%d", number]]; 
    } 
} 

-(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; 

} 

-(void)moveBall { 
CGFloat xdist = fabs(Destination.x-Ball1.center.x); 
CGFloat ydist = fabs(Destination.y-Ball1.center.y); 

if ((xdist<5)&&(ydist<5)) { 
    Destination = CGPointMake(arc4random() % 320, arc4random() % 480); 
    xamt = ((Destination.x - Ball1.center.x)/speed); 
    yamt = ((Destination.y - Ball1.center.y)/speed); 
} else { 
    Ball1.center = CGPointMake(Ball1.center.x+xamt, Ball1.center.y+yamt); 
} 
} 

-(IBAction)startMove { 
startButton1.hidden = YES; 

Destination = CGPointMake(arc4random() % 320, arc4random() % 480); 
xamt = ((Destination.x - Ball1.center.x)/speed); 
yamt = ((Destination.y - Ball1.center.y)/speed); 

mainTimer1 = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(moveBall) userInfo:nil repeats: YES]; 
} 

답변

1

게임이 시작되었습니다. 응용 프로그램이 거 아이폰 OS 3.2 이상이 될 경우 사용자를 눌러 시작 버튼이 이미지 뷰에 인식기를 추가 할 때

if (startButton1.hidden) { 
    // game has begun, add points 
} else { 
    // game hasn't yet begun, don't add points 
} 
+0

원하는 방식입니다. 그러나 상태 변수 (BOOL shouldCountPoints)를 사용하여 점을 추가해야하는지 여부를 확인하는 것이 좋습니다. 버튼이 숨겨져 있지 않지만 점을 카운트하려는 경우가있을 수 있습니다. – bitgarden

0

, UITapGestureRecognizer 대신 touchesBegan:withEvent: 다음 사용합니다. UIGestureRecognizer를 사용하려면 userInteractionEnabled를 UIImageView의 YES로 설정해야합니다.

관련 문제