DISPLAY OBJECTS
SPRITES vs MOVIECLIPS
MovieClips and Sprites are almost the same (like humans and monkeys?) except a Sprite doesn't have as many properties, and lacks a timeline. You can't animate a Sprite. Sprites are perfect for objects you can pick up such as coins, food. Also some backgrounds. Object that don't require transparency or filters can be converted to Sprite.
EMPTY MOVIE CLIP
AS2
_root.createEmptyMovieClip("container", _root.getNextHighestDepth());
AS3
var container:MovieClip=new MovieClip();
stage.addChild(container);
MOVIE CLIP-LIBRARY
AS2:
Go to your movieClip in the library, right-click, go to Linkage. Click on the 'Export for ActionScript' tab. In the Identifier field choose a name.
ActionScript.
_root.attachMovie("new_mc", "MyMovieClip", _root.getNextHighestDepth());
AS3:
Go to your movieClip in the library, right-click, go to Linkage. Click on the 'Export for ActionScript' tab. In the Class field choose a name. Standard AS3 convention for AS3 is ..._mc, so for example name your clip "hero_mc".
ActionScript.
var MyMovieClip:MovieClip=new new_mc();
stage.addChild(MyMovieClip);
DYNAMIC MOVIE CLIP
For example you have a function that creates MovieClips, but each time the MovieClip has a different name.
Let's assume you have mc's identified/linked as box1, box2, box3 in the library.
AS2
for(var count=1;count<=3;count++)
_root.attachMovie("box"+count, "MyMovieClip"+count,_root.getNextHighestDepth());
AS3
for(var count=1;count<=3;count++)
{
var type:String= "box"+count;
var ClassReference:Class = getDefinitionByName(type) as Class;
var MyMovieClip:MovieClip = new ClassReference();
MyMovieClip.name="MyMovieClip"+count;
}
REMOVING MOVIECLIPS
AS2
removeMovieClip (ob);
ob=null;
AS3
Use this function:
function removeMovie(ob:DisplayObject)
{
if (ob.parent)
ob.parent.removeChild(ob);
ob=null;
};
then:
removeMovie(ob);
FUNCTIONS
PASSING FUNCTION PARAMETERS TO EVENTS
Say you need to create an ENTER_FRAME Event on a MovieClip, how do you pass variables to that function?
AS2
myHero.onEnterFrame = function ()
{
MoveHero(x,y, 4.000000E-001, 0, "standing", restore);
};
Actual Function
MoveHero(x,y, gravity,state)
{
//CODE
};
AS3
There's a couple of methods, but the easiest way (in my opinion)? Create a temporary Object on your MovieClip with all the variables you require.
myHero.tempObject={x:0,y:0, gravity:4.000000E-001,state:"standing"};
myHero.addEventListener(Event.ENTER_FRAME,MoveHero);
CALLING DYNAMIC FUNCTIONS
Let's say you have a dozen functions called MoveObject1, MoveObject2, MoveObject3....
tag++;
var funktion:Function = this[ "MoveObject" + tag ];
stage.addEventListener(Event.ENTER_FRAME,funktion);
MOUSE EVENTS
AS3 has turned mouse management into a nightmare. Here are some common-errors and solutions.
Why doesn't my MC behind another MC react to a mouse_click?
Because you need to set the MC in front mouseEnabled property to false. Use this function to tell you which mc's are under the mouse and if their given properties.
function _checkMouseEventTrail($e:MouseEvent):void
{
var p:* = $e.target;
while(p)
{
trace(">>", p.name,": ",p," :mChildren? ",p.mouseChildren," :mEnabled? ",p.mouseEnabled);
p = p.parent;
}
};
stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
That's right, every time you have an mc behind another mc, you will have to somehow detect when that is happening. In this case a simple solution I've found is to use
hitTestPoint for individual detection of MovieClips. So, say for example you have a house MovieClip and within that mc are some nested windows, give each window a unique instance name (window1, window2 etc), and use this routine on your ENTER_FRAME Event for the house object.
function House(event:Event)
{
var myMovie:MovieClip = MovieClip(evt.target);
var point1:Point = new Point(stage.mouseX, stage.mouseY);
if(myMovie.window1.hitTestPoint(point1.x, point1.y, true))
myMovie.mouse_over="window1"; //instance name of object
else
myMovie.mouse_over=null;
};
Seems to have worked quite well for me in the past. You might also want to check out getObjectsUnderPoint() for referencing objects under DisplayObjects, very handy if you want a single mouse click be able to detect multiple clips under it. Works better than trying to mess about with mouseEnabled all the time.