0

我想知道为什么我的代码不起作用。我希望我的球(UIImageView)从一个块(也是一个 UIImageView)反弹。我的球将 x 和 y 运动都切换为负数,而不仅仅是一个。怎么了???请帮助...这是我的代码

-(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;
    }
}
4

1 回答 1

0

即使球击中边缘中心也会发生这种情况吗?无论如何,您不只需要一次处理一个维度吗?不能是第三个ifelse if

于 2011-02-18T21:12:04.153 回答