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