一直在阅读一本 xCode 书籍,试图学习如何为 iOS 编程。遇到了一个问题..除了我的应用程序中的桨碰撞之外,我几乎所有的东西都在工作。我做了一个简单的弹球游戏,当球击中积木时,积木会破裂,球会回来,我遇到的问题是当球下来时它无法识别碰撞并直接穿过桨。
问题是 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);
}
}