Tuesday, July 13, 2010

Reading variables from text file in AS3

This post will explain how to read data from variables stored in a text file.

The format of the text file will be as follows -

var_1=first variable&var_2=second variable

You can have any number of variables defined in this text file.

The AS3 code will be as follows -

var loader:URLLoader = new URLLoader();

//telling the loader that we are dealing with variables here.
loader.dataFormat = URLLoaderDataFormat.VARIABLES;


loader.addEventListener(Event.COMPLETE, loading);

//Name of the text file to load.
loader.load(new URLRequest("content.txt"));

//news_1 & news_2 are the dynamic text fields on the stage to display the values being read.
function loading (event:Event):void {
news_1.text = loader.data.var_1
news_2.text = loader.data.var_2
}

So with the above mentioned code we can read the values of var_1 and var_2 stored in the text file and populate the .text property of the dynamic text boxes sitting on stage to display those values.

Wednesday, June 23, 2010

How to switch to fullscreen mode in AS3

Following is the code for the Flash -

import flash.display.StageDisplayState;
function goFullScreen():void {
if (stage.displayState == StageDisplayState.NORMAL) {
stage.displayState=StageDisplayState.FULL_SCREEN;
} else {
stage.displayState=StageDisplayState.NORMAL;
}
}
stage.addEventListener(MouseEvent.CLICK, _handleClick);
function _handleClick(event:MouseEvent):void {
goFullScreen();
}

Function _handleClick will be called as soon user clicks on the stage which will execute the function goFullScreen which will set the displayState to full screen.

This will only work once you have embedded the flash in HTML and view it in the browser. Another important thing to keep in mind is to set allowFullScreen paramter in the embedding code to true.

In the flash embedding code you will need to have the following -

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="test" align="middle">

<param name="allowScriptAccess" value="sameDomain" />

<param name="allowFullScreen" value="true" />
<param name="movie" value="test.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />

<embed src="test.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="test" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />

</object>

Friday, May 28, 2010

XML Parser AS2 and AS3

It's been ages, since I last posted on my blog. I have been really busy at work managing expectations.

People interested in XML parsing for dynamic websites or applications, there is an easy way of doing it rather than getting into every node and fetching values specifically in AS2. You can download XML Parser from Greensock here -

http://www.greensock.com/xmlparseras2/

It's pretty easy to work with XML using this class if you are coding in AS2 and you can find an example on the above mentioned page itself.

Also from the same page you can find a link to download the XML Parser for AS3 as well.