MovieClip onLoad in AS3.0

If you want to call something once only on a movieclip’s creation like onload did in AS2.0, how do you do it in AS3.0? I know everything is an object so how can I over ride the movieclip’s constructor? Can it be done without having to write an entire class?

I thought this might work (I have a movieclip called foo in the stage):

foo.addEventListener(Event.ADDED, onMyMcAdded);

function onMyMcAdded(e:Event):void {
	trace("Added");
}

But it doesn’t. It compiles but that trace statement is never outputted.

Thanks.

Are you trying to load anything inside the movieclip ?

No, I just mean in As2.0 if for example I wanted the movie clip to be invisible when it loaded I’d do

onClipEvent(load) {
this._visible = false;
}

But there is no onload in AS3.0 so how do you do it? I know you can’t assign code to movieclips in AS3.0 but how come there is no event that replaces onload?

Thanks.

You can just use myMc.visible=false in AS 3 you dont need onClipEvent(load) in AS3.

But I want to call it when it loads so I need an equivalent on onload? Does AS3.0 lack this basic feature? I find that hard to understand.

the constructor is called when the clip is loaded…, just throw the visible=false; in the constructor… or, if you want you can do it when the clip is added to the stage:


public function imAConstructor():void
{
   addEventListener( Event.ADDED_TO_STAGE, turnVisibleOff );
}

private function turnVisibleOff( event:Event ):void
{
   visible = false;
}

Yes generally in AS3 an object is loaded when the constructor is fired, so you dont need a separate load event for a movieclip.

Thanks for the replies.

Okay then two more questions:

  1. How does ADDED_TO_STAGE work as it does nothing for me. I have tried:

function test(e:Event):void {
trace(“Added!”);
}
mymc.addEventListener(Event.ADDED_TO_STAGE, test);

Am I missing something?

  1. Can I define a movie clip’s constructor without declaring an entire class? Curiously enough when I Google as3.0 movieclip constructor I get this thread!

Thanks.

For your first question refer to the following article http://www.tink.ws/blog/eventadded_to_stage/

For the second question you need to elaborate a bit more, as in what you want to achieve.

Thanks again.

For the second question, let’s say I have a movieclip on the stage called foo. How do I write the constructor for it? Do I have to write an entire class that extends movieclip or can I just do the constructor?

I hope that makes sense.

you must write a class for it if you want to extend a movieclip’s constructor.

Thanks, I thought there might be a shorter way but obviously not. It’s hard to get into the version 3 frame of mind after years of AS2.0.

Just in case anyone else is interested tihs is what I ended up with:

package {

import flash.display.MovieClip;
import flash.events.Event;

public class ExtendsMC extends MovieClip {

	private var mc:MovieClip;

	function ExtendsMC( movieclip ) {

		trace("Contructor, used instead of onLoad");
		mc=movieclip;

		addEventListener(Event.ENTER_FRAME, onEnterFrame);

	}

	private function onEnterFrame(event:Event):void {

		mc.x+=1;

	}

}

}

This works like onLoad and onEnterFrame clip events in AS2.0 which is what I wanted.

Thanks for all your help.

If you just wanted a movieclip to move in a straight line then you could have dont something like this:


var foo:MovieClip=new MovieClip();
foo.addEventListener(Event.ENTER_FRAME,onEnterFrame);

private function onEnterFrame(evt:Event):void{
foo.x+=1;
}

Some control properties do not work when set from within the constructor (as3 + flash player 9). For example, myText_txt.enabled = false does not have the desired effect when set within the constructor.

This worked:

// ctor
public function Foo() {
this.addEventListener(Event.ADDED_TO_STAGE, init);
}

function init(ev:Event):void {
myText_txt.enabled = false;
}