• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1007
  • Last Modified:

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
0
spule
Asked:
spule
  • 4
  • 2
1 Solution
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now