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);
}
}