Deeper Into ActionScript...

*Make sure you are attaching the script to the right thing - ie to a Button (or Movie Clip) or to a Frame.*

 

  1. Navigation:
  2. Setting the visual Properties of an object:
    1. Using a Button to change the property of another object:

      on (press){
      _root.target_object._visible = 0;
      //makes target object invisible
      }
    2. You can also do this with a keystroke. Attach this script to a Button only - but it can be an invisible or offscreen button!

      on (keyPress "L"){
      //uses the L key
      _root.target_object._x = 10;
      //moves the object to the spot 10 pixels from the left of the stage
      }

      Or,

      on (keyPress "<Left>"){
      //uses the left arrow key
      _root.target_object._xscale = 50;
      }
  3. Math Operations - you can do some basic math to change your properties dynamically.
  4. Variables - holding place for information.
    1. Can be numbers, strings, or booleans (0/1)
    2. Can be global (available anywhere in entire project), timeline-based (associated with a particular timeline - main or movie clip), or local (just used in the current script, between the { }.
    3. Must be declared, and initialized:
      • global:
        _global.myVar = 200;
      • local:
        var myVar = 200;
      • timeline:
        _root.mc_name.myVar=200;
        or just
        myVar="some text";
  5. Tracing the state of variables. You can use the Output window to monitor what's happening with your variables:

    on (press){
    objectLocation = 0;
    _root.object_name._x += 10;
    objectLocation = _root.object_name._x;
    //stores the _x value in myVar
    trace("the horizontal location is " + objectLocation);
    //combines text string in quotes with value of variable objectLocation
    }
  6. Using variables to write to Dynamic Text Boxes: (this is good for things like keeping score, or customizing the user experience with their name, etc)
    1. Create a text box. In the property inspector, make it Dynamic text.
    2. Also in the property inspector, assign a Variable to the text box (like "textVar")
    3. Then, you can use a script like this one:

      on (press){
      _root.object_name._x += 10;
      textVar = _root.object_name._x;
      }
  7. Conditional Statements - making choices. (ie if-then)
  8. Random numbers - use the Math.random() to produce a random number between 0 and 1 (well, actually, it's 0-.9999) then multiply by another number to get a more useful value. For example, this:

    myRandomnumber = int(Math.random()*10);

    gives you a number between 0 and 9. (we use the int command to convert from a decimal to an integer value)
    If you wanted to get a number between 1 and 10, just add 1 to the value:

    myRandomnumber = int(Math.random()*10) + 1;

  9. For loops - are a way of dealing with repetitive tasks. This is a real common programming technique, and the syntax in ActionScript is pretty standard:

    for(i=0;i<10;i++){
    trace(i);
    _root.MC._x--
    }

    would count from 0-9 and write the count to the Output window. It would also move the movie clip MC one pixel to the left each loop.
  10. Arrays - useful for storing lists of data. You can declare it and fill it in one statement, or declare first and add items later:

    myArray= ["Apple","Orange", "Peach", "Plum"];

    or
    myArray = new Array();
    myArray.push ("Apple");
    myArray.push ("Orange");
    etc...

    the push command adds a new item to the list.
    To access an item out of the array:

    myItem = myArray[1];
    would return the value "Orange" because array positions begin counting at 0.

    Use the splice command to remove an item from the array:
    myArray.splice(2,1);
    would remove the item at position 2, and remove 1 item from that point. If we did
    myArray.splice(2,1,"Pear");
    it would remove one item from position 2 and replace it with the string "Pear".

  11. Functions - you can create your own functions to encapsulate code you will use repeatedly. The syntax is like this:

    function sum(a,b) {
    //the function is named sum
    c = a + b;
    return c;
    }
    //this function takes parameters at a and b and returns the value c.

  12. attachMovie - use this to dynamically add movie clips from the Library to the Stage. Note: if something is in the Library but not used on the Stage/Timeline, it will be removed to save space when the project is published. So in order to make sure it is included, you need to "Link" the object. In the Library window, select the item and click the Properties (I) button in lower left. You may have to click the Advanced button to get all options. Under Linkage, be sure to check Export for Actionscript.


    To load the MC using attachMovie, use the following code:

    attachMovie("MC_name", "instance_name", level);
    each attachMovie instance needs a unique level.

    Often, the attachMovie command is used in conjunction with a for loop to load many instances of an MC:

    for(i=0;i<=5;i++){
    attachMovie("sprite", ["sprite" + i], i);
    //notice how i is appended to the instance name and used to give a unique level number
    _root["sprite" + i]._x = i * 50;
    _root["sprite" + i]._y = i * 50;
    //this part puts each instance in a different spot on the stage
    }

  13. Making objects drag-and-droppable: (Note! - this will not work if you attach it to objects on the main timeline. If you try to do this, it will make the whole stage draggable! Instead, put the object inside a movie clip, and attach the code within the MC.)

    on (press){
    startDrag(this);
    //the "this" means it is targeting itself
    }

    on (release){
    stopDrag();
    }
  14. Checking to see if objects intersect using hitTest. This works well with the draggable items, above. You can use this to create an interactive interface where a draggable item can act as a "tool" on another item.

    if(_root.target.hitTest(_root.bullet)){
    gotoAndPlay("youwin");
    }

    In this case, we're checking to see if the bullet MC intersects the target MC, and if so, jumping to a labeled frame named "youwin".
  15. Custom cursor, and Showing and Hiding the mouse: you can use Mouse.hide and Mouse.show to toggle the cursor off and on. Then you need a script that will run every frame that will move your custom cursor-replacement to the mouse coordinates:

    _root.cursor_mc._x = _xmouse;
    _root.cursor_mc._y = _ymouse;

  16. Playing sounds with ActionScript: there are 3 steps - first, create a variable of type Sound, then tell this variable which sound to play, then trigger the sound. Remember to set the Linkage of the sound in the library to Export to ActionScript.

    on (press){
    mySound = new Sound();
    mySound.attachSound("beep");
    mySound.start();
    mySound.setVolume(50);
    //you can set the volume 0-100
    }

  17. Setting colors: This is actually pretty similar to the Sound operation above. You need to create a new Color variable, associate it with a particular movie clip, and then set the color Hex value.

    on (press){
    myColor = new Color("myMovieClip");
    myColor.setRGB(Oxff0000);
    //ff0000 is the hex for Red
    }
    You can get Hex values for colors in a color palette window.