游戏不会重置iOS

时间:2014-06-14 11:43:30

标签: ios

您好我正处于Xcode第一次“碰撞”游戏的早期阶段。

我写了这段代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    pos = CGPointMake(5.0,4.0);
}

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

-(void)onTimer {

    [self checkCollision];

    enemy.center = CGPointMake(enemy.center.x+pos.x,enemy.center.y+pos.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;
}

-(void)checkCollision {
    if( CGRectIntersectsRect(player.frame,enemy.frame))
    {
        [randomMain invalidate];
        [startbutton setHidden:NO];

        pos = CGPointMake(0.0,0.0);

        CGRect frame = [player frame];
        frame.origin.x = 137.0f;
        frame.origin.y = 326.0;
        [player setFrame:frame];

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

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lost!" message:[NSString stringWithFormat:@"You were hit! Try Again!"] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
        [alert show];
    }
}

虽然当我在模拟器中运行并且“敌人”和“玩家”发生碰撞时,用户按下“再试一次”,游戏就不会重置。任何人都可以告诉我缺少什么代码?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

  

- (void)onTimer {

if (![self checkCollision]){

    enemy.center = CGPointMake(enemy.center.x+pos.x,enemy.center.y+pos.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; 
   }
     

}

     

- (BOOL)checkCollision {

if( CGRectIntersectsRect(player.frame,enemy.frame))
{
    [randomMain invalidate];
    [startbutton setHidden:NO];

    CGRect frame = [player frame];
    frame.origin.x = 137.0f;
    frame.origin.y = 326.0;
    [player setFrame:frame];

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

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lost!" message:[NSString stringWithFormat:@"You were hit! Try Again!"] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
    [alert show];

    return YES;
    }

   return NO; 
     

}