Copy link to clipboard
Copied
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 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")
}
Copy link to clipboard
Copied
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(...)
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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:)
Copy link to clipboard
Copied
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);?
Copy link to clipboard
Copied
I tried that out, same result..
This line seems strange to me..
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);?
Just ignore this, I meant to delete that:) That is what I had in workss_mc for the back/close button.
Copy link to clipboard
Copied
To access this would i need to use Event.Added_To_stage?
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
It traces:
[object mc_workss].backssClicked
Copy link to clipboard
Copied
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")
}
Copy link to clipboard
Copied
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);
}
*/
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Ah yes.. Alright, could you explain what !work is?
Does this mean if(work is null)?
Copy link to clipboard
Copied
Yes, it returns false if it is null, undefined or boolean false - just a short and fast way to check if something exists.
Copy link to clipboard
Copied
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:)
Copy link to clipboard
Copied
You are welcome.
I really do appreciate you being courteous.
Copy link to clipboard
Copied
Oh, it's my pleasure. Don't hesitate to email me if you need anything. Really! Your help to me is priceless.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now