Quick tip: ExternalInterface.call is synchronous!

It is a little wonder to me how flash player communicates with JavaScript. Calls are fully synchronous, it works as those (fp and js) were fully integrated into each other. To simulate this lets test some simple flow:

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

Quick tip: embedding in Flex

In Flex application, you can embed your assets into 3 places: ActionScript, MXML, CSS. To embed into ActionScript use following syntax:

[Embed(source="assets/image.png")]
public var imageClass:Class;

[Embed(source="assets/library.swf", symbol="symbol1")]
public var symbol1Class:Class;

// flash library symbol alternative
[Embed(source="assets/library.swf#symbol2")]
public var symbol2Class:Class;

To embed into MXML use:

<mx:Image source="@Embed('assets/image.png')"/>
<mx:Image source="@Embed('assets/library.swf', symbol='symbol1')"/>
<mx:Image source="@Embed('assets/library.swf#symbol2')"/>
<mx:Button skin="{null}"/>

To embed asset into CSS file use syntax:

Button 
{
    upSkin: Embed(source="assets/image.png");
    overSkin: Embed(source="assets/library.swf", symbol="symbol1");
    downSkin: Embed(source="assets/library.swf#symbol2");
    selectedUpSkin:ClassReference(null);
}

@font-face 
{
    font-family: Copacetix;
    src: url("assets/copacetix.ttf");
    unicode-range:
        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 */
}

Embedding font to .as (.mxml):

[Embed(source="../arial.ttf", fontFamily="myArial")] 
public static const FONT_ARIAL:Class;
public static const FONT_ARIAL_NAME:String = "myArial";

[Embed(source="../fonts.swf", fontName="myCourier", fontWeight="bold|normal") ]
public static const FONT_COURIER:Class;
Font.registerFont(FONT_COURIER);

public function Test()
{
    var format:TextFormat = new TextFormat();
    format.font	= FONT_ARIAL_NAME;
    						
    var tf:TextField = new TextField();
    tf.embedFonts = true;
    tf.defaultTextFormat = format;
}

Read more about embedding assets into ActionScript best practices here. Read basics about embedding here.

Facebook profile album aid

In search of best practice to get your facebook default profile (archive) album I came to the official facebook api wiki page. It states there that it is possible to calculate aid (album id) from your uid (user id) by simple formula:

(uid << 32) + (-3 & 0xFFFFFFFF)

Well, it may seem simple, but notice 32 bit left shifting in first part! By this operation on uid, you go far beyond integer max value and flash will not give you correct results. So how to do that?

Read the rest of this entry »

Flex IFrame – Web browser in flash (update)

Clipboard02

You can not have a web browser inside your web flash applications right? Well, you can! This solution uses html iframe to generate fake build in browser. So the behaviour of the browser in flash is the same as the one you are previewing this flash in (it is the same one). The best part of it is, you can manipulate with the position and size of this browser directly from flash. sk.yoz.html.IFrame solution was inspired by Alistair Rutherford, www.netthreads.co.uk.

Read the rest of this entry »

Flash 3D Engine List

3d

If you are looking for 3D flash engine you have multiple open source choices. The best one offers modeling editors, so creating your scenes is click easy, or other animation / physics frameworks, pixel bender shaders etc. List of most popular 3d engines:

Read the rest of this entry »

Serialization

This classes let you serialize your ActionScript 3 Objects to ByteArray or String:

Read the rest of this entry »

ActionScript 3 Classes

You can always download classes used in posts here http://classes.yoz.sk/!

Update: ActionScript 3 singleton pattern

In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects (say, five). Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application. (wikipedia)

In search of the best actionscript 3 singleton pattern I came to this one. It uses private lock so user is not allowed to create new instance…

Read the rest of this entry »

private setter, public getter

It is possible to make private setter and public getter on actionscript classes. All you need is to use proper namespaces for getter and setter within the class.

Read the rest of this entry »