Helpful Code

Keeping Score

You will need a variable to hold the score.

First you must declare the variable. In this case the variable is called score.

var score

You may wish to initialize the variable as you declare it.

var score = 1000

You will need a textbox in which you display the score. In this example the textbox is called tScore. When creating your textbox be sure that it is large enough to display your score, format your text, name the instance and set the anti-alias to Display Device Fonts.

To display the score you will need code similar to this:


To increase or decrease the score you will need the following code. In this example the variable score is used and the variable will increase or decrease by 20. This code will be triggered by the event of your choice. It will appear most often within an if statement or some other decision structure.



You will also need to update the score by adding the code after the score is changed.


Limiting Stage Boundaries

To keep an object within  stage boundaries you must use some simple math to create a new function. In this example the function is called limitStageBorder, the object that is being kept within the boundaries is called object.

function limitStageBorder(object:MovieClip) {

   // Variables are declared that store half the objects width and height.
var objectHalfWidth:uint=object.width/2;
var objectHalfHeight:uint=object.height/2;

// If the object moves beyond the stage width, the object is pushed back by adding or subtracting the half the width of the object.
if (object.x+objectHalfWidth>stage.stageWidth) {
else if (object.x –  objectHalfWidth <0) {

// If the object moves beyond the stage height, the object is pushed back by adding or subtracting half of the height of the object.
if (object.y-objectHalfHeight<0) {
else if (object.y + objectHalfHeight > stage.stageHeight) {

You will need to add the limitStageBorder() function to enable the limiting of the stage boundaries. Place the line of code inside the function that controls the objects movement function.