gpt4 book ai didi

iphone - 当计时器在 xcode 中暂停时如何停止点击计数

转载 作者:行者123 更新时间:2023-11-29 04:59:15 30 4
gpt4 key购买 nike

我正在设计一个iPhone应用程序,它基本上让用户在屏幕上点击一个球,并且该应用程序计算用户点击球的次数,问题是,该应用程序从点击开始按钮之前开始计算点击次数。在点击开始按钮之前,如何阻止点击计入您的分数?

这是我的代码:

.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 {

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];
}

最佳答案

在方法中

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

检查游戏是否已经开始,例如

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

关于iphone - 当计时器在 xcode 中暂停时如何停止点击计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326570/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com