Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Sep 16, 2009 Sep 16, 2009

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?

TOPICS
ActionScript
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 16, 2009 Sep 16, 2009

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")

}

Translate
LEGEND ,
Sep 16, 2009 Sep 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(...)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 16, 2009 Sep 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2009 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 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:)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2009 Sep 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);?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 16, 2009

I tried that out, same result..

This line seems strange to me..

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

Why workss_mc?  Why not workss_mc.backss_mc?
I tested that out as well and that didn't work either.
As far as

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);?

Just ignore this, I meant to delete that:) That is what I had in workss_mc for the back/close button.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 16, 2009

To access this would i need to use Event.Added_To_stage?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2009 Sep 16, 2009

What does it trace when you do it this way in workss_mc:

backss_mc.addEventListener(MouseEvent.CLICK, backssClicked);

backss_mc.buttonMode = true;

function backssClicked(e:MouseEvent):void{

     trace(this + ".backssClicked");

     dispatchEvent(new Event("closeButtonClick"));

}

Also, will it change anything if you write:

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 16, 2009

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:)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2009 Sep 16, 2009

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")

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 16, 2009

ah ha! that seemed to return the trace! However, whenever work_mc is loaded back on the stage none of the buttons are working as I had it before..

Update on code on main timeline

stop();

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

work_mc.urbanthmb_mc.buttonMode = true;

var workss_mc:mc_workss;

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);

}

var work_mc:mc_work;

function onCloseClick(e:Event):void{

      trace("clickedtrue")

      work_mc = new mc_work();

     work_mc.x = 225;

     work_mc.y = 200;

     addChild(work_mc);

     removeChild(workss_mc);

}        

The following commented code refers to what was previously on the back button:

/*

var work_mc:mc_work;

backss_mc.addEventListener(MouseEvent.CLICK, backssClicked);

backss_mc.buttonMode = backss_mc.useHandCursor = true;

function backssClicked(event:MouseEvent) {

work_mc = new mc_work();

work_mc.x = 225;

work_mc.y = 200;

(parent as MovieClip).addChild(work_mc);

(parent as MovieClip).removeChild(this);

}

*/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2009 Sep 17, 2009

I don't know why work_ms' buttons don't work. Perhaps there is no sufficient information in the thread. One thing I  noticed though is that you create a new instance for work_mc and workss_mc every time and when you remove them from display list  - you don't nullify these instances. If you have code that listens for events on previous instnaces - you need to add this code to new instances or reuse previous instance by adding it to display list. You code may look like this:

function clickHandler(event:MouseEvent):void {
   if (!workss_mc) {
        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("clickedtrue");
    if (!work_mc) {
        work_mc = new mc_work();
        work_mc.x = 225;
        work_mc.y = 200;
    }
    addChild(work_mc);
    removeChild(workss_mc);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 17, 2009 Sep 17, 2009

Ah yes.. Alright, could you explain what !work is?

Does this mean if(work is null)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2009 Sep 17, 2009

Yes, it returns false if it is null, undefined or boolean false - just a short and fast way to check if something exists.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 17, 2009 Sep 17, 2009

Thank you so much for your help!!!  If you ever need any design or motion graphics work feel free to send me an email to pistolwreck@gmail.com!!!  I'll do some small things for you for free:)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2009 Sep 17, 2009

You are welcome.

I really do appreciate you being courteous.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 17, 2009 Sep 17, 2009
LATEST

Oh, it's my pleasure.  Don't hesitate to email me if you need anything. Really!  Your help to me is priceless.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines