Actionscript MouseEvent - actionscript-3

I have a MouseEvent function and I need to call it without MouseEvent as well. I know there is quick and easy way to do it but I forgot....
my_mc.addEventListener(MouseEvent.CLICK, callEvent);
function callEvent(e:MouseEvent):void
{
trace("Mouse event called");
}
callEvent()???
now I need to call the same event without any events. I know I can create a new function without any event an call that from callEvent. But that is not what I am looking for...
Thanks,
Rex

You can do that easily like this (put "= null" after the event arg):
my_mc.addEventListener(MouseEvent.CLICK, callEvent);
function callEvent(e:MouseEvent = null):void
{
trace("Mouse event called");
}

When you call the function simply insert null in the braces
Like that-
CallEvent(null);

from this:
dispatchEvent(new CustomEvent(CustomEvent.ONLOADED, data ));

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: Clicking a button via actionscript

This is my very simple code:
g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent):void
{
buttonNote="G2";
addWholeNote();
}
It works great when I click the button, but is it possible to fire this function from another function using Actionscript?
In some other function:
function otherFunction() {
buttG2(null);
}
You pass in null since it is never used. You could also give the parameter a default value like this:
function buttG2(event:MouseEvent = null):void
{
buttonNote="G2";
addWholeNote();
}
And then call the function without any parameters as event will then be null as per default:
function otherFunction() {
buttG2();
}
Use a default parameter of null to allow the function call from elsewhere in your code. You won't be able to get any mouseevent data, but that's probably not an issue. You could pass null into your function as you have it, but I find this to be cleaner.
g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent = null):void
{
buttonNote="G2";
addWholeNote();
}
call it like any other function, the param is now optional.
buttG2();

Creating your own ADDED_TO_STAGE event

Is it possible to create your own ADDED_TO_STAGE event?
I´m trying to pass some arguments to its handler...
It would be like this:
addEventListener(Event.ADDED_TO_STAGE, arg1, arg2, init)
There´s any workaround for this?
Thanks.
Visiting this link will provide an in-depth answer on this, however here's a quick and dirty snapshot:
A function called by a listener can only have one argument, which is the event triggering it.
You will need to either call another function from your listener function, or create a custom event to hold the properties you want to parse. The latter is recommended, but here's how you could implement the former:
function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
finalize(arg1, arg2);
}
function finalize(a:*, b:*):void
{
trace(a, b);
}

Add EventListener to function?

Quick question... Is is possible to attach an EventListener to a function? Such that if at any point in a function's execution an Event is Dispatched the EventHandler will get fired?
Cheers.
You can use the stage to dispatch such an event and listen to it:
stage.addEventListener("myFunctionWasCalled", callback);
myFunction();
public function callback(event:Event):void {
trace("callback executed");
}
public function myFunction():void {
stage.dispatchEvent(new Event("myFunctionWasCalled"));
}
Event listeners are attached to objects that belong to a class that descends from EventDispatcher. You cannot attach them to a function.
You can achieve this by simply passing a function reference (callback) as an argument
function cb(s:String):void {
trace(s);
}
function doit(f:Function):void {
// do something
f("Hi");
// do some more stuff
}
doit(cb);
If you only want to listen for a event in the lifetime of a function call just add/remove as needed.
example:
function somefunction():void{
someobject.addEventListener(Event, eventhandler);
... doing stuff
someobject.removeEventListener(Event, eventhandler);
}
Now keep in mind if your doing this then your choice in even flows may become very hard to track down the road.
Typicality you only really need to worry about this in the life and death of a object vs the life and death of a function call.
To attach an event listener to an object this object must implement IEventDispatcher interface. So you could extend Function class and add your methods. AS3 docs say that Function is not final, but AS3 compile disagrees with it: VerifyError: Error #1103: Class Bla cannot extend final base class.
So, the short answer is: you can't attach an event listener to a function.
But as it was already mentioned you could:
Pass callback/callbacks to the function which will be called at some point: function bla(callback1:Function, callback2:Function):void.
Dispatch events from some other object during function execution, for example you could make a Functor class which has method execute() and implements IEventDispatcher. In this way you'd call the function myFunctor.execute() and could get dispatched events.

how to match as3 function type declaration with differing calls?

private function playSound():void
{
_soundChannel = _soundObj.play();
_soundChannel.addEventListener(Event.SOUND_COMPLETE, nextTrack);
}
<s:Button width="35" label=">>" click="nextTrack();"/>
Assuming the nextSound() function looks the same as playSound, typewise... The button click works fine, but the event listener won't call the function because its sending an argument the function isn't expecting.
I can change the nextTrack function to be evt:Event, but then the button is sending not enough arguments, and I can't find anything to type it to that will work. I can make a typed function to call the un-typed nextTrack function from the event listener
public function callnextsong(evt:Event):void{
nextTrack();
}
But i feel like there's probably a less circuitous way of accomplishing it!
Use a default parameter:
public function nextTrack(evt:Event = null):void {
It can then be called with or without the caller supplying a parameter.