Link to home
Start Free TrialLog in
Avatar of spule
spuleFlag for Czechia

asked on

Flash MovieClip component -- creation complete?

Hi,

I have defined a MovieClip in my Library and I wrote an AS3 class for it. I turned it into a component by defining some parameters. I want to do some initialization but can't do it in the constructor, because when the constructor is called, the component's parameters aren't set yet. How do I know that the component is initialized and that all parameters have been set?

Thanks
Avatar of IqAndreas
IqAndreas
Flag of Sweden image

Could you elaborate a little more on what you are trying to do?

I don't believe you fully understand what it means for a class to be "ready". Do you mean you want to run other code AFTER the constructor is done?

Which properties exactly are you "missing" if you try to set items in the constructor?
Avatar of courtthree
courtthree

I am not certain exactly what you're asking but I think I get what you mean. You have instantiated a class but it doesn't have all of it's information available at instantiation.

While this probably means you should structure the application slightly differently, I have had the same situation before.

The way I've dealt with it is to have an "initialiser" method in the class that is triggered by a listener that is added in the constructor. This way, the initialiser can be called later when your other data/interaction has been dealt with.

PS: An "initialiser" is not a programming term as such and is not a special type of function – it's just a term I gave a private method.
Avatar of spule

ASKER

My component has two "parameters" (as Flash calls them) that need to be set before the component can be drawn. These parameters are set in the component inspector (select the instance on the stage and go to Window > Component Inspector or Window > Properties > Parameters). As the component's look depends only on the initial value of these parameters, I want to draw it just once, right after they are set for the first time. I tried listening to Event.ADDED and Event.ADDED_TO_STAGE, but when these fire, the parameters are still not set. I want to be able to tell when Flash has initialized the component (its parameters set in the component inspector are set) and wants it to draw itself.
Ah, yes, now I see.

You are creating an action "Flash Professional" component with proeprties that are set there rather than in ActionScript.

Do you think you could post the code you are using for these properties? Sadly, I am unable to open FLA files on this computer, so the actual class would be best.
Avatar of spule

ASKER

I have set the parameters to 10, 10 in the component inspector. It traces the following:

MyComponent()
0 0
[Event type="addedToStage" bubbles=false cancelable=false eventPhase=2]
0 0
set gridWidth(int)
10 0
set gridHeight(int)
10 10

package com.cifka.ondra {
	import flash.display.MovieClip;
	import flash.events.Event;

	public class MyComponent extends MovieClip{

		private var _gridWidth:int, _gridHeight:int;

		public function MyComponent() {
			trace("MyComponent()");
			trace(gridWidth, gridHeight);
			
			addEventListener(Event.ADDED_TO_STAGE, function(evt:Event) {
				trace(evt);
				trace(gridWidth, gridHeight);
			});
		}

		/* *********** GETTERS, SETTERS *********** */

		public function get gridWidth():int {
			return _gridWidth;
		}

		public function set gridWidth(gridWidth:int):void {
			_gridWidth = gridWidth;
			trace("set gridWidth(int)");
			trace(gridWidth, gridHeight);
		}

		public function get gridHeight():int {
			return _gridHeight;
		}

		public function set gridHeight(gridHeight:int):void {
			_gridHeight = gridHeight;
			trace("set gridHeight(int)");
			trace(gridWidth, gridHeight);
		}

	}

}

Open in new window

Give the attached code a try.

You can check if the component is initialized like this:
if (myComponentInstance.initialized)   { ... }

And you can listen to when the component becomes initialized like this
myComponentInstance.addEventListener(MyComponent.INITIALIZED, onInitDone);


It might get annoying if you have dozens of properties to do this check, but it definitely works. If you really are having difficulties with this class, the best way is to set a default value for gridHeight and gridWidth, and have the code redraw or whatever you do if those values are changed via the component properties.

Do you understand, or do you have any further questions?

Good luck with your programming,
Andreas
ASKER CERTIFIED SOLUTION
Avatar of IqAndreas
IqAndreas
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial