Tag Archives: EXIT_FRAME

AS3 Component Parameters always NULL or empty? Here’s the fix.

EDIT: A very helpful commenter named Charis Bournazos pointed out that using Event.EXIT_FRAME still sometimes means waiting a while (a while in computer terms anyway) to grab those variables. See the addendum here for a better option.

So you make a movieclip into a component by adding this to it’s class (not in a function)

[Inspectable(defaultValue='')]
public var myNewComponentParam:String = '';

But then when you try to access it later, say, in the constructor, you get nothing back (or you get the default value back). The reason is because of the order that Flash reads your code and executes things. Somebody had to decide what came first, constructors or going and getting component paramaters, and they picked constructors.

Here’s the fix:

public function myConstructor() {
	this.addEventListener(Event.EXIT_FRAME, afterEverythingElse,
		false, 0, true);
	trace(myNewComponentParam);
	// traces blank or default
}

public function afterEverythingElse() {
	this.removeEventListener(Event.EXIT_FRAME, afterEverythingElse);
	trace(myNewComponentParam); 
	// traces what you entered in the component window
}

Basically, we’re attaching a listener to an event that happens later. MUCH later. At least, in computer time it’s much later. For us it’s a tiny fraction of a second. That event is the end of the very first frame. Problem solved!