Keith Peters's <<foundation ActionScript 3.0 animation-making things move>> a book on the stage boundary bounce, the main way is when the object goes out of bounds, Simply displacement the target from the axis perpendicular to the boundary and adjust it to the close edge, 1.
The author also explains in detail the problems of this approach. Because it is impossible for the target to reach the position of the dashed ball in the picture, in fact, the coordinate of the target and the boundary should be adjusted, so how much should it be shifted? Take a look at Figure 2. We pull the ball back along its path, and the green ball is actually the right place to touch the border.
As the author has said, this small flaw in the actual application of the basic can be ignored, only if the target speed is faster, the movement of each frame is larger or the incidence angle is small, it is possible to have a visual impact. The author also tells us that this problem can be solved with trigonometric functions. Take a look at the known quantity: Figure 3.
We know the radius of the ball, the current position (the dashed ball) and the angle (calculated with the Atan function speed), to calculate the value of yd. let's look at my solution (math junior level, please forgive me, completely later my own Khan college).
Angle = Atan (Y-axis speed, x-axis speed)
To figure out the value of the xd side, xd = Ball radius-(stage width-ball.x)
Then, hypotenuse = xd/cosθ
Finally, yd = hypotenuse * sinθθ, then subtract this value from the y-axis.
Finally, attach the actual ACTIONSCRIPT3 code:
1 varLeft Number= 0;2 varTop Number= 0;3 varRight Number=Stage.stagewidth;4 varBottom Number=Stage.stageheight;5 6 varAngle Number=math.atan2 (Ball.vy, BALL.VX);7 varDist Number= 0;8 if(Ball.x-ball.radius <Left )9 {TenDist = (Ball.x-ball.radius)/Math.Cos (angle); OneBALL.Y-= Math.sin (angle) *Dist; Aball.x =Ball.radius; -BALL.VX *=Bounce; - } the Else if(ball.x + Ball.radius >Right ) - { -Dist = (Ball.radius-(right-ball.x))/Math.Cos (angle); -BALL.Y-= Math.sin (angle) *Dist; +Ball.x = right-Ball.radius; -BALL.VX *=Bounce; + } A at if(Ball.y-ball.radius <top) - { -Dist = (Ball.y-ball.radius)/Math.sin (angle); -Ball.x-= Math.Cos (angle) *Dist; -BALL.Y =Ball.radius; -Ball.vy *=Bounce; in } - Else if(Ball.y + Ball.radius >bottom) to { +Dist = (Ball.radius-(BOTTOM-BALL.Y))/Math.sin (angle); -Ball.x-= Math.Cos (angle) *Dist; theBall.y = bottom-Ball.radius; *Ball.vy *=Bounce; $}
View Code
ActionScript3 Learning record-boundary bounce parallel Axis displacement