2013-06-05 78 views
0

正在瀏覽試着學習如何編程iOS的xCode書。進入一個問題..我得到的主要工作除了在我的應用程序的槳衝突。我做了一個簡單的針球比賽,當球擊中方塊時,方塊破裂,球會回落,我遇到的問題是當球落下時,它不能識別碰撞並穿過球拍。反向碰撞

問題是checkCollisionWithBlocks

不知道我會究竟如何改變球的方向,當它擊中,因此不會通過槳去:,

-(void)checkCollisionWithScreenEdges{ 
    if(ballRect.origin.x <= 0){ 
     ballVelocity.x = abs(ballVelocity.x); 
    } 
    if(ballRect.origin.x >= VIEW_WIDTH - BALL_SIZE){ 
     ballVelocity.x = -1 * abs(ballVelocity.x); 
    } 
    if(ballRect.origin.y <= 0){ 
     ballVelocity.y = abs(ballVelocity.y); 
    } 
    if(ballRect.origin.y >= VIEW_HEIGHT - BALL_SIZE){ 
     ballRect.origin.x = 180.0; 
     ballRect.origin.y = 220.0; 
     ballVelocity.y = -1*abs(ballVelocity.y); 
    } 
} 

-(void)checkCollisionWithBlocks{ 
    for(mmBlockView* blockView in blocks){ 
     if(CGRectIntersectsRect(blockView.frame,ballRect)){ 
      ballVelocity.y = -ballVelocity.y; 
      [blocks removeObject:blockView]; 
      [blockView removeFromSuperview]; 
      break; 
     } 
    } 
} 

-(void)checkCollisionWithPaddle{ 
    if(ballRect.origin.x <= 0){ 
     ballVelocity.x = abs(ballVelocity.x); 
    } 
    if(ballRect.origin.x >= VIEW_WIDTH - BALL_SIZE){ 
     ballVelocity.x = -1 * abs(ballVelocity.x); 
    } 
    if(ballRect.origin.y <= 0){ 
     ballVelocity.y = abs(ballVelocity.y); 
    } 
    if(ballRect.origin.y >= VIEW_HEIGHT - BALL_SIZE){ 
     ballRect.origin.x = 180.0; 
     ballRect.origin.y = 220.0; 
     ballVelocity.y = -1*abs(ballVelocity.y); 
    } 

} 

回答

0

checkCollisionWithPaddle是不是儘管名稱,檢查與槳的碰撞,它檢查球是否離開屏幕。

要檢查它是否與槳碰撞,你可能會做這樣的事情:

if(CGRectIntersectsRect(paddleView.frame, ballRect)) 

如果有幫助,你可能會認爲槳的作爲像塊,但它可以移動(並且沒有按當你擊中它時不會消失:-)