Tips for learning ActionScript 3.0

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

ActionScript 3.0 is a powerful object-oriented language that represents a new programming model for the Flash Player runtime. If you are already familiar with ActionScript 1.0 or 2.0, you should be aware of some language differences as you develop your first application using ActionScript 3.0.

To help the transition adobe has compiled tips and common issues you might encounter during development.

http://www.adobe.com/devnet/actionscript/articles/actionscript_tips.html

Simple Find and Replace Function

This is a simple but effective function allows you to search a string and replace characters

function searchAndReplace(holder:String, searchfor:String, replacement:String) {
		var temparray:Array = new Array();
		temparray = holder.split(searchfor);
		holder = temparray.join(replacement);
		return (holder);
	}

Label Statements

One of the pimping new features of AS3 is the ability to label your loops. At first you may think this is pointless but once you start using them you will find out how wonderful they truly are.

They come into their own when you have nested loop structure and you want to exit loops when a certain condition is met.

Kudos goes to senocular for me finding out about this cool feature.

In AS2 you would do the following to break a loop.

var i:Number;

var j:Number;

var exit:Boolean = false;

for (i=0; i<10; i++) {

    for (j=0; j<10; j++) {

        if (i > 3 && j > 3) {

            exit = true;

            break;

        }

    }

    if (exit) {

        break;

    }

}

But with AS3 and labels you can break from a specific loop.
E.G

var i:Number;

var j:Number;

mainLoop: for (i=0; i<10; i++) {

    for (j=0; j<10; j++) {

        if (i > 3 && j > 3) {

            break mainLoop;

        }

    }

}

With labels the nested loop is able to break the mainloop. This makes the code cleaner and easier to read.

Nice.

Word Count

Returns the number of words in a string.

function wordcount(string:String):Number {
   var wrdArray:Array = string.split(" ");
   for (var i = wrdArray.length; i>0; i--) {
      if (wrdArray[i] == "") {
         wrdArray.splice(i,1);
      }
   }
   return wrdArray.length;
}
trace(wordcount("This will count the number of words"));

FileReference onComplete is not fired on Mac OS / 404 error / IO error

A While ago we had an interesting problem with file flash uploaders on Macs. It turns out it doesn’t work. But we have found the way. So here is how we solved the undocumented feature
1. Firstly ensure that you explicitly load the cross domain policy file.

2. Make sure you escape all the characters, Macs hate spaces.

3. And the most important you need to send empty response from upload server-side script.

In php, you would just do
echo (”");

after file-upload code. In ASP, you can do
Response.Write (”").

In Java servlet, you can do:
response.getWriter().println(”");

response.getWriter().flush();

That’s all, now onComplete event should trigger on Mac’s Adobe Flash Player 8.

But if sending “” dosent work try “ “ or “1” – we found that sending 1 was the best option.

Hope this shines some light on a frustrating problem

Getting List of Child MovieClips in AS3

Here is how you find all the movieclips inside an object in as3

var desc:XML = describeType(my_mc);
for each (var node:XML in desc.variable) {
    if (node.@type == "flash.display::MovieClip") {
        if (my_mc.getChildByName(node.@name.toString())) {
            trace("child mc found");
        }
    }
}

load external files / assets with AS3

This example shows you how to load and external asset (file) in AS3. The files can be a swf, jpg, gif or png.

This particular example loads a jpg called loademe.jpg and attaches to the stage

Load Asset / external files example
// loadAsset.as
package {
	import flash.display.*;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.*;

	public class loadAsset extends Sprite{

		private var _loadPath:String
		private var _loader:Loader;

		// Constructor
		public function loadAsset(){
			// Create the loader
			_loader = new Loader();

			// Add event listener that tells us when the
			// asset can be safely accessed
			_loader.contentLoaderInfo.addEventListener(Event.INIT, displayAsset);

			// Create new URLRequest
			var urlRequest = new URLRequest("loadme.jpg")

			// Start the asset load
			_loader.load(urlRequest);
		}

		// Display the asset once the load is complete
		private function displayAsset(ev:Event){
			// it to the DisplayObjectContainer
			addChild(_loader.content);
		}
	}
}

To load the asset/file you need to open the flash file and place this AS in the timeline

import loadAsset;
addChild(new loadAsset());

Mouse Event - click

In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a click event to a object.

Full code after the jump.

Mouse Event - click
// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)

cir_mc.addEventListener(MouseEvent.CLICK, clickListner);

function clickListner(ev:MouseEvent):void{
	trace("Circle Clicked");
}

Mouse Event - up

In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a mouse up event to a object.

Mouse Event - up

// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)
cir_mc.addEventListener(MouseEvent.MOUSE_UP, clickListner);
function clickListner(ev:MouseEvent):void{
trace(”Mouse Up”);
}

Mouse Event - over

In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a mouse over event to a object.

Mouse Event - over
// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)

cir_mc.addEventListener(MouseEvent.MOUSE_OVER, clickListner);

function clickListner(ev:MouseEvent):void{
	trace("Mouse Over Circle");
}
 Page 2 of 4 « 1  2  3  4 »
Close
E-mail It