Actionscript 3.0: Calling events only once and automatically remove them - actionscript-3

var Local:LocalConnection=new LocalConnection();
Local.addEventListener(StatusEvent.STATUS,function(event:StatusEvent):void{
// This stuff should only be running once
});
There can be many listeners applied, so only this should be removed. So basically after this event have been dispatched, there can be another listener for the same instance of LocalConnection.

You can remove anonymous event handlers inside of that anonymous event handler, as you always have a reference to the current function.
var Local:LocalConnection=new LocalConnection();
Local.addEventListener(StatusEvent.STATUS,function(event:StatusEvent):void{
// This stuff should only be running once
Local.removeEventListener(StatusEvent.STATUS, arguments.callee);
});
There is no other native way to cause an event listener to fire only once, the listener must be removed.

In my experience it is nearly always better to avoid anonymous functions in flash:
var local:LocalConnection = new LocalConnection();
local.addEventListener(StatusEvent.STATUS, statusHandler);
function statusHandler(event:StatusEvent):void{
local.removeEventListener(StatusEvent.STATUS, statusHandler);
}
Also the convention is to use lowercase letter for the beginning of a variable.

Yes you can:
Local.addEventListener(StatusEvent.STATUS,function(event:StatusEvent):void{
IEventDispatcher(event.currentTarget).removeEventListener(event.type, arguments.callee);
});

Related

as3 - Removing EventListener where function has been defined in addEventListener parameters

If I add event listeners as shown:
buttons[i][j].addEventListener(Event.ENTER_FRAME, function(e:Event){trace("foo");});
How would I go about removing this EventListener?
I've tried this but, it doesn't seem to work. :S
buttons[i][j].removeEventListener(Event.ENTER_FRAME, function(e:Event){trace("foo");});
Thanks in advance!
You can try:
myObject.addEventListener(Event.ENTER_FRAME, function(event:Event):void
{
// event.currentTarget (in this case myObject)
// event.type (in this case Event.ENTER_FRAME)
// arguments.callee (reference to the current function)
event.currentTarget.removeEventListener(event.type, arguments.callee);
trace("foo");
});
It works when you define the function which is called on handling the event.
In your case:
buttons[i][j].addEventListener(Event.ENTER_FRAME, myFunction);
function myFunction(e:Event){
trace("foo");
}
And then to remove the EventListener:
buttons[i][j].removeEventListener(Event.ENTER_FRAME, myFunction);
Hope this helps.
By definition you cannot remove that listener if you define it anonymously. That's the whole purpose of using that syntax. So if you don't mean that then you have to switch to a defined listener. If you do mean that then you have to use weakReference as:
addEventListener(Event.ENTER_FRAME, function(e:Event){trace("foo");}, false, 0, true);
The last parameter 'true' making it weak and making sure the event will be gc when the referenced object will cease to exist.
Using anonymous listener without weakRefernce set to true is an error.

as3 addEventListener to .copyTo()

Is there a way to add an event listener to the copyTo() function so I
can run code once the action is complete?
Below is my code and that last line is what I would assume might work, but it doesn't.
var sourceDir:File = File.applicationDirectory.resolvePath("userSettings.xml");
var resultDir:File = File.applicationStorageDirectory.resolvePath("userSettings.xml");
sourceDir.copyTo(resultDir, true);
// Is this possible?
sourceDir.addEventListener(Event.COMPLETE, fileCopied);
According to DOCUMENTATION no, but copyToAsync() function is dispatching Event.COMPLETE.

How can I recover an object who fires an eventListener event in AS3?

