February 02, 2010

Problem in adding the sprite class directly to canvas component in Flex

Hi,
we cannot directly added the sprite component to the Canvas component or any other Flex components; the main reason was all Flex omponent derived from the UIComponent. So it can support only to add the UIComponent to the flex component that are derived from the UIComponent. Its better way we can add the sprite component to the UIComponent and then we can add this UIComponent to the Canvas or other Flex components.

Sprite Vs Shape

HI,
Sprite and Shape are the basic graphics component for draw the graphics. The important points to be noted as the shape class is cannot dispatch the common event like mouse or keyboard event. So if you need to perform any mouse related event to be perform then you can choose sprite instead of shape class; because the sprite class having the basic event like mouse and keyboard relate events.

Create the custom HTTP Service class and return XML as output in AS3

Hi,
I am creating the custom HTTP service class for accessing URL and get the response from the server and return the result as xml format. Here i attached the code for that.


package service
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import service.MessageService;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.events.IOErrorEvent;
import flash.net.URLRequestMethod;
import flash.events.SecurityErrorEvent;
public class URLService
{
private var _url:String;
private var _callbackObject:Object;
private var _callbackFunction:Function;
private var _urlError:MessageService = new MessageService("URL Error","Warning");
private var _ioError:MessageService = new MessageService("IO Error","Warning");
private var _xmlError:MessageService = new MessageService("XML Error","Warning");
private var _securityError:MessageService = new MessageService("Security Error","Warning");
public function set url(val:String):void
{
if(_url != val)
_url = val;
}
public function set callbackObject(val:Object):void
{
if(_callbackObject != val)
_callbackObject = val;
}
public function set callbackFunction(val:Function):void
{
if(_callbackFunction != val)
_callbackFunction = val;
}
public function URLService()
{
}
public function loadXML(urlVar:URLVariables = null,callbackObject:Object = null,callbackFunction:Function = null):void
{
if(_url && _url != "")
{
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest(_url);
if(urlVar)
{
urlRequest.data = urlVar;
urlRequest.method = URLRequestMethod.POST;
}
urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE,onComplete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
_callbackObject = callbackObject;
_callbackFunction = callbackFunction;
}
else
{
_urlError.show();
}
}
public function selfNavigation(urlVar:URLVariables = null):void
{
if(_url && _url != "")
{
var urlRequest:URLRequest = new URLRequest(_url);
if(urlVar)
{
urlRequest.data = urlVar;
urlRequest.method = URLRequestMethod.POST;
}
navigateToURL(urlRequest,"_self");
}
else
{
_urlError.show();
}
}
private function onComplete(evt:Event):void
{
var _xml:XML = XML(evt.target.data);
if(_callbackObject && _callbackFunction != null)
{
_callbackFunction.call(_callbackObject,_xml);
}
else
{
_xmlError.show();
}
}
private function onIOError(evt:IOErrorEvent):void
{
_ioError.show();
}
private function onSecurityError(evt:SecurityErrorEvent):void
{
_securityError.show();
}
}
}

This is very nice class. So use this class and enjoy.

Dispatch custom event with values in AS3

Hi,
We can dispatch custom event with some values. The following steps we can send the values when dispatch the event. The Custom Dispatch Event class is created as follows


package service
{
import flash.events.Event;
import flash.net.FileReference;
public class UploadEvent extends Event
{
private var _data:Array;
public function get data():Array
{
return _data;
}
public function set data(val:Array):void
{
if(_data != val)
_data = val;
}

public static const UPLOAD_FILE_COMPLETD:String = "UploadFileSelected";
public function UploadEvent(type:String,arr:Array=null):void
{
this.data = arr;
super(type);
}
}
}

Event dispatches by following way
dispatchEvent(new UploadEvent(UploadEvent.UPLOAD_FILE_COMPLETD,data[Array]));

Handler function for the dispatched event as follows
[Event Dispatcher Instance].addEventListener(UploadEvent.UPLOAD_FILE_COMPLETD,onUpoadComplete);

private function onUpoadComplete(evt:UploadEvent):void
{
trace(evt.data);
//handle code here;
}

Dispatch a custom event without any values in AS3

Hi,
We need to dispatch the custom event for perform certain custom function after certain activity may be happen. The custom dispatch event is very nice approach and it used the DOM for event handling.
The some points to be remember for dispatch the event
1. The customevent class must be subclass of Event class i.e it extends from Event class
2. We can send some data when custom event dispatches
3. The event dispatcher class must support the feature of dispatch event. the UIComponent class supports its features otherwise we need to create the subclass of EventDispatcher class
4. To capture the dispatched event and handle those event by handler function.
Create the custom Dispatch event class without any values as follows

package service
{
import flash.events.Event;
public class UploadEvent extends Event
{
public static const UPLOAD_FILE_COMPLETD:String = "UploadFileSelected";
public function UploadEvent(type:String):void
{
super(type);
}
}
}
Dispatch the custom Event by
dispatchEvent(new UploadEvent(UploadEvent.UPLOAD_FILE_COMPLETD));
create the handler file for dispacthed custom event as follows
instance name[Event Dispatcher].addEventListener(UploadEvent.UPLOAD_FILE_COMPLETD,onUploadCompleted);
private function onUploadCompleted(evt:UploadEvent):void
{
//handling code here
}