Tag Archives: AS3

ActionScript 3.0

CoolIris type thing in 30 lines

package { // Piclens type thing in 30 lines (FP10+) Liam O'Donnell - spikything.com
	import flash.display.Sprite;
	import flash.events.Event;
	public class Main extends Sprite {
		private var container :Sprite;
		private var imageGrid :Sprite;
		private var images :Array = [];
		public function Main():void {
			container = addChild(new Sprite()) as Sprite;
			imageGrid = container.addChild(new Sprite()) as Sprite;
			for (var i:uint = 0; i < 200; i++) images.push(getItem(i));
			images.sortOn("z", Array.NUMERIC | Array.DESCENDING);
			for each (var item:Sprite in images) imageGrid.addChild(item);
			addEventListener(Event.ENTER_FRAME, update);
		private function getItem(index:uint):Sprite {
			var item:Sprite = new Sprite();
			item.x = -(200 / 3) * 210 / 2 + (index / 3) * 210;
			item.y = (index % 3) * 160 - 40;
			item.z = 100 + Math.random() * 2000;
			item.graphics.beginFill(Math.random() * 0xffffff);
			item.graphics.drawRect(0, 0, 200, 150);
			return item;
		private function update(e:Event):void {
			imageGrid.x += ((stage.stageWidth / 2) - mouseX) * .2;
			container.rotationY = ((stage.stageWidth / 2) - mouseX) * .2;

How to embed fonts in pure AS3

On my travels as a contractor, I’ve seen various methods used for embedding fonts in ActionScript 3 projects (code embedded, creating font SWFs, runtime loading, etc). Each of them has its own merits (and limitations), but generally you’re looking for something that’s easy to do, easy to maintain (for others, not just yourself) and of course that works!

So, here’s the method I use for pure AS3 projects I compile with the Flex SDK. I embed the TTF or OTF file in a ‘Style’ class and include those font files in the CVS/SVN repository too – it does my head in when fonts go missing, because a designer can’t remember which font they used:

import flash.text.Font;
import flash.text.TextFormat;

Default ranges
U+0020-U+0040, // Punctuation, Numbers
U+0041-U+005A, // Upper-Case A-Z
U+005B-U+0060, // Punctuation and Symbols
U+0061-U+007A, // Lower-Case a-z
U+007B-U+007E, // Punctuation and Symbols

Extended ranges (if multi-lingual required)
U+0080-U+00FF, // Latin I
U+0100-U+017F, // Latin Extended A
U+0400-U+04FF, // Cyrillic
U+0370-U+03FF, // Greek
U+1E00-U+1EFF, // Latin Extended Additional

[Embed(source = '../fonts/myfont.ttf', fontName = 'MY_FONT',
fontWeight = 'regular', unicodeRange =
mimeType = 'application/x-font')]
public static const MY_FONT :Class;

public static const DEFAULT_FONT :String        = "MY_FONT";
public static const DEFAULT_TEXT_COLOUR :int = 0xFFFFFF;
public static const DEFAULT_TEXT_SIZE :int = 14;
public static const MY_TEXT_FORMAT :TextFormat = new

Through experience, this seems to be the most robust and maintainable way of dealing with fonts (not to throw out more flexible, but perhaps less robust ways, such as runtime loading), especially when you need control of the unicode ranges you are embedding – which I have included for reference.

How to use ExternalInterface to integrate AS3 with JavaScript

Here’s a quick snippet of code for setting up intercommunication between a SWF and JavaScript, using ExternalInterface:

import flash.external.ExternalInterface;

// Calling a JavaScript function from AS3 (with optional parameters)
if (ExternalInterface.available) ExternalInterface.call("javascriptFunction", param1, param2);

// Implementing a JavaScript callback in AS3 (flashFunction is called when JavaScript calls javascriptFunction)
if (ExternalInterface.available) ExternalInterface.addCallback("javascriptFunction", flashFunction);