A Simple Pause Function in Actionscript 3

Just a quick function that you can use in your library of tricks to simulate pausing in Actionscript 3.

public static function pause(timeInSeconds:int, functionToCall:Function):void {
    var timer:Timer = new Timer(timeInSeconds * 1000);
    timer.addEventListener(TimerEvent.TIMER, callFunction, false, 0, true);
    function callFunction(event:TimerEvent):void {
        timer.removeEventListener(TimerEvent.TIMER, callFunction);
        timer = null;

The function takes 2 arguments:

timeInSeconds – how many seconds to wait before calling the function
functionToCall – the function to call after the given time has passed

example usage (assuming that you drop this function into a class called Utilities:

private function gameWinTriggered():void{
     // player just dropped the last piece on the game board,
     // but we want to wait a second before showing the win screen

private function showWinScreen):void{
     trace('You Win!');

That’s all there is to it. Really handy for instances like these when you want to wait before a transition as to not jar the player with instant changes.

Easily sort an Array of Strings: Longest to Shortest Word

So let’s say you are making some sort of a game or program in Actionscipt 3 that requires you to sort words in order from longest to shortest. for example:

var word:Array = new Array("this", "is", "a", "list", "of", "randomly","chosen","words", "to", "sort");

if you try something like:


you will get the following:


That’s not what we wanted! A cool little thing that you can do in as3 is write what’s called a ‘compare function’. As I love to do, I have made a simple class with a static function that you call:

package com.frigidfish{

    public class Sort {

        public function Sort() {
            // constructor code

        public static function hiToLow(a:String, b:String):Number {

            if (a.length < b.length) {
                return 1;
            else if (a.length > b.length) {
                return -1;
            else {
                return 0;

Now just save the class under your project in the correct folder (ie c:\myproject\com\frigidfish\Sort.as).

then, you can easily sort your array like so:

import com.frigidfish.Sort;

var word:Array = new Array("this", "is", "a", "list", "of", "randomly","chosen","words", "to", "sort");


The output will now be:



You can use compare functions to tell the built in sort function how you would specifically like your data sorted.

The function takes 2 variables that are passed by the sort function (you don’t pass anything manually to the function. it’s automatic).

Because our array contains nothing but strings, we made up 2 variable names, ‘a’ and ‘b’; both of type string.

the function can return -1, 0 or 1. Depending on the return value, the sort will be affected. Experiment with different sorting methods and see what you can come up with..

For example, to sort low to high (short word to long word in our case) you would just add this function to the Sort class:

public static function lowToHigh(a:String, b:String):Number {

            if (a.length > b.length) {
                return 1;
            else if (a.length < b.length) {
                return -1;
            else {
                return 0;

of course you will also need to call the proper function now when you sort:


I hope this helps. Just had to use it in a project myself!

Flash Actionscript 3: Easily Jumble elements of an Array or String

Sometimes in Actionscript you need an easy way to jumble elements of an array or string, such as for a card/word game. I created this simple little as3 class that you can use for just that:

// Usage:
// yourNewString = Jumble.string(yourOldString);
// yourNewArray = Jumble.array(yourOldArray);

package com.frigidfish{

    public class Jumble {

        public function Jumble() {}

        public static function string(s:String):String {
            var a:Array = s.split("");
            var r:String = "";
            a = a.sort(randomize);
            for(var i=0;i<a.length;i++){
                r += a[i];
            return r;

        public static function array(a:Array):Array {
            a = a.sort(randomize);
            return a;

        private static function randomize(objA:Object, objB:Object):int {
            return Math.round(Math.random()*2)-1;


import com.frigidfish.Jumble;

var string:String = "abcdefghijklmnopqrstuvwxyz";
var array:Array = ["a","b","c","d","e","f","g","e"];


possible output:

This is a useful couple of tools.. Maybe you could add functions to return the output in different formats? Above all, have fun with Flash Actionscript 3!

How to create an AS3 game grid

One of the things that you have to do a lot when developing grid based games in Actionscript 3 is obviously lay out the grid. This is confusing to a lot of people but in flash, it is really quite simple. I created a simple class just for this blog to demonstrate how this can be achieved.

Here is the Actionscript 3 class, and then an explanation:

package com.frigidfish{
    import flash.display.Sprite;   
    public class Grid extends Sprite{
        public var rows:uint;
        public var cols:uint;
        public var cell:Array = new Array();
        public var plot:Array = new Array();
        public function Grid(displayObject:*,rows,cols, spacer){
            this.rows   = rows;
            this.cols   = cols;
        private function populateCells(displayObject:*,spacer:int){
            var iterator:uint = 0;
            for(var c:uint = 0;c<cols;c++){
                for(var r:uint = 0;r<rows;r++){                                        
                    cell[iterator] = new displayObject;
                    cell[iterator].y = cell[iterator].height  * r + (spacer*r);
                    cell[iterator].x = cell[iterator].width * c + (spacer*c);
        private function plotCells(cell:Array){        
            var iterator:uint = 0;
            for(var c:uint = 0;c<cols;c++){
                plot[c] = new Array;
                for(var r:uint = 0;r<rows;r++){                
                    plot[c][r] = cell[iterator];

Example usage:

import com.frigidfish.Grid;

var gameGrid:Grid = new Grid(Token,5,5,5);

// There are 2 ways you can access the grid.
// by cell number (count 20 cells col by col):
gameGrid.cell[20].visible = false;

// or by plot info. if you count over 2 cols, and one
// row down, you'll see that this line makes the grid
// item invisible.
gameGrid.plot[2][1].visible = false;

Ok, so this is a very simple as3 class that can be built upon to do a lot of things. For example you could add a dynamic text field to your display object, and use the iterator in grid.as to add numbers to each cell.

You could use this as a starting point for a match type game or an array of other puzzle ideas. The point of this is just to help you understand how the grid is generated.

Let’s quickly go through the grid class:

The first thing we do is create 4 variables (lines 8-11).

rows and cols are passed to the constructor along with the class/display object that you want to instantiate and a spacer number. Note that in this class it is assumed that whatever you pass it as your display class must be displayable.

For this example, I just made a quick movieclip and called it Token, and selected ‘export for Actionscript’ in it’s properties.

Next is the constructor. As I said before, this requires 4 parameters:

your library item or class to display, rows (how many rows do you want) cols (how many columns do you want), space in pixels between display objects

the populateCells function does the meat of the work.

we create an iterator and start it at 0.
next we loop through each column.
for each column we add all of the rows:
1. create a new instance of the supplied display object and add it to the cell array
2. set the y position of the display object to it’s height x the number of the current row, + spacer
3. set the x position of the display object to it’s width x the number of the current column + spacer
4. Since this class extends Sprite, it is instantiated as a Sprite so we call just add the cell to it using the addChild() method
5. add 1 to the iterator and continue the loop.

notice that a key to making the spacer work properly is multiplying it by the row or the column. If you were to just ‘add’ spacer without multiplying by the row/col you would not achieve the desired result.

Next we execute the plotCells method. This creates an extra way to find the cell that you want. It sets up a 2 dimensional array and adds the cells to it so that you can access them by plotting coordinates.

first it creates an iterator and sets it to 0.
next we look through the length of the cols variable
now we create a new array and assign it to the plot array at the current column
for each number in cols, we iterate through all rows.
finally we assign each cell from our cell array to the proper plot coordinates.
add 1 to the iterator and process the next column.

So basically:
plot[0][0] = cell[0]
plot[0][1] = cell[1]
plot[0][1] = cell[2]

and so on. if you made a 5×5 grid, column 2, row 0 would be:
plot[2][0] = cell[10]

well, that’s it!
Hope you find this useful in your Flash Actionscript 3 projects.

Download this project as a working example here.