Actionscript How To Guide

Digital Arts and Sciences

Stephen Lewis

Trinity School

 

Here are some examples to help you with Actionscript

Make an object "draggable" by the user:

myball.onPress=startDrag;
myball.onRelease=stopDrag;
myball.onReleaseOutside=stopDrag;

Define a function like onPress, onEnterFrame for an object such as myBall:

myBall.onEnterFrame = function () {

    myball._x += 5;   // just an example

    // anything else you want can go here

}

 

Create a scoring display box:

 

create a variable called "score" with a starting value of 0

var score = 0;

make a text box and "select" it by clicking on it

in the properties window where it says "var", enter "score"

whenever you make score a new value (score++), the text box will automatically show the new value

 

Lock an object like a paddle to the mouse movement:

 

myPaddle.onEnterFrame = function() {

    myPaddle._y = _ymouse;

}

 

Define a "global" variable:

 

var speed = 5;

var isRunning = false;

var authorName = "Henry Thoreau";

 

Define a "local" variable inside an object like myBall:

 

myBall.xSpeed = 10;

myBall.ySpeed = 4;

 

Use the local variable to make something move:

 

myBall._x += myball.xSpeed;

 

Make something accelerate, given an acceleration you define as, for example

 

 myBall.accel = 3;  // define the acceleration

 

myBall.xSpeed += myBall.accel; // accelerate the speed

 

myBall._x += myBall.xSpeed;  // move the ball (inside an onEnterFrame)

 

Make something go the opposite direction, two different ways to do it:

 

myBall.xSpeed *= -1;

 

myBall.xSpeed = myBall.xSpeed * -1;

 

Use an if clause:

 

if (myBall._x > 400) {

    myBall.xSpeed *= -1;

}

 

Use an if clause inside another function, for example onEnterFrame:

 

myBall.onEnterFrame = function () {

    if (isRunning) {
        myBall._x += myBall.xSpeed;

    }

    if (myBall._x > 400) {

        myBall.xSpeed *= -1;

    }

}

 

Slow something down, two examples, one for an object called myBall which keeps getting slower every "tick" of the clock, and another which has a button called slowButton, which slows myBall every time it's clicked.  Note that you can either multiply by a fraction near 1 or subtract a value:

 

myBall.onEnterFrame = function() {

    myBall.xSpeed *= .95;

}

 

 

slowButton.onPress = function() {

    myBall.xSpeed -= 0.5;

}

 

Gravity, this example uses a "gravitational constant" of 1.5, but you can choose another value if you think it feels more "realistic".  You can change either myBall._y or myBall.ySpeed, depending on your code.

 

myBall.onEnterFrame = function() {

    myBall.ySpeed += 1.5;

}

 

 

Use key presses to determine whether a given key has been pressed, and then do something with it.  Some examples are shown.  You need an if clause for each key you want to detect, but don't need one if you aren't tracking a key shown in the example.:

 

_root.onKeyDown=keyPressed;
Key.addListener(_root);
function keyPressed() {
    var theKeyCode;
    theKeyCode=Key.getCode();
    if (theKeyCode==Key.UP) { // the up arrow is pressed
        rightpaddle._y -= paddleMoveDist;  //example, move a paddle
    }
    if (theKeyCode==Key.DOWN){// down arrow key pressed
        rightpaddle._y += paddleMoveDist;
    }
    if (theKeyCode==Key.LEFT){ // left arrow key.
        rightpaddle._x -= paddleMoveDist;
        leftpaddle._x -= paddleMoveDist;
    }
    if (theKeyCode==Key.RIGHT){ // right arrow key.
        rightpaddle._x += paddleMoveDist;
        leftpaddle._x += paddleMoveDist;
    }
    if (theKeyCode == Key.SPACE) { // space bar is pressed

        // code to do something is here

    } 
    if (theKeyCode == 65) {   // keycode 65 is the A key

        // code to do something is here

    }
    if (theKeyCode == 66) {  // keycode 66 is the B key etc.
       // code to do something is here

    }
 

}
 

Likewise, another function onKeyUp is used to determine whether a key has been released (after being pressed)

 

_root.onKeyUp=keyReleased;

function keyReleased() {
    var theKeyCode;
    theKeyCode=Key.getCode();
    if (theKeyCode==Key.UP) { // the up arrow is RELEASED
        //your code goes here
    }