ActionScript 3.0 :: Remove Listeners With An Array?
May 15, 2011
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]);
}
}
How 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.
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..
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] .....
Is 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.
I 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?
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?
just 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?
im learning as3 and am trying to understand arrays so could an array be used in this situation i have 5 buttons which all need a ROLL_OVER, ROLL_OUT and CLICK mouse event is it easier to put all the buttons in an array and then use the array identifier to add the even listeners? or is there another way to do this? p.s if anyone can give me a few basic usages of arrays that you use in your experience. where to use arrays too
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?
I'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.
Lately 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?
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].....
I've created a code to add a custom cursor and drag movieclips. It works fine for one moviclip. But I would like to create a function to add the drag function with custom cursor to any movieClip that i add to an array. My first try has problems cause I need to create a different listener name to each movieClip. cMouse = CustomMouse; mystring:FunctionParameters = movieClipsNames; mouseListener => Needs to duplicate (i guess);
Code: function arrastar (mystring) { arrasta = false; cMouse._x = _xmouse; cMouse._y = _ymouse; clips_array = new Array(); [Code] ..... I've attached my original working code to 1 movieclip with a customCursor onRollOver
I am using CS3 set to AS3. On frame one I have 120 buttons ( yes, one hundred twenty buttons), unfortunately I have tried to reduce this but to no avail. Anyway, I know I can put these into an array to take away some of the coding. but my question is can I also use a integer?
My button are named Btn1 - Btn120. I would want to use the array and integer for simple things such as event listeners for roll over, click, roll out, and a couple of basic if statements. All of the buttons are movie clips.For some other more detailed functions I know I could still call the button name ( at least I think I should still be able to), but wanted to make sure.
I am trying to create a drag and drop activity, and I would like to use an array for easy reusability.I'm new to as3 so I've been adjusting my old as2 code. I am using a component for each of my drags and most of it is working well.My problem is with the event handlers. I use the array to populate all of my 'drags' with the information added in their parameters and this works, but when I create my event listeners it seems to only want to move the last item in the array.My drag components are called drag1, drag2, and drag3.[code]