"There is more than one way to skin a cat" - crazy expression but you can do
what you want a number of ways in AS3.
I guess knowing that you only want to add that movieClip from within the
function would change things in the sample I sent.
Here's another basic version (not knowing what you are doing with the code or
movieClips) ...
function DrawMyStuffOnScreen():void{
var myMovieClip:MovieClip = new DefinedMovieClip();
myMovieClip.TextField.text = "SomeText";
myMovieClip.addEventListener("What text", showWhatText);
var myButton:SimpleButton = new DefinedButton();
myButton.addEventListener(MouseEvent.CLICK,DoSomething);
addChild(myMovieClip);
addChild(myButton);
}
function DoSomething(e:MouseEvent):void{
/* This is called from the button which will dispatch a
new event that your movie clip is listening for */
dispatchEvent(new Event("what text"));
}
function showWhatText(e:Event):void{
/* This event fires when the event "What text" happened */
trace(e.target.TextField.text);
}
1.) Why are you declaring your MovieClip within the function?
2.) Does that movieClip and button have to be added to the stage at a certain
time?
3.) Is all of this code on the timeline?
-Gerry
On Apr 22, 2011, at 12:35 AM, Steve Abaffy wrote:
> So basically the only way to make this work is to declare the myMovieClip as
> a global variable??
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Gerry
> Sent: Thursday, April 21, 2011 9:28 PM
> To: Flash Coders List
> Subject: Re: [Flashcoders] How to access MovieClip declared in one function
> in an event listener
>
> Steve,
>
> You have some things confused. Like Ross said, read up on event.target.
>
> Your code should look something like this...
>
> Var myMovieClip:MovieClip;
> Var myButton:SimpleButton;
>
> DrawMyStuffOnScreen();
>
>
> Function DrawMyStuffOnScreen():void{
>
> myMovieClip = new DefinedMovieClip();
>
> myButton = new DefinedButton();
>
> myButton.addEventListener(MouseEvent.CLICK,DoSomething);
>
> //Defined Movie clip has several input fields in it
>
> myMovieClip.TextField.text = "SomeText";
>
> addChild(myMovieClip);
> addChild(myButton);
> }
>
> Function DoSomething(e:MouseEvent):void{
>
> //What goes in this function to be able to do something like
>
> Trace(myMovieClip.TextField.text); // This show a complier
> error of basically I don't know what myMovieClip is;
> }
>
>
> On Apr 21, 2011, at 9:44 PM, Steve Abaffy wrote:
>
>> Hello,
>>
>>
>>
>> I basically have the follow:
>>
>>
>>
>> Function DrawMyStuffOnScreen():void{
>>
>> Var myMovieClip:MovieClip = new DefinedMovieClip();
>>
>> Var myButton:SimpleButton = new DefinedButton();
>>
>> myButton.addEventListener(MouseEvent.CLICK,DoSomething);
>>
>> //Defined Movie clip has several input fields in it
>>
>> myMovieClip.TextField.text = "SomeText";
>>
>> addChild(myMovieClip);
>>
>> }
>>
>> Function DoSomething(MouseEvent.CLICK):void{
>>
>> What goes in this function to be able to do something like
>>
>> Trace(myMovieClip.TextField.text); // This show a complier
>> error of basically I don't know what myMovieClip is;
>>
>>
>>
>> Then tried:
>>
>> Var myMC = new DefinedMovieClip();
>>
>> Trace(myMC.TextField.text) // this works but is always
> empty
>> since it initializes empty.
>>
>> So how do you access the textfield that is sitting on the
>> stage???
>>
>> }
>>
>> Thanks
>>
>> _______________________________________________
>> Flashcoders mailing list
>> [email protected]
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders