我是 Objective-C 的新手,所以请让你的答案尽可能简单。
这是在 Xcode 4.5 中制作的 Pong 游戏的 m 文件。在方法 viewDidLoad 中是一个计时器。我的问题是改变间隔(现在浮动间隙 = 0.05),所以球跑得越来越快。我想我必须在其他地方做一个新的计时器,而不是将重复设置为 YES。但我不知道把它放在哪里以及如何处理浮动间隙。
希望你明白我的意思。
PongOne.m:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
paddle.center = CGPointMake(location.x, paddle.center.y);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
-(void)CPU {
ball.center = CGPointMake(ball.center.x + xgain, ball.center.y + ygain);
if (ball.center.x < 15)
xgain = abs(xgain);
if (ball.center.y < 15)
ygain = abs(ygain);
gap = gap + 0.01;
if (ball.center.x > 305)
xgain = -abs(xgain);
if (ball.center.y > 445){
score++;
if (score <= 2){
ball.center = CGPointMake(100, 100);
label1.text = [NSString stringWithFormat:@"%d", score];
} else {
[timer invalidate];
timer = nil;
[self performSegueWithIdentifier:@"byta1" sender:self];
}
}
if (CGRectIntersectsRect(ball.frame, paddle.frame))
ygain = -abs(ygain);
if (CGRectIntersectsRect(ball.frame, paddle2.frame))
ygain = abs(ygain);
paddle2.center = CGPointMake(ball.center.x, paddle2.center.y);
}
- (void)viewDidLoad {
[super viewDidLoad];
timer = [NSTimer scheduledTimerWithTimeInterval:gap target: self selector:@selector(CPU) userInfo:nil repeats:YES];
xgain = 10;
ygain = 10;
}