Flash Exercise Notes 2
syllabus | schedule | assignments | class notes | web resources | ARTC courses
ActionScripting MovieClips
Levels and Depth

Depths

Layers in the timeline get converted to depths at runtime
Some MovieClip methods need to have a depth assigned
If you put something at the same depth as an existing item, it will erase the old one higher depth numbers are in front
Content added via code gets put in front of items in the timeline

Timeline Depths -16,384 to -1 (on the timeline - they can't be removed via code)
Dynamic 0 to 1048,575 (added via code - can remove via code)
Reserve 1,048,576 to 2,130,690,045 (can add via code but cannot remove them)

Layers

_level0 = main stage
_root = main stage
_parent = the parent of the clip you are in (this._parent.MCname._x)

this = the place where you are right now
Now a days people prefer to use "this" because if you import the MovieClip it will still work.
If you import a SWF with _root in the code then the code will have to be changed because the root level will have changed

Variable scope

To declare a global variable:
_global.variableName = 2;

To use a global variable:
myNum = variableName + 2;

You cannot use var in front of the declaration of a global
You cannot specify the type such as :Number or :String
Use globals sparingly - they take up valuable memory

Global variables are not accessible from within functions unless you say:
_global.VariableName each time you access it

Methods that act on MovieClips

createEmptyMovieClip()

MCName.createEmptyMovieClip(name:String, depth:Number):MovieClip

EX:
this.createEmptyMovieClip("container", this.getNextHighestDepth());

attachMovie()

Makes copies of items in the library and puts them on the stage
Should set Linkage on the item in the Library
Right click - Linkage - Identifier (keep name) - Check Export for ActionScript - Export in first frame
Flash deletes all unused Library items before publishing - setting up Linkage saves it from being deleted.

MCname.attachMovie(id:String, name:String, depth:Number, [initObject:Object]):MovieClip
id = the linkage ID
name = the new name
initObject are the new parameters you want to set for the object

EX:
create1.onRelease = function() {
     _root.count++;
     var MCname:String = "BTack"+count;
     attachMovie("BlueTack", MCname, _level0.getNextHighestDepth(), {_x:400, _y:50});
};
duplicateMovieClip()

duplicates a movieclip that is on the stage
The new one inherits the properties of the original.
You can change the parameters of the new MC with the initObject. the createNewMovieClip or attachMovie is used much more now a days.

MCname.duplicateMovieClip(name:String, depth:Number, [initObject:Object]):MovieClip

Ex:
MCName.duplicateMovieClip("newClip", this.getNextHighestDepth(), {_x:50, _y:50});

duplicateButton.onRelease = function() {
     _root.dubCount++;
     var newMC:String = "BTack"+dubCount;
     this._parent.redTackMaster.duplicateMovieClip(newMC, _level0.getNextHighestDepth(), {_x:200, _y:50, _rotation:145});
};

removeMovieClip()

Removes a Movieclip that has been placed on the stage using attachMovie(), duplicateMovieClip(), or createEmptyMovieClip() - you can't remove clips that have been placed on the stage by dragging them out of the library.

MCname.removeMovieClip():Void


loadMovie();

This loads in files from the Internet or locally. It can load .jpg, .swf, .gif and .png files

MCname.loadMovie(url:String):Void

Ex:
this.createEmptyMovieClip("newMovieClip", this.getNextHighestDepth());
newMovieClip.loadMovie("http://digitalarts.bgsu.edu/mitchell/spring07/artc433_1/FLAnotes/SWFs/testswf.swf");

unloadMovie()

MCname.unloadMovie():Void

Removes external content that has been loaded using loadMovie()

EX:
img_mc.unloadMovie("http://www.helpexamples.com/flash/images/image1.jpg");


startDrag()

MCname.startDrag();

Makes the movieclip follow the cursor - typically set on a onPress event and stopDrag is set on a onRelease()
The left, top, right and bottom restrict the movement of the object to an area.
To make it only go up and down, set right and left to the same number
To make it only go right and left, set the top and bottom to the same number

MCname.startDrag([lockCenter:Boolean], [left:Number], [top:Number], [right:Number], [bottom:Number]):Void

Ex:
clip.onPress = function() {
     this.startDrag();
}
clip.onRelease = function() {
     this.stopDrag();
}

stopDrag();

Use with startDrag();


hitTest()

See if the movieclip's boundary box touches the bounding box of another movieclip
MCname.hitTest(targetMovie:MovieClip):Boolean

blueHit.onPress = function() {
     this.startDrag();
};
blueHit.onRelease = function() {
     this.stopDrag();
     if (this.hitTest(hitBox)) {
     trace("you hit the spot");
     _root.warningBox.text = "Ouch!";
     }
     else
     {
          _root.warningBox.text = "Put the blue tack in the box";
     }
};

hitArea()

EX:
square_mc.hitArea = circle_mc;
square_mc.onRelease = function() {
     trace("touched it");
};


PROPERTIES of a MovieClip

_alpha Number transparency
_currentframe Number where the playhead is
_droptarget String path to the movieclip where a draggable clip was dropped on (read only)
_height Number height of the MC
_name String instance name of MC
_parent String MC that contains the MC you are in
_rotation Number the rotation of the MC
_target String path to MC relative main timeline (read only)
_totalframes Number total number of frames in MC
_url String gets address from which an SWF, JPG, GIF or PNG was loaded into the MC (read only)
_visible Boolean can you see it
_width Number width
_x Number x location
_xmouse Number x location of mouse relative to main timeline
_xscale Number percentage of the x scaling of the clip
_y Number y location
_ymouse Number y location of mouse relative to main timeline
_yscale Number percentage of the y scaling of the clip