Contents |
|
Holding Buttons | Responsive Layouts |
When creating games, images or sprites, are added to the movie to represent elements in the games such as characters, vehicles or explosions. The position of the sprite is adjusted many times a second from inside a loop depending on some game logic. Every movie has a built in game loop, called 'On Update'. It is called every time the animation updates, and it can be found in the movie properties.
Create a new movie and add an image, also called a sprite. We've used a beach ball.
Import |
Click to save this image to your computer.
in the properties of the first frame of the beach ball, turn off the timeline and set its 'Script ID' to Ball. Turning off the timeline allows the ball to be controlled using Javascript.
We're going to make the beach ball bounce around the movie. So we need to create some velocity variables in the 'General' script event. Any initial values will do.
In the 'On Update' event, we can make the ball move by adding the velocity values to the X and Y values of the Ball.
Press play to test the movie. The ball should fly straight off the movie.
Play |
The X and Y properties of the ball are its center point. By checking these values against the width and height of the movie, we can check to see if the ball should bounce. To bounce the ball, simply reverse the velocity.
Press play to test the movie again. The ball should now bounce around.
Export the movie to HTML and notice that the Ball moves at a slightly different speed to the player. This is because different web browsers and different devices run at different speeds.
To make the ball move at the same speed everywhere, we need to calculate the difference in time between updates. The 'On Update' event includes two helpful variable that do just that. TimeChange : The number of frames that have passed since the last update. SecondsChange : The number of seconds that have passed since the last update. These values are independent of whether the movie has been stopped or not. They are simply a way to calculate the time since the last update. To make animation always run at the same speed, each change in position, angle or opacity needs to be multiplied by the change in time or seconds.
Test the movie in the player and HTML. The ball should move at exactly the same speed in both.
Holding Buttons | Responsive Layouts |