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
這個問題似乎不完整; s – OthmanT 2012-07-06 15:09:06
如果您正在製作遊戲,我建議使用Cocos2d。 – Shailesh 2012-07-06 17:08:24