Archive for June, 2008

AS3 function Overloading

The below mxml code demonstrates function overloading in AS3 using …args
fnCreateOverLoaded function can be given with variable number of arguments and handled differently.

<?xml version=’1.0′ encoding=’utf-8′?>
<Application xmlns=’http://www.adobe.com/2006/mxml’&gt;
<Script>
<![CDATA[
import flash.utils.Dictionary;
import flash.events.Event;
public var targetMap: Dictionary = new Dictionary();
public var targetEventMap: Dictionary;
private function addEvent(event:Event, listener:Function, …args ) : void
{
targetEventMap = targetMap[event.target] == undefined ? new Dictionary : targetMap[event.target];
targetEventMap[event.type] = { listener:listener, args:args };
targetMap[event.target] = targetEventMap;
event.target.addEventListener( event.type, onEvent,false,0,true);
}
private function onEvent ( e:Event ) : void
{
targetEventMap = targetMap[e.currentTarget];
if (targetEventMap[e.type].args[0] is Event) targetEventMap[e.type].args.shift();
targetEventMap[e.type].args.unshift( e );
targetEventMap[e.type].listener.apply( e.currentTarget, targetEventMap[e.type].args );
}
private function handlerFn(e:MouseEvent,…args) : void
{
var tempObject:Object = new Object();
var functionName:String = ‘fnc’ + (args.length).toString();
tempObject.functionName = new Function()
tempObject.functionName.call(e.target.parent[‘fnOverLoaded’].apply(this, args))
}
private function fnOverLoaded(…args):void {
mx.controls.Alert.show( args.toString(),’arguments ‘+(args.length).toString())
}
private function fnCreateOverLoaded(…args):void {
var event:Event = args[0] as Event
if(targetMap[event.target] == undefined ){
this[‘addEvent’].apply(this,args)
event.target.dispatchEvent(event)
}
}
public function removeEvent( target:IEventDispatcher, type:String ) : void
{//removeEvent(btn1,MouseEvent.CLICK)
if(targetMap[target]!=undefined){
var targetEventMap : Dictionary = targetMap[target];
delete targetEventMap[type];
target.removeEventListener( type, onEvent );
}
}
]]>
</Script>
<Button id=’btn1′ label=’with2Args’ click=”fnCreateOverLoaded(event, handlerFn,’arg1′, ‘arg2′)” />
<Button label=’with4Args’ click=”fnCreateOverLoaded(event, handlerFn,’arg1′, ‘arg2′,’arg3’, ‘arg4’)” />
</Application>

Advertisements

custom Event with isDefaultPrevented()

Step 1:
dispatchEvent(new Event(“customEvent”, false, true )) [Link]
Step 2:
addEventListener( “customEvent”, handler, false, EventPriority.DEFAULT_HANDLER, true );  [Link]
The EventPriority.DEFAULT_HANDLER enables prevent default

 By Implementing these steps – and plus your custom handling for event.isDefaultPrevented()  enables your custom event to be prevented from action. Example: [Link]

Flash Player 10 Release

Great news, now we can save file from flash. we have new data type vector, support for 3d, new text engine and lot more 🙂  To develop code using flex for Flash 10

Code to Save any file in using flash
var fr:FileReference;
fr.save(‘data to be saved in a file’, fr.name);

example : files

Adobe release notes