You wouldn't have the class close itself, it doesn't know where it
lives, it could live anywhere. Instead, you would have the class (i.e,
your timeline I'm guessing) that creates an instance of the class listen
for a close event and then remove the child. The DataCard class could
broadcast when the close occurs and your other class (timeline) would
listen for that event and then remove it, or you could listen to the
button event in the DataCard class for the close event - easiest way to
do that is set the listener to listen for bubbles = true. Here is an
example of the former way, though the latter (event bubbling) is
probably better:
var dc:DataCard = new DataCard();
dc.name = "Hello";
dc.addEventListener(dc.CLOSE, onClose);
addChild(dc);
private function onClose(event:Event):void
{
removeChild(dc);
}
package
{
import flash.display.MovieClip
import flash.text.*;
import flash.events.MouseEvent
public class DataCard extends MovieClip
{
public static const CLOSE:String = "close";
public function DataCard()
{
initListeners()
}
private function initListeners():void
{
CloseButton.addEventListener(MouseEvent.CLICK, disptachClose);
}
private function dispatchClose(e:MouseEvent)
{
dispatchEvent(DataCard.CLOSE)
}
public function set name(value:String):void
{
theTextField.text = value;
}
}
}
Jason Merrill
Bank of America
GT&O and Risk L&LD Solutions Design & Development
eTools & Multimedia
Bank of America Flash Platform Developer Community
Are you a Bank of America associate interested in innovative learning
ideas and technologies?
Check out our internal GT&O Innovative Learning Blog & subscribe.
>>-----Original Message-----
>>From: [EMAIL PROTECTED]
>>[mailto:[EMAIL PROTECTED] On Behalf
>>Of Omar Fouad
>>Sent: Monday, March 17, 2008 5:16 PM
>>To: Flash Coders List
>>Subject: Re: [Flashcoders] Writing Custom MovieClip Classes
>>
>>ok this worked.... But the DataCard Clip has a CloseButton so
>>In the DataCard Class:
>>
>>this.CloseButton.addEventListener(MouseEvent.CLICK, close);
>>
>>public function close(e:MouseEvent) {
>> removeChild(this);
>>
>>}
>>
>>but it is throwing an error message.
>>I know this could be a silly question, I have not been into
>>AS3 for a long time. :)
>>
>>Thanks for your effort !
>>
>>On Mon, Mar 17, 2008 at 9:51 PM, Omar Fouad
>><[EMAIL PROTECTED]> wrote:
>>
>>> Wow..
>>>
>>>
>>> On Mon, Mar 17, 2008 at 5:08 PM, Merrill, Jason <
>>> [EMAIL PROTECTED]> wrote:
>>>
>>> >
>>> > Try this instead, this is how I would do it (you have a
>>few screwy
>>> > things in the code you posted, too many to bother
>>commenting on :) ):
>>> >
>>> >
>>> > package
>>> > {
>>> >
>>> > import flash.display.MovieClip
>>> > import flash.text.*;
>>> >
>>> > public class DataCard extends MovieClip {
>>> > public function set name(value:String):void {
>>> > theTextField.text = value;
>>> > }
>>> > }
>>> > }
>>> >
>>> > And in the .fla with the clip's class set to DataCard:
>>> >
>>> > var dc:DataCard = new DataCard();
>>> > dc.name = "Hello";
>>> >
>>> >
>>> >
>>> > Jason Merrill
>>> > Bank of America
>>> > GT&O L&LD Solutions Design & Development eTools & Multimedia
>>> >
>>> > Bank of America Flash Platform Developer Community
>>> >
>>> >
>>> > Are you a Bank of America associate interested in innovative
>>> > learning ideas and technologies?
>>> > Check out our internal GT&O Innovative Learning Blog & subscribe.
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > >>-----Original Message-----
>>> > >>From: [EMAIL PROTECTED]
>>> > >>[mailto:[EMAIL PROTECTED] On Behalf Of
>>> > >>Omar Fouad
>>> > >>Sent: Sunday, March 16, 2008 9:24 AM
>>> > >>To: Flash Coders List
>>> > >>Subject: [Flashcoders] Writing Custom MovieClip Classes
>>> > >>
>>> > >>I have a MovieClip in my project that I put in the library.
>>> > >>This MovieClip is used in the application lots of times and I
>>> > >>"attach" it on the stage on run time when required like
>>> > >>
>>> > >>var DC:DataCard = new DataCard();
>>> > >>DC.x = Math.random()*550;
>>> > >>DC.y = Math.random()*400;
>>> > >>addChild(DC);
>>> > >>
>>> > >>In the Identifier Dialogue Box I set the Class name to DataCard
>>> > >>and I am trying to associate it to a Class file
>>DataCard.as This
>>> > >>MovieClip has a text field for example, and it is named
>>"nameTF".
>>> > >>I want to be able to control the components of the DataCard
>>> > >>MovieClip from the Class Itself, in this case to change
>>the text
>>> > >>in the nameTF TextField.
>>> > >>
>>> > >>So in the Class I worte:
>>> > >>
>>> > >>package {
>>> > >> import flash.display.MovieClip
>>> > >>
>>> > >> import flash.display.MovieClip;
>>> > >> import flash.text.*;
>>> > >>
>>> > >> public class DataCard extends MovieClip {
>>> > >>
>>> > >> var cardName:String;
>>> > >>
>>> > >> static public function DataCard():void {
>>> > >> nameTF.text = cardName;
>>> > >> }
>>> > >> public function set cardName(Name:String):void {
>>> > >> cardName = Name;
>>> > >> }
>>> > >> }
>>> > >>}
>>> > >>
>>> > >>It shows me many errors and it does not work.
>>> > >>
>>> > >>when I tried:
>>> > >>
>>> > >>public class DataCard extends MovieClip {
>>> > >>
>>> > >> public function CardName(CN:String):void {
>>> > >> nameTF.text = CN;
>>> > >> }
>>> > >>}
>>> > >>
>>> > >>and in the FLA:
>>> > >>
>>> > >> var DC:DataCard = new DataCard();
>>> > >>
>>> > >>DC.CardName("here is the name");
>>> > >>
>>> > >>DC.x = Math.random()*550;
>>> > >>DC.y = Math.random()*400;
>>> > >>DC.name = "dataCard";
>>> > >>addChild(DC);
>>> > >>
>>> > >>this worked. But how can I use a Constructor, setters
>>and getters
>>> > >>in this case? Also how can I set the name of the
>>MovieClip like I
>>> > >>would do in the FLA DC.name = "DCName"; in the class itself?
>>> > >>
>>> > >>
>>> > >>Thanks for the Help.
>>> > >>
>>> > >>
>>> > >>
>>> > >>--
>>> > >>Omar M. Fouad - Digital Emotions
>>> > >>http://www.omarfouad.net
>>> > >>
>>> > >>This e-mail and any attachment is for authorised use by the
>>> > >>intended
>>> > >>recipient(s) only. It may contain proprietary material,
>>> > >>confidential information and/or be subject to legal
>>privilege. It
>>> > >>should not be copied, disclosed to, retained or used
>>by, any other
>>> > >>party. If you are not an intended recipient then please
>>promptly
>>> > >>delete this e-mail and any attachment and all copies and inform
>>> > >>the sender. Thank you.
>>> > >>_______________________________________________
>>> > >>Flashcoders mailing list
>>> > >>[email protected]
>>> > >>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>> > >>
>>> > _______________________________________________
>>> > Flashcoders mailing list
>>> > [email protected]
>>> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>> >
>>>
>>>
>>>
>>> --
>>> Omar M. Fouad - Digital Emotions
>>> http://www.omarfouad.net
>>>
>>> This e-mail and any attachment is for authorised use by the intended
>>> recipient(s) only. It may contain proprietary material,
>>confidential
>>> information and/or be subject to legal privilege. It should not be
>>> copied, disclosed to, retained or used by, any other party.
>>If you are
>>> not an intended recipient then please promptly delete this
>>e-mail and
>>> any attachment and all copies and inform the sender. Thank you.
>>>
>>
>>
>>
>>--
>>Omar M. Fouad - Digital Emotions
>>http://www.omarfouad.net
>>
>>This e-mail and any attachment is for authorised use by the intended
>>recipient(s) only. It may contain proprietary material,
>>confidential information and/or be subject to legal
>>privilege. It should not be copied, disclosed to, retained or
>>used by, any other party. If you are not an intended
>>recipient then please promptly delete this e-mail and any
>>attachment and all copies and inform the sender. Thank you.
>>_______________________________________________
>>Flashcoders mailing list
>>[email protected]
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders