Archive for December, 2008

Working in 3D with Flash Player 10/ActionScript 3 101

December 30, 2008

1. The coordinate system in Flash Player is right handed. For angles in ActionScript, when dealing with DisplayObject properties such as rotationX, use degrees; when dealing with maths such as Math.atan, Math.cos etc., use radians.

2. Gumbo SDK and Flex SDK 3.2 have slightly different implementation on coordinates and layout. A UIComponent with the z depth appears much closer in Flash Player if complied with sdk 3.2 than if complied with Gumbo.

3. When Flash Player applies 3D, “the DisplayObject is drawn into a bitmap, then a vector rectangle (1 quad, not 2 triangles) is created with the dimensions of that bitmap, using that bitmap as a fill. The quad edges are then transformed into 3d world space and projected into 2d stage/global space.” (here, see comment #11.)

4. The Matrix3D and PerspectiveProjection class are used to manipulate visaul classes.

DisplayObject.perspectiveProjection.projectionCenter, which is the type of flash.geom.Point, determines the displayObject’s projection center. If you are setting root (which is  [_Main_mx_managers_SystemManager]) object’s projection center, be aware that  (a) root may be be available when the display list of the component is validated; (b) DisplayObject.transform.perspectiveProjection returns null unless you explicitly instantiate and assign a new PerspectiveProjection object to it, like so:

var pp:PerspectiveProjection = new PerspectiveProjection();
pp.projectionCenter = new Point(width/2, height/2);
contentPane.transform.perspectiveProjection = pp;//this prevents the mouse events from emitting from the contentPane

UICompnent(contentPane).parent.transform.perspectiveProjection = pp;//this doesn’t have the mouse event masking problem.

UICompnent(contentPane).root.transform.perspectiveProjection = pp;//this doesn’t have the mouse event masking problem.

Setting the projection center to the root object will affect the 3D projection of all the displayObjects on the stage, while setting the projection center for individual displayObjects will only affect those objects themselves and their children.

AppendRotation/PrependRotation/AppendTranslation/PrependTranslation apply transformation incrementally to a DisplayObject. To apply transformation in absolute values, use .transform.Matrix3D.recompose() instead:

import flash.geom.Vector3D;
import __AS3__.vec.Vector;

var v3:Vector.<Vector3D> = new Vector.<Vector3D>(3);
//The following line is required to avoid error in Gumbo but not sdk 3.2:
//items[id].transform.matrix3D = new Matrix3D();
v3 = item.transform.matrix3D.decompose();
var ang:Number = (-v + 180 +(centralAngle/numItems)*id)*Math.PI/180;
v3[0] = new Vector3D(0, radius*Math.sin(ang), radius*Math.cos(ang));

To quickly manipulate an object in 3D, you can also use a Matrix3D object as follows:

var matrix:Matrix3D  = new Matrix3D();
matrix.appendRotation(1, Vector3D.YAXIS);
//instantiate .transform.matrix3D property if it doesn’t have one yet:
myObject.z = 0;
myObject.transform.matrix3D = matrix;

“To make something not be 3d anymore, set its .transform.matrix to a non-null value (or set .transform.matrix3D to null)”.

5. Some quirks of Flex 3 (“Moxie”):

If you apply an effect (say mx.effects.Blur) on a UIComponent that has its transform.perspectiveProjection.projectionCenter set explicitly already, FP will first remove the .tranform.Matrix3D on the component before applying the effect. This makes the target look like it’s jumping back to the origin. (Tested on SDK 3.2).

If you try to set projectionCenter (of its parent) and z properties on a UIComponent at the same time, your component won’t be able to receive any mouse event at all. This seems to be fixed on Gumbo.

mx.effects.Move seems to have its own projection center set and ignores your setting for its (parent’s) projectionCenter.


Targetting Flash Player 10 on Flex development

December 20, 2008

Here: Targeting Flash Player 10 (UPDATED for FB 3.0.2)

One thing to remember is to “Do whatever you need to do in your OS to ensure SWF files are default opened by the new Flash Player 10 debug app”.

I didn’t realize that my system (Windows XP SP 3, with Flash CS3 and Flex Builder 3.0.1/2+sdk 3.2/4.0 installed) continued to associate FP 9 with .swf type even after I had had FP 10 installed (debug and non-debug); I was quite confused though whether it happened at the OS level (for all apps) or  only for Flex Builder. Anyhow during the compiling process, FB kept on complaining and all the complaints seemed to associate with the Player verison issue. Even confusingly the compiler or the runtime said it couldn’t write UIComponent.rotationX property, which seemed to signal that the SWF was running inside version 9, while a trace for “flash.system.Capabilities.version” did get “WIN 10,0,12,36”!

Long frustrations short, the following are a couple of ways I found worked for me:

1. Exploerer > “Tools” > “Folder Options” > “File Types” > Scroll down the “Registered File Types List” and choose “SWF Flash movie” > Choose “Advanced” button on the bottom > “Edit” > Substitute the path in the “Application used to perform action” to the path to FP10 in your system. This way FP10 shows up on top of FP9 in the “Recommended Programs” list!.

1. Uninstall/Reinstall Flash Player 10 that are downloaded from Adobe site. Restart.

2. (Bullet-proof) Add “-target-player=10.0.0” to the “Additional compiler arguments” before compiling.

FB stops hinting Flash Player 10 functions?

1. Make sure you are using playerglobal.swc from an SDK that targets FP10, which means SDK should be v3.0.1+(?) and it should reside in something like “frameworks/libs/player/10/playerglobal.swc”;

2. “Source attachement” option for the playerglobal.swc is “None” and the “Link type” should be “External”. Here.

Here are some links about code hinting in FB (SDK 3 vs. 4?) and the new Vector class (typed Array):

Kirupa forum: Using vectors (typed arrays) with Flex 3?

Mike Chambers: Using Vectors in ActionScript 3 and Flash Player 10

Access stage instance in Flex/AIR/Flash

December 17, 2008

In Adobe AIR:

If you want to access stage instance from the start-up of your application (usually to do some set-up stuff), you could access it with the “windowComplete” event for the WindowApplication tag (equivalent of “applicationComplete” for Application tag for Flash Player application), like so:

<mx:WindowedApplication xmlns:mx=”;



private function initApp():void
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

In Flex (targetting Flash Player):

I believe you could do so within a “applicationComplete” handler.

In Flash:

Stage Access/Outside In

Loading data in Flex 101

December 16, 2008

First of all, all the Flash ways of loading data are available for Flex.

Then there are some Flex specific ways (depending on whether it’s Adobe Flash Player application or Adobe AIR application).

Using the HTMLControl Class in Adobe AIR to parse HTML as a data source

Memery allocation in C++/C

December 14, 2008

Dynamic memeory allocation in C++

Why C has pointers

Class declarations and definitions

December 14, 2008

0. A declaration which reserves memory is more properly known as a definition in C++.

1. Class declarations is different than class definitions in C++.

2. Class declaration is what you do in a header file. The “class declaration block” consists of the keyword class, followed by the name of the object, a pair of braces, and a semi-colon. It basically declares class interface, with function prototypes and static constants.

3. Non constant static members (i.e., static class variables) need to be declared but not initialized, inside a class (header), like so:

static int totalStudents;

The member has to initialized outside the class (header) and inside a class instance definition (i.e., the source code file, *.cpp), like so:

int GradeBook::totalStudents = 10;

4. When you initializing a class instance whose constructor takes no parameters, you do it like this:

GradeBook gb;

as opposed to

GradeBook gb();

But if the constructor does take parameters, you do it like this:

GradeBook gb("C++ Programming");

// GradeBook2.h
class GradeBook
GradeBook(bool foo);
void countGrades();
void displayResults();
//const int TEST = 12; //wrong.
static int totalStudents; // non constant static member needs to be declared inside class here.
static const int TOTAL_STUDENTS_SCHOOL = 200; //constants has to be declared and initialized here inside class.s
//static const int TOTAL; // wrong. will compile but get runtime error whenever this member data is referred. "undefined reference to `GradeBook::TOTAL'"
int passedTotal;
int failedTotal;

// GradeBook2.cpp
#include "gradebook2.h"
#include <iostream>;
using namespace std;

int GradeBook::totalStudents = 10;
//int GradeBook::TOTAL = 300; //error: declaration of ‘const int GradeBook::TOTAL’ outside of class is not definition

GradeBook::GradeBook(bool foo)
cout<<“you said it’s “<<<“\n”;

cout<<“you did NOT say anything\n”;

cout<<“bye bye!\n”;

void GradeBook::countGrades()
cout<<“this is a gradebook! and we have a total of “<< TOTAL_STUDENTS_SCHOOL <<” students in school!\n”;
//cout<<“the total is “<< TOTAL <<” though!\n”;
//cout<<“Total students in the book are “<<<“/n”;//test

void GradeBook::displayResults()

// runner.cpp
#include "gradebook2.cpp"
#include <iostream>;
using namespace std;

int main()
//int GradeBook::totalStudents;//error: invalid use of qualified-name ‘GradeBook::totalStudents’
GradeBook gb(true);
GradeBook gb2;

Some links:

C++ static varialbles

Declaring a class

Class declaration vs. class definition

The C++ ‘const’ Declaration: Why & How

How to declare/initialize a constant in a class?