提高速度

时间:2012-07-06 15:04:38

标签: objective-c ios xcode

嘿大家我正在制作一个应用程序,你是一个蓝色的球,必须移动而不让红球碰到你,我想知道我能做什么,以便每一秒速度增加我有这样的事情:< / p>

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

1 个答案:

答案 0 :(得分:0)

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

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

[self checkCollision]后调用加速;

然后使用加速度常数,直到获得所需的结果。

如果你想让它成为一个步长函数,它只会每秒增加,你需要使用NSTimer iVar,这也很容易。只需使用此初始化函数

以一秒的时间间隔对其进行实例化
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

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