PDA

View Full Version : MovieClip Class to Load its Symbol to Stage



Raabi
October 1st, 2009, 10:35 PM
Hello everybody

This is my first post in this forum. I am struggling with one issue as below:

In order to make it very simple, I have simplified the code as below:



Test_btn.addEventListener(MouseEvent.CLICK, TestHandler);
function TestHandler(e:MouseEvent):void
{
var mc: myMC = new myMC();
}


/////////// Class definition ///////////



package
{
import flash.display.*;
import flash.events.*;
public class myMC extends MovieClip
{
public function myMC()
{
parent.addChild(this); // reports error.
}
}
}
}


In this case, parent= NULL error shows up.

My requirement is just to load the MC on to the stage. Please help.

dandylion13
October 1st, 2009, 11:03 PM
I couldn't see anywhere in your code that the object was actually added to the stage. Do you mean something like this?

Test_btn.addEventListener(MouseEvent.CLICK, TestHandler);
function TestHandler(e:MouseEvent):void
{
var msgBox_mc: showMsg;
var msg:String = "Hello!";
mgBox_mc = new showMsgBox(msg);
addChild(mgBox_mc);
}

Raabi
October 1st, 2009, 11:16 PM
Thank you for your attention. If I include
addChild(msgBox_mc); on the main timeline, it reports the following error:

1120: Access of undefined property msgBox_mc.

I have included the same statement inside onAddedToStage event handler function. This place does not make much sense to me, but if put the same anywhere else, like inside the Constructor function, parent = NULL error shows up.

Secondly, my intention is to create the msgBox as a stand-alone object, and it should not be required to add it in its consumer code - but just to call it and it does the rest. The class iis supposed to bring its symbol by itself.

I hope, you geeks will definitely come up with some solution. Thanks again.

Raabi
October 1st, 2009, 11:44 PM
In order to make it very simple, I have revised the code as below:



Test_btn.addEventListener(MouseEvent.CLICK, TestHandler);
function TestHandler(e:MouseEvent):void
{
var mc: myMC = new myMC();
}


/////////// Class definition ///////////



package
{
import flash.display.*;
import flash.events.*;
public class myMC extends MovieClip
{
public function myMC()
{
parent.addChild(this);
}
}
}
}


In this example, compiler reports parent = NULL.

I just want the MC appear on the stage. Please help.

dandylion13
October 2nd, 2009, 12:48 AM
Hi,

Is it:

mgBox_mc

or

msgBox_mc

?

Your code contains both

Also, could you please post your entire document class?

Regarding this code:

parent.addChild(this);


The object hasn't been added by its parent yet so it doesn't have a parent. Only parents can add children, children can't add themselves to parents.

You would need to use:

var mc: myMC = new myMC();
addChild(mc);

snickelfritz
October 2nd, 2009, 01:51 AM
dandylion13 is correct I think; the parent should add the child to the displaylist.
You might be able to simulate this functionality by dispatching an event from the child that ultimately calls a function in the parent that adds the child, but this is going around the barn the long way.

BTW, technically, class names should always begin with a Capital letter.
ie: MovieClip, Sprite, SimpleButton, etc...

Raabi
October 2nd, 2009, 03:26 AM
Sorry for the late response. Thank you very much, both, for the replies. It made me one point clear, and is quite logical, that the child can be added by parent only.

Have a good time.