How can I access to an object who fires an eventListener event?
Let's say I have a mc:
var element = new MovieClip();
which has an eventlistener:
element.addEventListener(MouseEvent.CLICK, elementEventHandler);
And then, in the event handler, I want to add something to my mc:
function elementEventHandler(event:MouseEvent):void
{
var b1:balloon = new balloon("ballon1"); //this is another class.
event.target.addChild(b1);//this doesn't work.
}
So that is what I want to achieve... Recover the object who fired the event and then do crazy things with it (in this example, add another object in it).
If anybody has any idea, thanks in advance!
pd: yes, I know I can directly use the var element in this snippet, but in the real code I'm generating the mcs in a loop, according to a xml file.
function elementEventHandler(event:MouseEvent):void
{
// use the as-operator to cast the target into the class you need
var element:DisplayObjectContainer = e.target as DisplayObjectContainer;
// if the cast fails, element will be null, then we bail
if(!element) return;
// then, create your child and add it
var b1:balloon = new balloon("ballon1");
element.addChild(b1);
}
The reason you're getting an error is probably that the event is not coming directly from element but instead from one of its descendant objects.
"click" is a bubbling event.
Check out event flow in the DOM Level 3 Events spec to understand how the capture, target, and bubbling phases work:
http://www.w3.org/TR/DOM-Level-3-Events/#dom-event-architecture
So here's what I would do:
function elementEventHandler(event:MouseEvent):void
{
if (event.target != event.currentTarget)
// If event is not from "element", ignore it.
return;
...
}

Dispatch event on every frame for MovieClip

I have a Movie Clip, and I need to stop the movie when it's reaches a certain frame. To do this, I need to add an event listener that will dispatch when that specific movie clip enters a new frame (as opposed to the whole animation ENTER_FRAME).
bar.addEventListener(Event.ENTER_FRAME, function(e:Event) {
var b:MovieClip = MovieClip(e.currentTarget);
if(b.currentFrame == mp.getPopularity())
b.stop();
});
Any ideas?
Thanks.
Try this;
A simple function:
function playUntil(target:MovieClip, frame:int):void
{
target.stopFrame = frame; // Works because MovieClip is dynamic.
target.addEventListener(Event.ENTER_FRAME, _checkFrame);
}
With the listening function:
function _checkFrame(e:Event):void
{
var m:MovieClip = e.target as MovieClip;
if(m.currentFrame == m.stopFrame)
{
m.stop();
m.removeEventListener(Event.ENTER_FRAME, _checkFrame);
}
}
Apply it to all your instances:
playUntil(instance1, 15);
playUntil(instance2, 27);
playUntil(instance3, 113);
You should remove the event listener when it's no longer needed, otherwise you risk memory leaks. An anonymous function can make this difficult, though you might be able to do it with arguments.callee.
bar.addEventListener(Event.ENTER_FRAME, function(e:Event) {
var b:MovieClip = MovieClip(e.currentTarget);
if(b.currentFrame == mp.getPopularity()){
b.stop();
b.removeEventListener(e.type, arguments.callee);
// ^^ this may work to remove your anonymous listener.
}
});
There's another way to go about this, though. Does mp.getPopularity() change frequently? If it does not change after bar is told to play() then you could use addFrameScript. Just remember that addFrameScript is 0-indexed, so adding a script to frame 1 means you have to pass 0... so if an action is to happen on mp.getPopularity() then you'll have to pass mp.getPopularity() - 1.
var framescript:Function = function():void{
bar.stop();
bar.addFrameScript(bar.currentFrame-1, null);
// ^^ nulling the framescript after
// ^^ it is no longer needed.
}
bar.stop(); // Generally a good idea to call stop before calling addFrameScript
bar.addFrameScript(mp.getPopularity() - 1, framescript);
bar.gotoAndPlay(1); // or wherever it needs to start from.
This is a more precise solution, but you do have to remember to clean up your framescript after you're done, if you plan to use this same bar instance later with a different mp.getPopularity() value.

Clearing eventListeners on a FileReference object

