0

大家好,我正在制作一个应用程序,你是一个蓝球,必须在不让红球碰到你的情况下移动,我想知道我该怎么做才能让速度每秒增加一次,我有这样的事情:

- (void)viewDidLoad {
[super viewDidLoad];

//(X Speed, Y Speed)
pos = CGPointMake(13.0,8.0);
pos2 = CGPointMake(10.0,9.0);

}


- (IBAction)restart:(id)sender {

     [restartbutton setHidden:YES];
     [menubutton setHidden:YES];
     randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

- (IBAction)start:(id)sender {    
     [startbutton setHidden:YES];
     [restartbutton setHidden:YES];
     [menubutton setHidden:YES];
     randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

- (IBAction)menu:(id)sender {

     [self.navigationController popViewControllerAnimated:YES];

}

- (void)onTimer {

    [self checkCollision];

    enemy.center = CGPointMake(enemy.center.x+pos.x, enemy.center.y+pos.y);
    enemy2.center = CGPointMake(enemy2.center.x+pos2.x, enemy2.center.y+pos2.y);

    if (enemy.center.x > 320 || enemy.center.x < 0) 
        pos.x = -pos.x;
    if (enemy.center.y > 480 || enemy.center.y < 0) 
        pos.y = -pos.y;
    if (enemy2.center.x > 320 || enemy2.center.x < 0) 
        pos2.x = -pos2.x;
    if (enemy2.center.y > 480 || enemy2.center.y < 0) 
        pos2.y = -pos2.y;

}

- (void)checkCollision {

    if (CGRectIntersectsRect(player.frame, enemy.frame)) {

        [randomMain invalidate];
        [startbutton setHidden:YES];
        [restartbutton setHidden:NO];
        [menubutton setHidden:NO];

        CGRect frame = [player frame];
        frame.origin.x = 135.0f;
        frame.origin.y = 205.0f;
        [player setFrame:frame];

        CGRect frame2 = [enemy frame];
        frame2.origin.x = 135.0f;
        frame2.origin.y = 20.0f;
        [enemy setFrame:frame2];

        CGRect frame3 = [enemy2 frame];
        frame3.origin.x = 135.0f;
        frame3.origin.y = 390.0f;
        [enemy2 setFrame:frame3];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lost!" 
                                                        message:[NSString stringWithFormat:@"Better Luck Next Time"]     
                                                       delegate:nil 
                                              cancelButtonTitle:@"Cancel" 
                                              otherButtonTitles:nil];
    [alert show];

    }
}

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

    UITouch *myTouch = [[event allTouches] anyObject];
    player.center = [myTouch locationInView:self.view];

}

 - (void)viewDidUnload
 {
     [super viewDidUnload];
     // Release any retained subviews of the main view.
 }


@end
4

1 回答 1

0

您给我们的唯一不断被调用的方法似乎是 onTimer。你为什么不设置一个非常小的加速度常数,比如 0.01。然后,创建一个名为加速的方法并放入代码中,效果如下:

pos = CGPointMake(pos.x + ACC_CONSTANT, pos.y + ACC_CONSTANT);
pos2 = CGPointMake(pos2.x + ACC_CONSTANT, pos.y + ACC_CONSTANT);

[self checkCollision]后调用加速;

然后使用 Acceleration 常数进行调整,直到获得所需的结果。

如果您希望它是一个仅每秒增加的阶跃函数,您将需要使用 NSTimer iVar,这也很容易。只需使用此 init 函数以一秒的时间间隔实例化它

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

然后调用@selector(accelerate) 作为目标选择器。

于 2012-07-06T17:13:53.760 回答