How can I smoothly scale the layer in IOS to keep an object on screen?
I'm working on a game where a ball is hit, and can leave the visible area of the layer. I'm setting the position of the ball each frame, as I get feedback from Box2D about its location. When the ball nears the edge of the screen, I want 开发者_JAVA百科to zoom out just the right amount to keep the ball visible. Can someone give me a hand with the logic to do this? Thanks.
Let's have the size of the screen size of 480 (pixels), with the original diameter of the ball being 10 pixels.
Original size of ball = bOriginal = 10
Distance represented by screen = s = 480
Distance ball has travelled = x
Diameter of the ball = b = bOriginal
You would have a flag for when the ball reaches a certain distance from the edge of the screen. After which you have your velocity, which you already know; this can also be thought of as the rate the ball is moving towards the edge of the screen, therefore the rate at which the screen must expand with respect to the size of the ball to ensure the total distance the ball has travelled is included within the size of the screen.
If x >= 475
ratio of screen size to distance = r = 480 / (x+5)
b = bOriginal * r
end
This will demonstrate a "zoom-out" in that the ball will get continually smaller to ensure that the total distance travelled fits into the size of the screen.
精彩评论