2012-07-06 190 views
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 
+1

這個問題似乎不完整; s – OthmanT 2012-07-06 15:09:06

+1

如果您正在製作遊戲,我建議使用Cocos2d。 – Shailesh 2012-07-06 17:08:24

回答

0

看來,你給我們的唯一方法是不斷調用onTimer。你爲什麼不設置一個像0.01那樣小的加速度常數。然後,創建一個名爲加速,並把在代碼中的作用方法:

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

呼叫加速後[自checkCollision]

然後使用加速度常數,直到獲得所需的結果。

如果您希望它是一個只增加每秒的步進功能,您將需要使用NSTimer iVar,這也相當容易。只是使用一個第二時間間隔這init函數

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 

然後調用@selector(加速)作爲目標選擇器實例化。