Tag Archives: Flash

IDAT102 – Flash Game – Timer and play button 0004-0006

Next I added a timer using the code:

var time = 120;
var count:Number = time;
var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
countTxt.text = String((count)-myTimer.currentCount);
}

This counts down from 120 displaying the current time remaining in countTxt.#

I then created a separate frame for a splash screen at the start of the game. To prevent the game simply playing through the frames as it would an animation, I started the actionscript with stop();. After this I added a button which becomes lighter on mouseover and onclick runs nextFrame();.

I experienced an error when transitioning between frames which I eventually determined to be because of the event listeners on the first frame persisting. To fix this I used:

btnPlay.removeEventListener(MouseEvent.MOUSE_OVER, hoverON);
btnPlay.removeEventListener(MouseEvent.MOUSE_OUT, hoverOFF);

IDAT102 – Flash Game – Movement 0001-0003

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.