Archive for ActionScript3.0

Mouse Wheel Issue AS3

While implementing Mouse Wheel in as3 I got some issue

1. When I compile from flash cs5 then the swf doesnot fire event of mouse wheel but that worked when I publish.

2. I am using a code less mouse then also it doesnot fire mouse wheel event.

Would be great if any one have some suggestion on this.

Getting browser name in Flash

To get the browser name in flash , let say in click of a button and display it in a text field

btnMC.addEventListener(MouseEvent.CLICK,btnClicked){

var browser:String = getBrowserName();

txtField.text = browser;

}

function getBrowerName():String{

var browser:String;
var browserAgent:String = ExternalInterface.call(“function getBrowser(){return navigator.userAgent;}”);
if(browserAgent != null && browserAgent.indexOf(“Firefox”) >= 0) {
browser = “Firefox”;
}
else if(browserAgent != null && browserAgent.indexOf(“Safari”) >= 0){
browser = “Safari”;
}
else if(browserAgent != null && browserAgent.indexOf(“MSIE”) >= 0){
browser = “IE”;
}
else if(browserAgent != null && browserAgent.indexOf(“Opera”) >= 0){
browser = “Opera”;
}
else {
browser = “Undefined”;
}
return (browser);

}

 

njoy :) ………..

Loading External image files — Flash CS3

One of the improvements made in AS3 is how you load external content into your application. Some of the new changes make it easier for you to initiate and measure the progress of a download – something which was always a bit of a challenge in the AS2 world.

To load suppose an image(png file) to flash content or to a movieclip which is added in runtime. Let the image file be bird.png into a movieclip imageHolderMC. And imageHolderMC is on the stage or you have added that to stage.

var imgPath = “bird.png”;

var loader:Loader = new Loader();

var request:URLRequest = new URLRequest(imgPath);

loader.load(request);

imageHolderMC.addChild(loader);

:- Now you can see that the image is loaded into imageHolderMC. It is same as we doing loadMovie in AS2.

Njoy…………..

navigateToURL problem in Firefox

I was stuck somewhere when I have to open a new window in Flash AS3 using navigateToURL. In my work I have to open a image .jpg file in a new/other window when user click on the size of the image . Its working fine when I tested it with IE7 but it didnt work in Firefox. I googled for this I got some solution but that again didnt work for me in FF. The problem what I figured out  is when we open a new window through navigateToURL in FF it blocks if your setting of FF for Block Window popup is checked. I unchecked that and ecerything works fine for me in my system.

I am using

var urlReq:URLRequest = new URLRequest(urlToOpen);
 navigateToURL(urlReq,”_blank”);

Any one knows how to solve the problem when   Block Window popup is being checked , please let me know. Thanks..:)

onReleaseOutside in AS3

Event onReleaseOutside is removed from as3 for this we can do now is add an event listenter to stage mouse up and call the method for the same. eg:

movieClip.addEventListener(MouseEvent.MOUSE_DOWN, MCPressed);

function MCPressed(e:Event):void{

//////// do what you want then

stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);

}

function mouseReleased(e:Event):void{

// do what you what

stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased);

}

Random colors in Flash

If you are looking for the easiest way to get the random colors through actionscript go for the code

var color = Math.round( Math.random()*0xFFFFFF );

:) njoy……

Retrieving attributes name of xml tag in as3.0

Let say the xml you loaded is

<main >
 <chart Month=”Jan” Profit=”1200″ Expence=”700″/>
 <chart Month=”Feb” Profit=”1500″ Expence=”900″/>
 </main>

Now you wnat to get the nodeValue of Jan 1200 700 etc. That was able to achieve by traversing the xml and then .nodeValue is as2 we can do the same in as3 but now we have to make this as xmlDocument first. There is another solution here for as3 developer. In the same you can traverse the xml here I am using fixed index to get the value—-

Suppose you loaded the xml in variable myXML, The node value will be

myXML.chart[0].attributes()[0].name()  —- This will return you Month Similarly you can get others too

:)  njoy……

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……

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