Skip to main content
Known Participant
September 16, 2009
Answered

AddListener to a button inside of a movieclip in the library?

  • September 16, 2009
  • 2 replies
  • 2352 views

Could someone steer me in the right direction of how to add a listener to a button inside of a movieclip that is in the library?

Example:
Root->work_mc(timeline)->workss_mc(added with Addchild)

There is workss_mc in the library that gets loaded when someone clicks a button inside of work_mc. This is achieved with addChild. Within workss_mc there is a close button to close this. I've run into problems with working the navigation with this b/c I need to declare and create a single instance of workss_mc at a scope where it is visible to both click handlers, Doing this will place both handlers in one location. So in order to do that I would need them all to be where the navigation code is, the root.

So how can I access workss_mc from the root?

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

It traces:

[object mc_workss].backssClicked

That didn't bring back anything either:(
If you want to take a look check it out at:
www.iankemp.com/beta/silvercollective7
I have the cs3 and cs4 files there:)

This is a good news.

Now, what happens if you do just following on the main timeline - nothing else but trace:

function clickHandler(event:MouseEvent) {

     workss_mc = new mc_workss();

     workss_mc.addEventListener("closeButtonClick", onCloseClick);    

     workss_mc.x = 225;

     workss_mc.y = 200;

     addChild(workss_mc);

     removeChild(work_mc);

}

function onCloseClick(e:Event):void{

     trace("clicked")

}

2 replies

Inspiring
September 16, 2009

I much better solution would be not to chase objects but create a straightforward functionality/interfaces/communication between objects. For instance, you can:

inside workss_mc add click event listener to close button --> dispatch event when button clicked --> add event listener to the display list where you place instances of workss_mc --> do whatever needs to be done. Here is a sample of implementation of this use case:

Inside workss_mc:

// whatever name of the button is

closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);

function onCloseClick(e:MouseEvent):void{

     dispatchEvent(new Event("closeButtonClick");

}

Anywhere you place instance (work_mc in your case):

var workss_mc1:workss_mc = new workss_mc();

var workss_mc2:workss_mc = new workss_mc();

workss_mc1.addEventListener("closeButtonClick", onCloseClick);

workss_mc2.addEventListener("closeButtonClick", onCloseClick);

function onCloseClick(e:Event):void{

     // do whatever

}

Naturally, if you have several clips that have close button and they all dispatch the same event - you can use the same function in their parent.

Known Participant
September 16, 2009

This is what I have on my root timeline.

work_mc.urbanthmb_mc.addEventListener(MouseEvent.CLICK, clickHandler);

work_mc.urbanthmb_mc.buttonMode = true;

var workss_mc:mc_workss;

//var workss_mc = new mc_workss();

function clickHandler(event:MouseEvent) {

workss_mc = new mc_workss();

workss_mc.x = 225;

workss_mc.y = 200;

addChild(workss_mc);

removeChild(work_mc);

}

//var workss_mc2:workss_mc = new workss_mc();

workss_mc.addEventListener("closeButtonClick", onCloseClick);

function onCloseClick(e:Event):void{

trace("clicked")

work_mc = new mc_work();

work_mc.x = 225;

work_mc.y = 200;

(parent as MovieClip).addChild(work_mc);

(parent as MovieClip).removeChild(this);

}

THEN in workss_mc

I have this dispatcher

backss_mc.addEventListener(MouseEvent.CLICK, backssClicked);

backss_mc.buttonMode = true;

function backssClicked(e:MouseEvent):void{

     dispatchEvent(new Event("closeButtonClick"));

}

What's wrong with this?  The back button is not getting the trace?  The button mode is working correctly.  I've also attached some screen shots of the main timeline and workss_mc, not sure if it will help.  Just some visual:)

Inspiring
September 16, 2009

Try:

workss_mc.addEventListener("closeButtonClick", onCloseClick, true);

Is the first part of the code on main timeline? If so, why do you add children to parent

(parent as MovieClip).addChild(work_mc);

(parent as MovieClip).removeChild(this);

not just like:

addChild(work_mc);

removeChild(this);?

Ned Murphy
Legend
September 16, 2009

At some point you should be creating the new instance...

var someVar:libObj = new libObj();

addChild(someVar);

So with that reference to he object you should be able to assign a listener to it....

someVar.addEventListener(....);

or to the next level...

someVar.insideBtn.addEventListener(...)

Inspiring
September 16, 2009

could you add the event listener to it after you declare a new instance of the object but before you add it to the stage?

var someVar:libObj = new libObj();

someVar.addEventListener(....);

.

.

.

.

later on in the code:

addChild(someVar);