2011-02-18 83 views
0

我想知道爲什麼我的代碼不工作。我希望我的球(UIImageView)從塊(也是UIImageView)反彈。我的球將x和y的運動切換爲負值而不是一個。哪裏不對???請幫助...這是我的代碼如何檢測UIImageView碰撞

-(void)animateBall:(NSTimer *)theTimer { 

    collisionCount ++; 
    bouncyBall.center = CGPointMake(bouncyBall.center.x + ballMovement.x, bouncyBall.center.y + ballMovement.y); 

    if (CGRectIntersectsRect(bouncyBall.frame, wallOne.frame)) { 
     if (collisionCount >= 5) { 
      [self processCollision:wallOne]; 
      collisionCount = 0; 
     } 
    } 

    if (bouncyBall.center.x > 313 || bouncyBall.center.x < 8) { 
     ballMovement.x = -ballMovement.x; 
    } 
    if (bouncyBall.center.y > 453 || bouncyBall.center.y < 8) { 
     ballMovement.y = -ballMovement.y; 
    } 
} 

-(void)processCollision:(UIImageView *)wall { 

    if (ballMovement.x > 0 && wall.frame.origin.x - bouncyBall.center.x <= 4) { 
     ballMovement.x = -ballMovement.x; 
    } 
    else if (ballMovement.x < 0 && bouncyBall.center.x - (wall.frame.origin.x + wall.frame.size.width) <= 4) { 
     ballMovement.x = -ballMovement.x; 
    } 

    if (ballMovement.y > 0 && wall.frame.origin.y - bouncyBall.center.y <= 4) { 
     ballMovement.y = -ballMovement.y; 
    } 
    else if (ballMovement.y < 0 && bouncyBall.center.y - (wall.frame.origin.y + wall.frame.size.height) <= 4) { 
     ballMovement.y = -ballMovement.y; 
    } 
} 

回答

0

即使當球撞到邊緣的中心時,它會發生嗎?無論如何,你不需要一次處理一個維度嗎?第三個if不能是else if