ActionScript 3.0 :: Calling Function To Remove Listeners?
Mar 27, 2010Drawing Class:
Code:
private function quitTool():void {
switch (currentTool) {
[code].....
Drawing Class:
Code:
private function quitTool():void {
switch (currentTool) {
[code].....
I've been having a problem with multiple event listeners calling the same function (this is just a snippet of the code):
ActionScript Code:
stage.addEventListener(MouseEvent.MOUSE_UP,Stop_Scale,false,0,true);
stage.addEventListener(Event.MOUSE_LEAVE,Stop_Scale,false,0,true);
[code]......
I want to make a static function that I can use in all the custom classes.It should be some kill(); function that would remove the object from the stage as well as removing its event listeners. I made it in Document class, and it didn't work. Here's the code:
ActionScript Code:
public static function kill(e:DisplayObject)
{[code].....
ActionScript Code:
onClipEvent(enterFrame){
this.slot1incr();
[code]....
I have made an event listener:text0_mc.addEventListener(MouseEvent.CLICK, showMe);showMe is a function, but I need it to call more than 1 function at once i.e. -ext0_mc.addEventListener(MouseEvent.CLICK, showMe1, showMe2, showMe3, showMe4, showMe5, showMe6, showMe7);Each function contains an if statment as below.
function showMe1(event:MouseEvent):void
{
if (shuffledArray[1]==orderArray[1]){
[code].....
Is it common knowledge that distributing an event to 1000000 listeners is twice as slow as manually calling these listener functions for each such listener in 1000000 loop? This is what my conclusion is, I just cooked up some code benchmarking this. How is the native API useful then (aside from in retrospect when it probably will get more effective), if anyone can replicate it using non-native code, that will be a couple of kilobytes worth of extra code, and one can potentially achieve much greater speeds.
View 4 RepliesHow do you know when you should remove event listeners? For example, I know that if I have a TimerEvent listener, I can safely remove that event when I have stopped and / or deleted my timer. Or, if I have a display object on the stage, and I remove it, I can safely remove any event listeners associated with the display object.
View 1 RepliesIs there a way to remove ALL ENTER_FRAME event listeners at once?
View 2 RepliesI've placed listeners on the var "topic_mc" (below), but I'm not sure how to remove them properly:
Code:
private function layoutTopics(modNum:Number, attachTo:MovieClip):void {
topicLength = MainMenu.modArr[modNum].topicArr.length;
if (Interface._ui != null) {
[code]...
doesn't really work - the listeners keep running. How do I call the objects properly to remove the listeners?
I've placed listeners on the var "topic_mc" (below), but I'm not sure how to remove them properly:
Code:
private function layoutTopics(modNum:Number, attachTo:MovieClip):void {
topicLength = MainMenu.modArr[modNum].topicArr.length;
if (Interface._ui != null) {[code]....
doesn't really work - any trace statement I place within the event function will run for infinity. How do I call the objects properly to remove the listeners? Note: I've tried referring to currentTarget instead of target but that doesn't make any difference.
is it necessarily to add and remove event listeners like so:
Code:
addEventListener(MouseEvent.MOUSE_DOWN,mDown);
function mDown(e:MouseEvent){
removeEventListener(MouseEvent.MOUSE_DOWN,mDown);
[code]....
assuming rapid input, is it really better to do this rather than just let the mouseDown and MouseUp Listeners run all the time? It seems like it would be strenuous to constantly add and remove listeners like this. But this is how i generally see it done..
I'm trying to remove all the event listeners from nextBtn. the names of all the possable callback functions are stored in mdpArray. I want to use the for loop to remove all the listeners from nextBtn. the error message says
cannot convert "mdp1_1" to Function
How can i make this work.
Code:
var mdpArray:Array = ["mdp1_1","mdp1_2","mdp1_3","mdp1_4","mdp1_5"];
private function clearEvents(nextBtn):void{
var _nextBtn = nextBtn;
for(var i:Number = 0; i<mdpArray.length; i++){ _nextBtn.removeEventListener(MouseEvent.CLICK,mdpArray[i]);
}
}
I'm trying, to remove and then reassign an MouseEvent.CLICK to a on stage button, using a second on stage button.
when first button is released, it works fine, but when 2nd is released, the 1st one MouseEvent should be cleared and re-assigned a second listenner.
example code
bt_ok.addEventListener(MouseEvent.CLICK, log_in);
bt_pesquisa.addEventListener(MouseEvent.CLICK, pesquisar);
//
function log_in(e:MouseEvent) {
[Code].....
circDraw.addEventListener(Event.ENTER_FRAME,function(e:Event){expand(e .target); },false,0,true);
function expand(obj):void {
obj.height+=3;
obj.width+=3;
How would I add a line here to remove that event?
What I am trying to do is, if a user presses keyboard (key 1), picture 1 should FadeIn and if a user presses keyboard (key 2) picture one should FadeOut and picture 2 FadeIn and vice-versa. With the code mentioned below it works fine sometimes if I am not pressing any other key while it is performing one action but sometimes it behaves weird also. What I want AS3 to do is not to take any action or in other words it should not listen to keyboard until it finishes the opening or closing animation of a picture or any other object assigned to the key.
stage.addEventListener (KeyboardEvent.KEY_DOWN, KeyDownHandler);
/**///keyboard keycode for 1 & 2var key1:uint = 49;
var key2:uint = 50;
var BG1:Image1 = new Image1();
var BG2:Image2 = new Image2();
[Code] .....
In some cases it is important to remove multiple event listeners in order to reduce CPU time.[code]...
View 3 RepliesIs there some way where I can remove all event listeneres on my components all at once. Expecially when I dont have any prior knowledge of, what all listeners are being attached to my component.
View 4 RepliesI load into an empty movieclip (via addChild) a library object (a movieclip of class MyObject that extends the MovieClip class). At some point, from the main I remove this custom movieclip from its parent and I set any reference to null to completely destroy it. The question: what if in the custom movieclip class there are eventListeners? Are them deleted when I destroy the object? Should I write a method to remove them before deleting the object?
View 3 RepliesI create a stage keyboard event listener in an externally loaded swf such as:
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
My question is how and where I should remove this event listener? I have added an unload event handler to the external swf like this:
Code:
this.loaderInfo.addEventListener(Event.UNLOAD, unloadHandler);
The unloadHandler code looks like this:
Code:
private function unloadHandler(event:Event):void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onDown);
}
When the container swf unloads the external swf, unloadHandler() gets called, but with the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Where should the keyboard event be removed?
I am creating a large amount of sprites on the fly that have different listeners applied to them -- I want to be able delete these sprites and clear the memory ties that go with them, quick and easy --
Is there a method to detect/remove all listeners on an object without having to check for each one individually?
I have a mc that i use as a button. I have an external class that controls the button and adds interactivity to it, here's part of the code:
[Code]...
The problem is, i want the btn that, when clicked, remain inactive.That's why I remove all the listeners in the class. HOWEVER, it still functions as a button, I mean the functions haven't been removed at all after I click it. What's wrong here? Why are the functions still there, if I have removed them when clicked at the btn?
I have a level select screen with about 30 buttons. I created an eventListener for each of these buttons. My question is: When I click a level to enter the game, should I remove all the eventListeners, or would keeping them around not cause any problems?
View 6 Repliesjust cause i'm trying to optimize my project to the fullest (concerned about garbage collection and all that...), i want to know something about timers...is the killTimer function enough? or do i also have to remove the listeners?
PHP Code:
[code]....
I've got a project I'm working on that has two frames in the main timeline and on the second frame there is a movieclip (pages_mc) with 7 frames to it, each containing content (more movieclips, text, graphics, etc). I set it up this way because I have a title bar and navigation buttons on the main timeline I want to always stay on top of everything.If I have actionscript on an individual frame of pages_mc, that controls buttons on that frame, do I need to remove the MouseEvent listeners if the user navigates to a different frame? I've read that if you don't remove them it will eat up memory and cause poor performance.
I already ran into a problem with the ENTER_FRAME event listener, in which I was forced to remove it because it would just keep going even when the user navigated to a different frame of the pages_mc movieclip.Just didn't know how much of a problem this would be. I'm thinking that if I don't remove them, every time the user goes back to that frame it's just adding another MouseEvent listener to the buttons. Is that correct?
If you dynamically create a parent movieclip, and dynamically add a color picker to each clip, and add an event listener for each color picker, does a removeChild on the parent movieClip remove event listeners belonging to any of it's children?
View 2 RepliesI'm having problem with garbage collection in a large, not-so-well-written project that I didn't start. I am getting memory leaks -- and want to make sure that all event listeners on the offending objects are removed. I've gone through the code and done what I can, but still ain't happening -- is there a way in AS3 to take an object and see IF it has an event listener, and to REMOVE that listener -- that is, to remove ALL listeners, without knowing what they are? I know if I know what the EVENT TYPE is then I can use hasEventListener() to figure out if it has a listener of that type (although the presupposes I know all the event types being used), but if I don't know the function that it's subscribing to then I can't remove it.
Or is there some other way to simply nullify an object for GC? I've changed all listeners to weak -- but I think (since the offending objects are FLVPlaybacks) that something in the flash internal setup is still listening to my object (I've gleaned this from FB 4 profiler which I've used in external mode). I would just, at this point, like to see what's going on.
ok so i built a nice little CMS that is all XML driven, everything working pretty smooth you can open the documents edit them same them, make new ones, and so forth. the only issue that im hitting is that it will take the app some time before it understands the xml has changed.
so i built a refresh button, it removes all of the listeners nulls the two xml lists then rebuilds everything, but this will only work after sitting around for a minuet, if i refresh right after i add content or make a new file, it won't show. this isn't a huge issue since the files do eventually update, but i would like to figure out what is going on,
I've managed to create a button which on MOUSE_DOWN scales its parent object- it works just like windows in your OS, where you grab the corner and can change the scale of the window.
Here's how I've done it, (minus the MOUSE_DOWN listener)
Code:
function scaleDragHandler(event:Event):void {
var mcWidth:Number = event.target.parent.width;
var mcHeight:Number = event.target.parent.height;
[Code].....
However, it throws the error, "Access to undefined property 'scaleMC'" on the removeEventListener line. My thought was to put another listener for mouse up inside the first, but that sounds wrong
If I have 5 frames and each contanins a MovieClip on stage that includes it's class, listeners, functions etc.If I go from one frame to another,should I clean everything up? I mean remove listeners, remove childs etc etc.Or other MovieClips except those in the current frame are ignored from flash player.
View 3 RepliesLately I've found myself constantly writing removeEventListeners everywhere, which makes code quite messy. I know it's a best practice and such, but in general it has no sense. Garbage collector should handle such a simple task by itself, shouldn't it? Is it that hard to remove all listeners automatically when object is set to null?
So I just want to be sure if that's the case. Does setting an object to null, automatically remove all attached event listeners? In my case scenario is like this - I create an object and attach bunch of event listeners to it, then after a while I need to re-initialize this object. Of course setting this object to null is much easier then unbinding every listener by hand. And on most part listeners are anonymous functions, which means that it's not possible without code refactoring. When I simply re-initialize a variable with new I do not get duplicate listeners and such, but I'm not sure that it's previous value, along with all the listeners gets garbage-collected. Is it?