Loading XML in as3.0

Let suppose the xml file you want to laod is data.xml is in the same folder where our fla resides.

The code in as3 goes like this…

var myXML:XML = new XML();
var XML_URL:String = “data.xml”;
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener(“complete”, xmlLoaded);

function xmlLoaded(event:Event):void
{
    myXML =  new XML(myLoader.data);
    trace(myXML);
}

 

:) njoy……

Getting page URL in php

code goes like this—>

function curPageURL() {
$pageURL = ‘http’;
if ($_SERVER["HTTPS"] == “on” )
{
$pageURL .= “s”;
}
$pageURL .= “://”;
if ($_SERVER["SERVER_PORT"] != “80″ )
{
$pageURL .= $_SERVER["SERVER_NAME"].”:”.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

}
return $pageURL;
}

Press Esc to close window

Todays fashion is press Escape key to close popups rather than clicking on close buttons. That can be done easily by javascript.  Here is the code for that write this small function inside <script /> tag of your html code and njoy pressing Esc…………

window.document.onkeydown = function(evt)
{
if(evt.keyCode == 27)
{
closeWindow();
}
}

closeWindow is the function now its your turn to write the code inside that…. as what ever you want.

Collection Properties–Flash CS3

While creating FLA Based Component we need to display the properties of Component to flash parameters panel available for user to edit. The property values can be edited by the user in the Values dialog box (opened from a text box within the Parameters tab for your component). these are called Collection Properties.

To add a collection properties to your component. We can define it  directly to a variable or can define through getter setter method.

I will be discussing here for defining the collection properties to a variable.

1. Define two classes and that need not to extend or implement any classes. Name those classes as for your need I am naming as Collection.as and CollectionItem.as.

2. Collection class looks like :-

package{

public class Collection
{
private var _items:Array;
public function Collection(){
super();
_items = new Array();
}
public function addItem(item:Object):Boolean {
if (item != null) {
_items.push(item);
return true;
}
return false;
}
public function clear():void {
_items = new Array();
}
public function getItemAt(index:Number):Object {
return(_items[index]);
}
}

}

Nothing more you need here it define s an array and when user add any item from parameter then it will be pushed into this array(that we dont have to think how does it do..). Method getItemAt gets the data from the array and returns as object.

3. Now Comes CollectionItem Class

Basically it define the items of the Collection. The list of inspectable parameters for this class determines the list of properties shown in the Values dialog box for each item.

package {
public class FrustumCollectionItem
{

[Inspectable (defaultValue="")]
public var label:String;

[Inspectable (defaultValue="")]
public var data:String;

[Inspectable (defaultValue="")]
public var color:String;
public function FrustumCollectionItem() {
label = “”;
data = “”;
color = “”;

}
}

}

After you completed making these two classes not come to the original class where you need to make a variable as collection properties.

4. Defien the variable as object. as

public var dataSource:Object;   Now you need to import the above to classes (Collection.as and CollectionItem.as)

5. Now the last step to go……..where you declared the variable dataSource add a line just above that as :-

[Collection(collectionClass="Collection", collectionItem="CollectionItem", identifier="label")]

public var dataSource:Object;

you are done now……..njoy

Trim function in AS2.0

This function of trim let you trim spaces from both the end (Start as well as end)…

function trim (txt_str) {
while (txt_str.charAt(0) == ” “) {
txt_str = txt_str.substring(1, txt_str.length);
}
while (txt_str.charAt(txt_str.length-1) == ” “) {
txt_str = txt_str.substring(0, txt_str.length-1);
}
return txt_str;
}

Handling Special Characters

Sending or receiving special characters through URL or loading through xml file………

Let say a string contains characters like ‘éíóúýÂÃØøå’ and you want ro send it as a parameter in the URL to let say a jsp page..

var str:String =”éíóúýÂÃØøå”;

my_loadVar.load(“somejsppage.jsp?name=” + str);  In this case the str value recevied will not be well format so for that hadling these characters you need to encode the URL and that can be done ……

my_loadVar.load(“somejsppage.jsp?name=” + escape(str));  By using escape() function provided by flash we can able to encode it and in the similar way to decode these kind of variable we can use unscape(str)..

escape and unescape take parametera as a string .

Reserved Keywords in Context menu(cut,copy….) in Flash

Using Reserved keywords (cut , copy , paste delete ) in context menu of flash is does not allowed there we have to use something like a dot or three dots etc… as

var my_cm:ContextMenu = new ContextMenu();
var menuItem_cmi:ContextMenuItem = new ContextMenuItem(”Cut”, cutNode);

If u use this then nothing will be displayed in the context menu so…..

var menuItem_cmi:ContextMenuItem = new ContextMenuItem(”Cut…”, cutNode);

you have to give “Cut…” or anything except space …..

If you want to show your contextmenu Cut only then the trick is here..

var menuItem_cmi:ContextMenuItem = new ContextMenuItem(”Cut ”, cutNode);

There is a space after cut but thats not from space key its graphical space for doing that

type Cut+ctrl+2+5+5 i.e after Cut press control key and press 2 5 5 from num pad you are done now……

Trim in AS2.0

These function lTrim, rTrim and Trim let you  use the trim functionality in Flash…….

ltrim trims from left side, rTrim from right side and trim from both end..

// parameters:
// str, string to be trimmed
// returns:- string, whitespaces removed from left side.

function lTrim(str) {
if ((str.length>1) || (str.length == 1 && str.charCodeAt(0)>32 && str.charCodeAt(0)<255)) {
i = 0;
while (i<str.length && (str.charCodeAt(i)<=32 || str.charCodeAt(i)>=255)) {
i++;
}
str = str.substring(i);
} else {
str = “”;
}
return str;
}

// parameters:
// str, string to be trimmed
// returns:- string, whitespaces removed from right side.

function rTrim(str) {
if ((str.length>1) || (str.length == 1 && str.charCodeAt(0)>32 && str.charCodeAt(0)<255)) {
i = str.length-1;
while (i>=0 && (str.charCodeAt(i)<=32 || str.charCodeAt(i)>=255)) {
i–;
}
str = str.substring(0, i+1);
} else {
str = “”;
}
return str;
}

// parameters:
// str, string to be trimmed
// returns:- string, whitespaces removed from both sides.

function Trim(str) {
return lTrim(rTrim(str));
}