当谈到Objective-C时,我是一个新手,所以请让你的答案尽可能简单。使NSTimer的时间间隔越来越短
这里是在Xcode 4.5中制作的Pong游戏的m文件。在方法viewDidLoad是一个计时器。我的问题是改变时间间隔(现在float gap = 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;
}
当你试图做你说你认为你需要做的事情时发生了什么? – 2013-02-10 22:30:44
你的意思是在别的地方添加一个新的计时器? – user2059738 2013-02-10 22:34:04
你真的想让计时器本身加快吗?当然你想增加球的速度。你可以在不改变定时器周期的情况下做到这一点例如,乘以xgain和ygain一些不同的因素。 – occulus 2013-02-10 22:43:41