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
LVL 6
spuleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IqAndreasCommented:
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?
0
courtthreeCommented:
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.
0
spuleAuthor Commented:
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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

IqAndreasCommented:
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.
0
spuleAuthor Commented:
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

0
IqAndreasCommented:
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
0
IqAndreasCommented:
That's odd, for some reason the code wasn't included when I hit submit.
Let's hope this works...

Just ask if you want me to explain any part of the code better.

Andreas
package com.cifka.ondra {

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

	public class MyComponent extends MovieClip{
        
        public static const INITIALIZED:String = "initialized";
        
		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;
		}
        
        private var _gridWidthSet:Boolean = false;
		public function set gridWidth(gridWidth:int):void {
			_gridWidth = gridWidth;
			
            if (initialized == false)
            {
			    _gridWidthSet = true;
			    checkIfInitialized();
			}	
		}

		public function get gridHeight():int {
			return _gridHeight;
		}
		
        private var _gridHeightSet:Boolean = false;
		public function set gridHeight(gridHeight:int):void {
			_gridHeight = gridHeight;
			
            if (initialized == false)
            {
			    _gridHeightSet = true;
			    checkIfInitialized();
			}			
		}
		
		private var _initialized:Boolean = false;
		public function get initialized():Boolean
		{ return _initialized; }
		
		private function checkIfInitialized():void
		{
		    if (_gridWidthSet && _gridHeightSet)
		    {
		        _initialized = true;
		        this.dispatchEvent(new Event(MyComponent.INITIALIZED));
		    }
		}

	}

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.