I have a strange issue! I am trying to remove an event listener on a FileReference object by calling a function, but it seems not to be removed, and I do not understand why.
Here is the code:
private function clearFileUploadListeners(file:FileReference, index:String):void {
var dispatchEvent:Function = function(event:Event):void {
dispatch(event.type, event, index);
};
file.removeEventListener(Event.COMPLETE, dispatchEvent);
var bool:Boolean = file.hasEventListener(Event.COMPLETE);
if (bool)
trace("ERROR");
}
When I run this code, the trace actually happens. I don't understand why this boolean returns true, when I just tried to remove the eventListener just above! I guess I am probably doing something really stupid because it seems like a strange error.
I hope someone can please help me on this issue.
EDIT:
I believe it has to do with the fact that the dispatchEvent function is defined inside another function when I add the listener:
private function upload(file:FileReference, index:String):void {
var dispatchEvent:Function = function(event:Event):void {
dispatch(event.type, event, index);
};
file.addEventListener(Event.COMPLETE, dispatchEvent);
}
The problem is that I need to access this "index" variable from the listener, and I can't set it as a global variable as each file has it's own index and it's a burden if I have to extend each event class to keep track of the index (Event, ProgressEvent, ..). I hope someone can please help me on this.
EDIT2:
I actually found a temporary solution, I am not sure if it is the best! I put my removeListener method actually inside the upload method, but made it a variable. As AS3 allows dynamic object, I attached this method to one of my object, and so I just call the reference to the method when necessary. The event is actually removed. Is this a good solution please?
Thank you very much,
Rudy
You're right, it has to do with the fact that you're defining a function inside another function, then using it to handle events.
Each time the function upload is called, it creates a new closure, and assigns a reference to it to the dispatchEvent variable, which is then passed to the addEventListener class. So each time upload is called, it is using a new, different closure in the call to addEventListener. Similarly, in the clearFileUploadListeners function, a new closure is being created on each call (which happens to have the same code each time, but isn't the same function object). The call to removeEventListener does nothing if the given callback has not been added as an event listener for the given event, which is the case here.
To solve your problem, you need to store a reference to the closure that you pass to the addEventListener function. This way, you can get a reference to the same closure that was added when you need to remove it later in clearFileUploadListeners.
You can try something along the lines of the following code (untested):
import flash.utils.Dictionary;
var callbackRegistry:* = new Dictionary();
private function upload(file:FileReference, index:String):void {
var dispatchEvent:Function = generateFileUploadCompleteCallback();
callbackRegistry[file] = dispatchEvent;
file.addEventListener(Event.COMPLETE, dispatchEvent);
}
private function clearFileUploadListeners(file:FileReference, index:String):void {
var dispatchEvent:Function = callbackRegistry[file];
callbackRegistry[file] = null;
file.removeEventListener(Event.COMPLETE, dispatchEvent);
var bool:Boolean = file.hasEventListener(Event.COMPLETE);
if (bool)
trace("ERROR");
else
trace("YAY, ALL OK!");
}
private function generateFileUploadCompleteCallback(index:String):Function {
return function(event:Event):void {
dispatch(event.type, event, index);
};
}
Two other things to note on this subject.
If you must utilize a native Event directly then you should pretty much always make sure and use these last three optional params :
myObject.addEventListener( Event.COMPLETE, myFunction, false, 0, true );
Check Grant Skinner's post on the subject here :
http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html
And the very best practice of all is to ALWAYS (seriously always) use Robert Penner's Signals (instead of custom events) and his NativeSignals (to wrap needed native Flash events).
Five times faster than Flash's native events.
Always safe with weak references.
Any number of typed payload(s) in each Signal.
Get the SWC here :
https://github.com/robertpenner/as3-signals
Signals were designed to solve the very problem you are having.
Imagine instead of creating an array and managing that to remove all listeners if you could just call :
signalBtnClicked.removeAll();
or
signalBtnClicked.addOnce( function( e : MouseEvent ) : void { /* do stuff */ } );
Knowing that the closure you just created will immediately be dereferenced once it is called and happily go night night when the GC makes its rounds.