Monday, 26 October 2009

Instantiate from library by class name

This maybe is one of the best hints for any as3 developer.

You have several objects in the Library, and some of them have sequenciall names, like "obj1", "obj2", "obj3"... and so on.

Normally if you want to instanciate an object from the library you would do like this:

var obj1:obj1=new obj1();
var obj2:obj2=new obj2();

What do you think... tedious, right? and not eficient, at all.

One of the solutions is to use the flash.util class that as a method called getDefinitionByName, wich can help a lot on this sort of thing.

By the upper example you just need a FOR cycle to instantiate all the objects. Imagine that I have from obj1 to obj5, this is how I would do it:

import flash.util.*;

private function instObj():void{
  for(var i:Number=1; i<6; i++){
    var name:String="obj"+i;
    var object = new (getDefinitionByName(name) as Class)();

Sunday, 25 October 2009

Generalize Methods

This one I saw in the "Actionscript 3 Cookbook" by Joey Lott, Darron Schall, Keith Peters and published by O'Reilly. Although it's on the first chapter of the book, it may come handy most of the times.

This recipe applies when you have a method and you want to use it several times, but with slightly different numbers of arguments, like calculating the average number. You can calculate the average between 2 numbers, or maybe, 7.

Lets  make a example:

public function average (num1:Number, num2:Number):void{
   trace("The average number is: "+(num1+num2)/2;

You call this method like this:


The answer will be:

The average number is: 4.5

So how can I call the average method with a different number of arguments?

In the first place, you don't specify the number of arguments you put in the method, when you define it.
 Then, you must have a cycle inside the method, that will read all the arguments when you call it. 
Do it like this:

public function average():Number{
  var sum:Number=0;

  for(var i:Number=0; i
  return sum/arguments.length;

The keyword arguments will retrieve the number and the value of the arguments that you place when you call a method.

then call the function like this:

var value:Number=average(2,3,4);


var value:Number=average(5,6,7,8,9,10);

This is a nice feature

Friday, 16 October 2009

How to Format Text in Actionscript

This time I will post a snippet about how to format text in a textfield that is placed dynamicaly in the Stage.

Firstly you must insert the TextField on the stage. For that you need to place these two line in order to import the class that controls the TextField object in AS3. The second import, will import the class that control the appearence of TextFields:

import flash.text.TextField;
import flash.text.TextFormat;

Next you need to create the TextField and place it in the DisplayObject list:

var myText:TextField = new TextField();

Next you can tell Flash if the user can select the text, and if you want some border around the TextField:

myText.selectable = false;
myText.border = false;

Next you can tell if the TextField is autosizable, that is, if the text inside is to be dynamic, maybe you need the TextField to stretch or shrink, according to the text value. You can even tell if the autosize is to the right, center or left:

myText.autoSize = "center";

Now we're ready to format the contents of the TextField. For that you need to create a TextFormat object that will be associated to the TextField later on.

Create the TextFormat object:

var myFormat:TextFormat = new TextFormat();

Define the font face you want to use. Then you can define if the text is to bold, underline or italic:

myFormat.font = "Myriad Pro";
myFormat.bold = true;

Give it some colour:

myFormat.color = 0xFF0000;

Change the size:

myFormat.size = 12;

Finally associate the TextFormat to the TextField:


Happy Texting...

Tuesday, 6 October 2009

AS3 Sandbox

Flash has some issues towards using foreign information, that is, you have an SWF file that has to access an external XML file, or other type of file. When this type of error occurs, you will get (if you have the Flash Player Debugger) a sandbox violation.

You have two ways to solve this... one that requires PHP or ASP, or any other script language knowledge, and the other requires that you have access to the source server (the one that has the information that you require).

This example will be to retrieve XML data, for example, a WebService.

1. By PHP, ASP or other script language (I will focus on PHP)
In order to do this you have to establish a "proxy" file, wich is, a file that will retrieve the data for you.
It works this way: In the same directory where the SWF file is you have to put a PHP (or ASP, or...) file that has a command to get the information. This is very simple.

In the SWF side:

function loadNews():void {
   var loader:Loader = new Loader();
   xml_NwsLoader.addEventListener(Event.COMPLETE, loadXMLNews, false, 0, true);
   xml_NwsLoader.load(new URLRequest(""));

Notice that I will not call the XML file but a PHP one.

In the PHP side you must put a simple address call to get the data you want.

You can always look for a example and download files for Coldfusion, PHP, ASP and Java Servlet at this URL

2. Using crossdomain
For this technique is supposed that you have access to the server where the XML file, or webservice is.

You must put a file at the server root called crossdomain.xml

You can see examples of this in detail in the Adobe site