Myth Buster – Benchmarking Loops

Lets have a look at some of the most common and fastest loops used in ActionScript 3. Is int really faster than uint and Number? Is while loop faster than for? Is pre-increment faster than post-increment? To test all of these I have created benchmark test application on wonderfl. App compares loops with 10.000.000 iterations and logs testing times. To compare different variable types, loop methods, increment / decrement modes, there are buttons grouping each test. Please be patient with your first click, the model Array is being generated (Link button also generates linked list). The first test compares Arrays, second one Vectors.

Read the rest of this entry »

How to communicate between loader and child

This post is about communication between loader (Parent.as) flash object (.swf) and loaded (Child.as) flash object. While flash is event based, I find it correct to use events to communicate, although different methods exist (direct function call etc.)

Read the rest of this entry »

Common Tricks in ActionScript Code Shortening

Code shortening can make your code more readable as well as more unreadable :-). While it is recommend to use coding conventions and standards, in some cases you can spare a lot of lines using some shortening. In fact there must not be a conflict between using convetion vs. using shortening, and soon after you get used to some “shortcuts”, your code may be reduced in reasonable amount of lines. Some reading about code optimization can be found here, this article extends optimizations.

Read the rest of this entry »

What fontName to use with embedding?

This article extends “Embedding fonts bold vs. black” post. Sometimes it may be tricky to guess correct fontName with your font. Compilator works with different fontNames than what Flash IDE shows you. Lets say you want to embed fonts from .swf (library) file into .css file. In Flash IDE, properties panel for TextInput, character Family and Style stand for something totaly different that what you gonna need with correct fontName value later in .css file. In fact solution is very easy.

Read the rest of this entry »

Quick tip: … (rest) parameter

Here is a quick tip for using … (rest) parameter in your code. It sometimes happens that you want to have your function called multiple times, when different events dispatched or even without any event dispatched. Properly you would create callback/handler functions for all events, that would call the required function. Or you can use … (rest) parameter to bypass arguments casting.

priate function init():void
{
    addEventListener(Event.ENTER_FRAME, doSomething);

    var timer:Timer = new Timer(500);
    timer.addEventListener(TimerEvent.TIMER, doSomething);
    timer.start();

    callLater(doSomething);
    doSomething();
}

private function doSomething(... rest):void
{
// whatever
}

What Else You Should Know About Flash/Flex (update)

Even for seniors, there is still something to learn. I have chosen those great articles that would help you improve your knowledge about flash and flex and something littlebit behind the scenes.

Flex SDK coding conventions and best practices

This document lays out the coding standards for writing open-source Flex framework components in ActionScript 3. Adhering to these standards makes the source code look consistent, well-organized, and professional…

The Flex Non-Docs (previous site)

For all those secrets in Flex, those that aren’t known, those that aren’t published, or those that are just hard to find for some unknown reason, like percentWidth…

10 Things Every Senior Flash Developer Should Know

John Lindquist interviewed quite a few Flash/Flex developers as potential employees for Roundarch. The hard part for him is knowing exactly what questions to ask to be able to gauge a Flash dev’s skill level…

Nemo 440 – ActionScript 3 disassembler in AIR

Nemo 440 is free ABC code diassembler implemented as AIR-based application. Nemo 440 can read SWF files compiled with Flex 2/Flex 3/Flex 4 and translates ActionScript 3 byte code to more understandable text dump.

Update (Feb 12, 2010): AS3 hidden treasure in the mm.cfg

Revealing and documenting many Flash secrets hidden in mm.cfg file.

Embed Bitmap text into Flex

pixelFont

This article helps you embed pixel sharp fonts (without antialias) into your flex apps. As far as I know you wouldn’t be able to do this just by direct embedding local font file into your flex application, but you gonna need Flash CS4 authoring tool. There are two ways you can make Bitmap text work in flex. You can use pure css method or actionscript method. The downside of both methods is, you have to define and export from flash exact font size you are going to use.

Read the rest of this entry »

Binding in flex 3 magic in curly brackets

A lot of things have been said about binding (read basics). But to really understad what is going on behind the scenes lets do some experiments… First to mention is, the curly brackets magic does not work as BindingUtils does. BindingUtils lets you bind only one setter, but curly brackets lets you bind whole logic.

  • test1 – notice constant C1 in (binding constants is valid, and is working without Binding meta tag) and some plus inside expression “Working: {C1 + ‘ ‘ + v2}”
  • test2 – you can use more curly brackets expressions within one attribute “Working: {C1} {v2.toUpperCase()}”
  • test3 – even if you expect function result from your bindable variable it will work “Working: {C1} {uberFunc(v2)}”
  • test4 – notice some math functions here “Working: {v3 + v3}, {Math.pow(v3, 2)}”
  • test5 – simple Object and Array is not working (nor ArrayCollection using brackets) “Not working: {v4.v}, {v5[0]}, {v6[0]}”
  • test6 – getItemAt() on ArrayCollection works just smooth “Working: {v6.getItemAt(0)}”
  • test7 – once you use curly brackets you can not remove binding, but binding can be unbinded when binded by BindingUtils. (both getter and setter in bindProperty must be public!)
  • test8 – binding works when correct event is dispatched “{foo.gimmeSometing()}”
  • test9 – binding with ternary operator is super powerful “{v2 == ‘world’ ? ‘is world’ : ‘not world’}”

Read the rest of this entry »

9 slice scale for bitmaps in flash (no hacks)

9slice_result_flash

9 slice scale is a nice feature, but when you try to use 9 slice in flash ide on MovieClip with bitmap, you will notice a problem. It will just not work as expected. So first thing you try is “Break Apart”, with same result. You are almost there, but due to the solution is not documented you need a small hint: After break apart (ctrl + b), select each of nine slices one by one and group (ctrl + g) individually and voilà its done. See snapshot and result images…

Read the rest of this entry »

ActionScript 3 Performance Testing

Have you ever been curious about performance (benchmark) tests in ActionScript 3? Here are some results gathered from gskinner blog.

Performance testing on loops (read more).

method............................................................. ms
for (var i:uint=0; i<length; i++)                                   40
for (var i:uint=length; i>0; i--)                                   40
var i:uint = 0; while (i < length) i++;                             40
var i:uint = length; while (--i)                                    52
var i:uint = 0; do {} while (++i < length);                         68
var i:uint = length-1; do {} while (i--);                           68
for (var b:* in arr)                                               224
for each (var b:Boolean in arr)                                    216
for each (var b:* in arr)                                          200
for each (var b:* in arr) var c:Boolean = b as Boolean;            686
arr.forEach(arrForEachF)                                          1446

Performance testing on operations with Number, int, uint. (read more)

Assignment (a:TYPE = 0) ......... min - max (ms)
int:                               24 -  45
Number:                            24 -  36
uint:                              25 -  37
Assignment (a:TYPE = 0.5) ....... min - max (ms)
int:                               56 -  83
Number:                            26 -  43
uint:                              57 -  92
Division (a:TYPE = i/2) ......... min - max (ms)
int:                               60 - 105
Number:                            34 -  64
uint:                             184 - 278
Multiplication (a:TYPE = i*2) ... min - max (ms)
int:                               78 - 129
Number:                            39 -  64
uint:                             207 - 280
Addition (a:TYPE = i+2) ......... min - max (ms)
int:                               31 -  49
Number:                            44 -  55
uint:                              85 - 113
Bitshift (a:TYPE = i<<1) ........ min - max (ms)
int:                               31 -  63
Number:                            61 - 114
uint:                              71 - 130