Remove Action from MovieClip - actionscript-3

I am adding a widget to someone elses movieclip.
On stage is another movieclip which has an action inside the movieclip (getting to this by pressing F9 in Flash).
function onClicked()
{
parent.onOldBtnClick("go");
}
What I need to do is to get rid of this behaviour and add my own action.
How can I override this action?

If it's ActionScript3, just removeEventListiner on the MovieClip, that triggers onClicked() and addEventListener for your own action. If you don't know the exact listener, you can trace it with hasEventListener. For more info on Events and how they work, you can read on ActionScript3 referance

To override, just delete that line in the onClicked function and write your own. Care must be taken while writing your block of codes, mainly scope. Because your are writing the code inside the movieclip, if you are trying to access the another movieclip in stage(withour specifying parent or root or its actual scope) then it will throw error.

Related

Will removing a MovieClip class remove the eventListeners inside it?

So, I have a class (called class A) that adds a child of a MovieClip class (both an object from the library, and a .as-file). Class A will remove and add this class a lot of times, so I donät want to be haning around with a bunch of Listeners that doesn't do anything.
I want to listens for clicks on the MovieClip, so I figured there are at least tree ways of doing this:
Add and remove an EventListener in class A at the same time you add/remove child.
Add the EventListener (to listen for clicks on itself) in the MovieClip class as-file.
Add an eventlistener inside the MovieClip (that's in the library).
So, my questions are:
Which one of the methods above is the best, and why?
Do you have to remove the eventListener in alternative 1 above? It's good practice, right?
Important question: Do I create a new Eventlistener every time I add the MovieClip class, if I've written the code like in alternative 2 above? And/Or will the EventListener be removed when the class is removed from Class A?
Would be really nice if someone could answer these questions clearly, one at a time, so I can make up my head. =)
Since you have to click on SOMETHING visual in your library MovieClip, and since your 'MovieClip class as-file' controls the library MovieClip, then you clearly want your listener function in the .as file.
2 and 3. I think that you're saying that you will be adding and removing your MovieClip multiple times, from class A. Your MovieClip's .as file should have, besides the CLICK-event listener that you're talking about, two other event-listener functions -- one that fires when there's an ADDED_TO_STAGE event, and another that listens for a REMOVED_FROM_STAGE event. These two will add and remove your CLICK listener whenever your MovieClip, itself, is added or removed from the display list.
So in the MovieClip's constructor say this: addEventListener(Event,ADDED_TO_STAGE,onAddedToStage,false,0,true);
In the 'onAddedToStage' function say:
addEventListener(Event,REMOVED_FROM_STAGE,onRemovedFromStage;
And then add your CLICK listener.
In the 'onRemovedFromStage' function: Remove both the REMOVED_FROM_STAGE listener and your CLICK listener.
The only listener that is not removed by code is the ADDED_TO_STAGE listener, but because you created a weak reference to it (the 'true' in the ’addEventListener' line that registers it), it will eventually be garbage-collected if your MovieClip is no longer needed.

accessing functions between two (2) movieclip inside a nested movieclip AS3

ok im a noob for AS3 since i just started, i have two (2) movieclips inside a movieclip, the main mc is called main_mc then the two movieclips inside named trigger_mc and move_mc, trigger_mc has the instance name of start_ani, then inside the timeline of main_mc i have this code:
import flash.events.MouseEvent;
start_ani.addEventListener(MouseEvent.CLICK, correctans);
function correctans(e:MouseEvent):void {
move_mc.animate();
}
then i created a motion as actionscript 3.0 using move_mc then i inserted the code inside the timeline of the move_mc itself, and i made a function for that motion called animate, my problem is how do i access a function between two movieclips which both are inside another movieclip, i know this method is not programming wise, but i kinda need to learn this, pls help me, i badly need this, thank you in advance.
Parent is a property, not a function - you don't need ():
this.parent.move_mc.animate();
Also, you didn't mention the instance name of the move_mc movieclip, but access like the above requires the instance name to be move_mc - the movieclip symbol name doesn't matter in actionscript.
Update 1: To clarify, you said: trigger_mc has the instance name of start_ani
Good, then this code will work:
start_ani.addEventListener(MouseEvent.CLICK, correctans);
But you didn't say: move_mc has the instance name of ???
So we don't know if this code will work:
function correctans(e:MouseEvent):void {
???.animate();
}
Fill in those ??? for one.
Update 2: do you know if the click handler is being fired? Why not add a trace statement?
function correctans(e:MouseEvent):void {
trace("got click event!");
???.animate();
}
Because for a CLICK event, you need:
start_ani.buttonMode = true;
Though as you say, this isn't good programming practice because this assumes those two movieclips are siblings of the same parent. It's not extensible. If they're not, your code could throw errors. Just keep that in mind.

AS3: Access button from class

So im quite new to AS3 but have worked with AS2 a lot before.
I have created a button and placed it on my stage then inside my class i have added this:
test.addEventListener(MouseEvent.CLICK, buttonClicked);
function buttonClicked(ev:MouseEvent):void
{
trace("Clicked");
}
Now this does not work as it can't find the stage, the only way i can get this to work is if i put the listener on the same frame as the button & not in the class.
But there must be away around this.
Thank you.
Eli
Update - adding Error messages
If I keep the above code all in the external class these are the errors i get.
Line 22 1120: Access of undefined property test. Line 22 1120: Access
of undefined property myButtonClick.
If you have created a document class with timeline then your "test" button must be in first frame. Because document class starts executing from first frame. You can access your button instance only when its available in stage.
Oh, I forgot to mention. You have to declare those instances as public variable in your document class.
public var test:SimpleButton;
Please go thru below and let me know which of the way you were having.
1) Are you having Document class?
There is a field Class in the Document Properties under the Publish tab of the flash IDE, if you are giving your class name in that field then it is called as Document Class
If you are having document class then you can create listener to your button even in the constructor button. Flash won't throw any errors like you got.
2) If you are instantiated your class in the first frame, it won't have the properties of stage till you add that to the stage using addChild. Also it won't have access to your button. And so it will throw the error, The access of undefined property.
Have you assigned the instance of the button on the stage the name "test"? The error message you posted seems to say there is nothing with the name "test" to assign the event listener to.
To check, click on the button the stage and look at the 'Properties' tab: will show in a text box near the top if it needs assigning.
Now the second error you posted means you're referring to something called "myButtonClick" without first declaring/initialising a variable/function with that name. You will either need to declare it or correct it if you meant to refer to something else.
Fixed.
I was being rava stupid as normal, forgot to put them inside the init :|
For the people who might come across this problem.
Working Code:
public function Main()
{
// constructor code
test.addEventListener(MouseEvent.CLICK, myButtonClick);
}
function myButtonClick(ev:MouseEvent):void
{
trace("button Clicked);
}
Anyway thanks guys for the help sometimes its just the simplest answers are the correct ones.
Eli

ActionScript 3 Removing All RESIZE Event Listeners

I'm working on a Flash project which is separated into separate scenes.
In Scene 1 I have multiple MovieClips (which include event listeners for RESIZE (and others) inside them).
In Scene 2 I have a few common MovieClips and new ones (which also include event listeners for RESIZE (and others) inside them).
After clicking a button from Scene 1 to go to Scene 2, it's fine, except for if I resize the stage and then I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I know it's related to the event listeners, but it would be unrealistic to remove them each individually as it's expected there will be many.
If I undertand your situation correctly, I think you will in the end need to remove each listener individually, or add the resize listener only once. Since you mentioned scenes, am I right to assume you are working on the timeline? I also am assuming the null object reference error comes from a scene that has been removed from the stage, making reference to a display object that is no more, a ref to the stage after the scene has been removed, or just calling a function (the resize handler) on an object that no longer exists.
Some way to deal with this are:
Add some checking in the listener handler functions
if (!this.stage) return
To avoid the errors, but will not help if the object the function is a method of has been removed.
To avoid needing remember to remove hundreds of listeners, create removeAllListeners and addCustomEventListener functions. Instead of the usual addEventListener, call you addCustomEventListener which in turn will call addEventListener. Have addCustomListener store the target, listener function and event string in a dictionary or array of objects. removeAllListeners can loop through the dictionary or array and remove all your listeners. It is a bit like setting up an event hub, but does not go quite that far.
Instead of adding the RESIZE event listener to each scene, add it only once. Then in the listener function call a function on whichever scene is the active scene or view.
This last one is the approach I have seen most often, and is the most bullet proof. It may be tricky to implement on the time line, I have always been a little hazy on timeline variable scope.
Yes, so far as I know there is no good automated way to do this, however it would be a good practice to create a registerAllListeners and a removeAllListeners methods that manually add and remove the appropriate listeners to your object.

Calling a Movieclip from the stage from a Class

I'm trying to call a movieclip called mcMain that's already on the stage. I'm calling it from a class and I've tried googling a whole bunch of possible solutions, none of which appear to work. I've tried stage.mcMain, this.stage.mcMain, MovieClip(root).mcMain, but nothing seems to work. Anyone got any ideas? I don't even get an error message. Just nothing happens.
I don't think that the root of your document timeline is actually the stage. However, you shouldn't be doing this. If the "Class," as you call it, is a DisplayObject, it should not know about anything outside its own scope (unless you've exposed properties or methods on it that would allow for this information to be passed in. If the Class is a data class it shouldn't know about the View at all. If it's a controller class, you'll need to pass a reference to it.
However, given the code you said you tried, I'm guessing your Class instance is some sort of DisplayObject. What you should do is dispatch a bubbling event from your Class, and then in your main Document class, listen for that event. In the event handler function, do whatever you need to do with your mcMain instance, such as adding ketchup. This should work just fine, because your main document Class can receive events from anywhere in the display list, and mcMain is its own instance.