开发者

Is there some way to get certain UIImageView properties?

I have just started messing around with xCode, and am trying to give myself a basic introduction to it by t开发者_Go百科weaking some premade projects.

Currently there is a project that I am working on that has a ball just bouncing around the screen and there is a gameLoop that is setup with the following:

This part is defined in the .m:

 - (void)gameLoop {
    if (gameState = kGameStateRunning) {
        ball.center = CGPointMake(ball.center.x + ballVelocity.x, ball.center.y + ballVelocity.y);
        if(ball.center.x > self.view.bounds.size.width || ball.center.x < 0) {
            ballVelocity.x = -ballVelocity.x;
        }

        if(ball.center.y > self.view.bounds.size.height || ball.center.y < 0) {
            ballVelocity.y = -ballVelocity.y;
        }
    }
    else {
        if(tapToBegin.hidden) {
             tapToBegin.hidden = NO;
        }
    }
}

What I would like to know, is as opposed to having my UIImageView *ball only bounce off of the walls if it reaches the center, is there a way to make it so that once it hits the edge of ball then it bounces.

First time poster here so I hope I was descriptive enough.

EDIT: I would also like to try doing this so that should I change the radius of the UIImageView without having to adjust constants. Hopefully that is possible, just trying to find out for sheer curiosity. Thanks again guys!


The simple answer is to include an offset for the radius of the ball (in pixels), like so:

 - (void)gameLoop {
    ball.center = CGPointMake(ball.center.x + ballVelocity.x, ball.center.y + ballVelocity.y);
    int radius = 10; // radius in pixels
    if (gameState = kGameStateRunning) {
        if(ball.center.x + radius > self.view.bounds.size.width || ball.center.x - radius < 0) {
            ballVelocity.x = -ballVelocity.x;
        }
        if(ball.center.y  + radius > self.view.bounds.size.height || ball.center.y - radius < 0) {
            ballVelocity.y = -ballVelocity.y;
        }
    }
    ...

There are lots of tutorials on collisions and more advanced physics on pages such as: NeHe Productions

Hope this helps a bit.

EDIT: In response to your edit about dynamic radius size, try something like (untested):

UIImageView *ball;  
CGFloat radius = ball.frame.size.width; // alternatively ball.frame.size.height
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