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.

Tuesday, December 1, 2009

Passing values to Flash using swfobject and capturing in Flash using AS3

Just like flashvars you can pass values to Flash even if you are using swfobject to display your flash. Below is the code to do that -

var so = new SWFObject("flash/465x535.swf", f_size, "385", "535", "9", "");
so.addVariable("file_path", "filename.jpg");

file_path is the variable which will be passed to Flash. Now to capture the value in Flash using AS3 the code will be as follows -

var filepath = root.loaderInfo.parameters.file_path;

Wednesday, October 7, 2009

Passing values between Flash and ASP.Net

Recently I had to integrate Flash and ASP.Net for Harley's test ride website because the website has been built in Flash and the data needs to be passed to asp.net which subsequently stores it in the MS-SQL database.

I had done it in the past and I was following the same approach but somehow the values which were returned from asp.net to flash were the complete code of the webpage whereas I was only passing "passed" or "invalid" to take some action in Flash depending upon the output from Asp.Net

After doing hours of search on the internet and R&D I finally found the problem and so to save time to my fellow developers I am posting the complete code below -

Flash Source Code:
var dataSender:LoadVars = new LoadVars();
var dataReceiver:LoadVars = new LoadVars();
dataSender.email="atul.narang@designworxz.com";
dataReceiver.onLoad = function() {
if (dataReceiver.response == "invalid") {
gotoAndStop(1);
} else if (dataReceiver.response == "passed") {
gotoAndStop(4);
}
}
dataSender.sendAndLoad("bookatestride.aspx", dataReceiver, "GET");

C# Source Code:
public partial class bookatestride : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (....)
{
Response.Write("&response=passed&");
}
else
{
Response.Write("&response=invalid&");
}
}
}

dataSender is calling the bookatestride.aspx page and passing one value in the variable called "email". In return aspx page is passing variable-value pair and that gets stored into the dataReceiver object.

I wasn't including "&" in the response.write and that was the reason I had wasted few hours trying to figure this out. So guys don't forget to include "&" in your Response.write

Thursday, June 25, 2009

Embed fonts in Flash

To embed the fonts for a dynamic text field in Flash select the text field and open the properties panel. In the properties panel you will be able to see a button called "Embed", click on that and it will ask you what all you want to embed such as Uppercase letters, Lowercase letters or Numerics.

Select the appropriate and press Ok and you are done.

Monday, May 25, 2009

JSON to Flash: YouTube Case Study

I had to implement a custom YouTube gadget in Flash. The term gadget does not refer to a specific gadget technology, such as Google Gadgets. YouTube Gadget refers only to small web applications, implemented in HTML or Flash, that are displayed within an iFrame on a YouTube brand channel.

And for that purpose I had to fetch the list of videos published by a particular YouTube user which can only be fetched in the form of JSON, RSS or Atom Feed. I decided to go with JSON, initially it was difficult for me to fetch the appropriate values I needed because the JSON returned by YouTube is quite complex in structured but finally I was able to sort it out.

JSON stands for JavaScript Object Notation and is mainly used to transmit such structured data over a network connection.

Example:
{
"firstName": "Atul",
"lastName": "Narang"
}

In my case the JSON has to be parsed from the following URL -
http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&orderby=published&author=designworxz

If you change the last parameter in the URL which is "author=designworxz" to "author=your youtube username" it will fetch list of videos published by you in the form of JSON.

The great community over at http://json.org have created a JSON parsing library for AS1, AS2 & AS3. But we will focus on Flash CS3 & AS2.0. You can find that parse class here: http://json.org/JSON.as

Below is the code to parse the JSON returned by YouTube -


import JSON;

var jsondata:LoadVars = new LoadVars();

jsondata.onLoad = function(success) {

if (success) {
trace("load successful");
var o:Object = json.parse(
unescape(this.toString()));
var s:String = json.stringify(o);
trace(s);
} else {
trace("unable to load JSON data");
}
};

jsondata.load("http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&orderby=published&author=designworxz");

The variable "s" will display the complete JSON returned by YouTube in the Output window & then the only thing you have to do is to fetch the required String, Variables & Values from the returned JSON.

Good Luck!!