Monkey Coder – X Bouncing Ball
1. Notice:
———-
I have created on Class yourgame Extends App a new variable:
The code:
Field bXSpeed:Float = 3.0 ‘ X Speed (add 3.0 pixel every 1/60 sec)
2. Notice:
———-
I have created the Method BallUpdate:Int() to change the ball position. The ball has to stay inside the canvas. I verify the x position of the ball (bx variable) and I need to invert speed to create the bounce effect
The code:
‘ Update Ball START
Method BallUpdate:Int()
bX += bXSpeed ‘Add the X speed of the ball to its X position every 1/60 sec (frame rate: 60 fps)
If bX < 5.0 Then ' To control left bounce
bX = 5.0 'Set the X position back to 5.0
bXSpeed *= -1 'Inverse the balls X speed
Endif
If bX > 635.0 Then ‘ To control right bounce
bX = 635.0 ‘Set the X position back to 635.0
bXSpeed *= -1 ‘Inverse the balls X speed
Endif
Return True
End
‘ Update Ball END
3. Notice:
———-
Very very Important: I put the BallUpdate() inside Method OnUpdate:Int() to execute BallUpdate().
Without this code the ball does not change its position!!!
The code:
‘ OnUpdate START
Method OnUpdate:Int()
BallUpdate()
Return True
End
‘ OnUpdate END
1. Notice: ---------- I have created on Class yourgame Extends App a new variable: The code: Field bXSpeed:Float = 3.0 ' X Speed (add 3.0 pixel every 1/60 sec) 2. Notice: ---------- I have created the Method BallUpdate:Int() to change the ball position. The ball has to stay inside the canvas. I verify the x position of the ball (bx variable) and I need to invert speed to create the bounce effect The code: ' Update Ball START Method BallUpdate:Int() bX += bXSpeed 'Add the X speed of the ball to its X position every 1/60 sec (frame rate: 60 fps) If bX < 5.0 Then ' To control left bounce bX = 5.0 'Set the X position back to 5.0 bXSpeed *= -1 'Inverse the balls X speed Endif If bX > 635.0 Then ' To control right bounce bX = 635.0 'Set the X position back to 635.0 bXSpeed *= -1 'Inverse the balls X speed Endif Return True End ' Update Ball END 3. Notice: ---------- Very very Important: I put the BallUpdate() inside Method OnUpdate:Int() to execute BallUpdate(). Without this code the ball does not change its position!!! The code: ' OnUpdate START Method OnUpdate:Int() BallUpdate() Return True End ' OnUpdate END