Skip to main content
January 4, 2009
Answered

custom component - dynamic instances

  • January 4, 2009
  • 2 replies
  • 320 views
Hi - I've just finished creating a custom component. During development I've been dragging the component to the stage in the ide and it has been working fine. However, now I've come to try to create it dynamically eg
var newinstance:Mycomponent = new Mycomponent();
I've got a problem.
Access to all my methods disappears including the addeventlistener method. Flash moans that there is no method named addEventListener(...) etc.
If I put my component definition in the first frame and the method calls and event listener stuff in the second frame, then flash no longer complains and I can trace all the properties etc but the events aren't fired.
So it seems there are two problems. The first I think has to do with #initclip stuff in component movieclip. In there I'm calling this.registerClass(...) as instructed but I have a feeling from what I've read that there should be something else going on in there to initialise the component when it's dynamically created. The second is something to do with eventdispatching, how is it that something so simple can take so long to figure out. I must be thick.

Any help would be greatly appreciated.

btw I've tried using createClassComponent and attachmovie with the same results.
This topic has been closed for replies.
Correct answer
Aha! - I've got the answer. In the actions layer of the first frame of your component movie clip:

#initclip
import mx.core.UIObject;
import MillertheGorilla.myComponentsClass;
this.registerClass("myComponentsClass",myComponentsClass);
this.prototype = new UIObject();
#endinitclip
stop();

To instantiate a new component at runtime:

import MillertheGorilla.myComponentsClass;

var newcomponent:myComponentsClass = new myComponentsClass();
newcomponent.createClassObject(myComponentsClass,"newcomponent",getNextHighestDepth());

2 replies

January 5, 2009
Aha! - I've got the answer. In the actions layer of the first frame of your component movie clip:

#initclip
import mx.core.UIObject;
import MillertheGorilla.myComponentsClass;
this.registerClass("myComponentsClass",myComponentsClass);
this.prototype = new UIObject();
#endinitclip
stop();

To instantiate a new component at runtime:

import MillertheGorilla.myComponent;

var newcomponent:myComponent = new myComponent;
newcomponent.createClassObject(myComponent,"newcomponent",getNextHighestDepth());
Correct answer
January 5, 2009
Aha! - I've got the answer. In the actions layer of the first frame of your component movie clip:

#initclip
import mx.core.UIObject;
import MillertheGorilla.myComponentsClass;
this.registerClass("myComponentsClass",myComponentsClass);
this.prototype = new UIObject();
#endinitclip
stop();

To instantiate a new component at runtime:

import MillertheGorilla.myComponentsClass;

var newcomponent:myComponentsClass = new myComponentsClass();
newcomponent.createClassObject(myComponentsClass,"newcomponent",getNextHighestDepth());