Controlling Frames with JavaScript Creating a Game Loop

Holding Buttons

This tutorial continues from the previous tutorial, controlling frames with JavaScript. It demonstrates how to move the circle whilst holding down the buttons. Create the controlling frames with JavaScript tutorial.

Holding Buttons 1

We will need to track the state of each button, add this script to 'General' in the movie properties.

var LeftButtonPressed=false,RightButtonPressed=false;

Turn on 'Use Release' for both buttons. This enables a new event called 'On Release'. 'On Click' is called when the button is pressed. 'On Release' is called when the button is released.

Holding Buttons 3

Holding Buttons 4

Delete the current script in the 'On Click' events. Add the following for the 'On Click' and 'On Release' events of the buttons.





The state of both buttons is now being tracked. Add this script to the 'On Update' event in the movie properties. The 'On Update' event is called every time the movie updates, which can be many times a second.


When the left button, for example, is held down, the variable LeftButtonPressed is set to true. When this value is true, the X value of the circle will decrease every time 'On Update' is called.

Play Play

Holding Buttons 10

Controlling Frames with JavaScript Creating a Game Loop