To start my flash game I decided to use a simple but effective movement system. The system starts by declaring variables for up, down, left and right which are either true or false. Three event listeners are used to run three functions. The first is masterLoop which runs on the enter frame event; this continually loops at the framerate which has been set, in this case 30. The other functions and corresponding event listener is atKeyDown and atKeyUp which use KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP. Within both of these functions if statements detect if matching keycodes have been pressed and sets the variables to either true or false. The masterloop contains if statements so that if leftIsPressed is true then the new X position of the player becomes the old X position minus the value of the speed variable.
I then added an additional if statement for each to prevent the player being able to exit the stage.
eg:
if (rightIsPressed) {
if(carP1.x < (800 - ballDiameter)){
carP1.x = carP1.x + speed;}
}
The ballDiameter variable offsets the distance preventing any of the player from overlapping the stage.
In later versions the above code is shortened to.
if (rightIsPressed)&&(carP1.x < (800 - ballDiameter)) {
carP1.x = carP1.x + speed;
}
The numbers in the heading of each post such as “0001-0003” indicate the version numbers the post covers.