01 Practice: Simple Movement in ActionScript

Having read the explanation related to object properties, carry out the following exercise. Have your teacher check it on your computer for credit.

  1. Start Adobe Flash and create a new Actionscript 3.0 file at the default size and frame rate.
  2. Create a square on the screen that is about 30 pixels by 30 pixels. You can select the object you drew and adjust size in either the Info Panel (Ctrol+I) or the Properties Panel.
  3. Select the square and turn it into a symbol by choosing Modify>Convert to Symbol or pressing F8. Be sure to name the symbol thing1 (case specific) and make it a Movie Clip symbol.
  4. In the Instance Name box at the top of the Properties Panel, name the instance boxyThing.
  5. Open the Actions Panel by selecting Window>Actions or by pressing F9.
  6. In the Actions Panel, enter the following code:
    boxyThing.x = 250;
    boxyThing.y = 200;
  7. Test your code by pressing Ctrl+Enter. Your box should appear near the center of your movie.
  8. Change your code slightly as follows to learn to center the object on your screen:
//set variables to set the start positions in the middle
var startX:Number = stage.stageWidth/2;  
var startY:Number = stage.stageHeight/2;
//now put the box there
boxyThing.x = startX;
boxyThing.y = startY;
  1. Now we’ll create a listener and a function.
  2. The listener consists of the following code:
stage.addEventListener(Event.ENTER_FRAME,boxMover);
  1. The function is as follows.
function boxMover(e:Event):void {
boxyThing.x += 15;
//this part loops it around by checking the x value and resetting if it's gone too far
if(boxyThing.x > stage.stageWidth) {
boxyThing.x = 0;
}
}
  1. Be sure to test your code by pressing Ctrl+Enter. If something doesn’t work, check the following:
    1. Does each line terminate with ; or {
    2. Have you been consistent in spelling variable and function names?
    3. Are your capitals used correctly according to the instructions?

When finished upload your .swf file to this assignment. Be sure to include your last name and first initial in the file name.