by Darrick Brown

 Comments (3)

Created

May 25, 2006

This article covers aspects of ActionScript 3 that would be helpful to C/C++ application engineers transitioning to application development in Flex and Flash.

I’ve used C/C++ through most of my educational and professional career. I’ve also done a respectable share of Javascript and Perl. ActionScript 3 could be viewed as an interesting blend of features from all of these languages. Actionscript 3 conforms to the ECMAscript 4 spec. So, it is a standard languange. In addition to the standard language syntax, AS3 contains a custom class framework for working with Flex/Flash.

The following are areas in the AS3 language that I personally found interesting.

Type declarations

Compared to C/C++, the first syntax oddity that you’ll notice is how AS3 declares it’s variable types. All type declarations are in post-fix notation. For example, in C you would define a function like:

int myFunction(char *str);

In AS3 this same function declaration looks like:

function myFunction(str:String) : int

 

Typecasting

In AS3, think of typecasting like calling the constructor of the type. Functionally, this isn’t what happens, but the syntax is what it appears to be doing.

In C: int i = (int)somefloat;
In AS3:    var i:int = int(somefloat);

If you see what appears to be something calling the constructor of a class or type but it does not use “new”, it’s not calling a constructor. It’s performing a typecast. For example:

var foo:SomeClass = SomeClass(someObject);
var bar:SomeClass = new SomeClass(someObject);

The first line is typecasting “someObject” into SomeClass. The second line is creating a new SomeClass object, passing “someObject” as a parameter to the constructor. This subtle difference can have wide ranging effects (new object vs. reusing an existing object, etc.).  Depending on what the class constructor takes as a parameter, it is possible that both the typecast and the constructor would compile with no errors/warnings in all situations.  So, be careful.  The difference between a typecast and a new object is just the "new" keyword.

 

Variable scope

Variables are scoped to the function. ActionScript employs a system called "variable hoisting", which implicitly pulls all variable declarations in the function (even ones in nested blocks) to the top of the function at compile time.  For example:

public function doSomething() : void{var foo:int = 4;if (foo){var bar:int = 2;}}

With viable hoisting, all declared variables in a function are moved to the top of the function block at compile time. In the above example, the compile time result looks like:

public function doSomething() : void{var foo:int;var bar:int;foo = 4;if (foo){bar = 2;}}

Note that the "bar" variable is now in scope for the entire function.  This subtle variable handling in AS3 may lead to unintended situations since any use of the "bar" variable before the "if(foo)" block is now valid, even though it is not declared until inside the if() block.  AS3 will complain if you declare the same variable more than once in the same function, but it won’t complain if you use a variable before it’s declared.

void *

In AS3, you can use the wild card type to mimic the "void *" type.  For example, say you have a factory object that can return objects of many types.  This can be implemented as such:

public function wildcard() : void{var anything:* = ObjectFactory.getData();}

Run time type checking

Since you can pass things around as anything using the wildcard type (void *), you need a way to check the type of the object at runtime. To do this, you can use the “is” directive:

public function doSomething() : void{var something:* = getData();if (something is String){// handle string logic}else{// do something else}}

This allows for runtime type checking which allows your application to perform different logic depending on what the given object is.

No function overloading

There is no function overloading in AS3. However, you can implement a system that mimics function overloading. For example, in C++ you might have some function definitions like:

int doSomething(int i);int doSomething(char *str);

In AS3, you can’t overload a function, but the language allows you to make use of the type wildcard “*” and use the "is" directive as a way of performing runtime checks and branching based on what was passed in. For example:

function doSomething(obj:*) : int{if (obj is int)// do int stuffelse if (obj is String)// do string stuffelse// what type did you give me?}

No operator overloading

AS3 has no way to override the meaning of “+”, “=”, or any other operator. The closest functionality is the get/set member accessors that you can declare to handle the getting and assignment of class members.

Everything is an object

All types within AS3 are derived classes of the base class “Object”. Even if a class does not “extend Object”, it still does. This means when you pass anything to a function, you are passing that data by reference (pointer) in all cases. If the called function modifies the object you passed, your version of the data will be modified as well.

However, there are some exceptions. Basic types like int, Number, and String are objects as well, but their implementation performs reference counting to make them behave like stack objects. If the called function simply assigns new values to variables of these basic types, the data in the caller function does not get modified.

Only one packaged class per file

When implementing AS3 classes, you can only have one class definition per package declaration per AS file. For example:

MyClass.as

package my.class.package{public class MyClass{public function MyClass(){...}}}

Fig. 1

MyClass.as

package my.class.package{public class MyClass{public function MyClass(){...}}}class MyPrivateClass{public function MyPrivateClass(){...}}

Fig. 2

MyClass.as

package my.class.package{public class MyClass{public function MyClass(){...}}public class AnotherClass{public function AnotherClass{...}}}

Fig. 3

Fig. 1 and 2 are valid in AS3. Fig. 3 is not valid. AS3 does not allow you to have more than one class inside a package declaration and you can only have one package declaration per file. The interesting thing to note is that the “PrivateClass” in Fig. 2 is only accessable to code inside that file. Outside that file, the “PrivateClass” is an unknown type. You can use the construct in Fig. 2 to hide implementation classes from the rest of the world.

Being limited to one public class per file may not be a huge problem, but it may come as a logistical hurdle if you are expecting to define multiple classes within the same file. Plan your file structure accordingly.

virtual functions

The major difference between AS3 and C++ when it comes to inherited functions is the fact that the functions in a derived class do not override the base class functions unless you declare a method as “override“. Without this declaration, the base class version of the function will be called in all cases.

You can think of this as almost the opposite of C++’s “virtual” declaration. In C++, once a function is declared virtual, that function is automatically virtual for all derived classes regardless if the derived class declares it as virtual. In AS3, the derived class controls what functions are “virtual” (overridden).

dynamic_cast

In AS3, you will find yourself dealing with interfaces. These are similar in concept to abstract base classes in C++. Objects that implement an interface will be passed around as instances of that interface. So, given an object of a specific interface, how do you get the object as an instance of its subclass? You use the “as” functionality. For example:

var someInterface:ISomeInterface = factory.getSomeInterface();var someClass:SomeClass = (someInterface as SomeClass);

If "someInterface" is actually an instance of "SomeClass" or a derived class of "SomeClass", the variable "someClass" will be a reference to that object.  If "someInterface" is an instance of some other class, the "someClass" variable will be null.

Raw character strings

AS3 has a primative string type named “String”. You can access the characters of the string and do things with them, but what if you want access to the raw ASCII or UTF8 bytes? The easiest way to do this is via the ByteArray() class. You can write a string into a ByteArray object and then pull it back out as raw bytes.

 

Well, I hope this is useful to anyone making the transition to Flex/ActionScript from C/C++. If you have any questions, found an error in my assessment, or just want to say something, please leave a comment. We’re trying to make Flex development as easy as possible for everyone and any feedback is much appreciated.

Thanks!

COMMENTS

  • By Paul Robertson - 7:30 PM on May 31, 2006   Reply

    I just wanted to add one note about the as operator described above in the section titled “dynamic_cast”:The as operator, which is used to cast an object to a type at runtime, can be used not only to cast an instance of an interface (as in the example above) but also to upcast or downcast any type to a possibly related type (i.e. a subclass or superclass). So in the code below, either of the as statements would properly cast the instance:public class ParentClass {}public class ChildClass extends ParentClass {}…var child1:ChildClass = new ChildClass();// note this is a ChildClass instance, upcast as ParentClassvar parent1:ParentClass = new ChildClass();…var parent2:ParentClass = (child1 as ParentClass);var child2:ChildClass = (parent1 as ChildClass);Of course, the second as statement only works because parent1 was declared as ParentClass but instantiated as a ChildClass instance.For me personally I think of is and as as a pair of related operators; one tells you whether something is an instance of a type, the other one tells you and casts the object all in one operation.(Sorry about the short note that turned into a long note. I’m sure you already know all of this anyway, but I thought it might not be precisely clear to everyone, based solely on the context of the article.)In any case, thanks for the articles, and I’m excited to read more!

  • By Jone - 7:03 AM on July 6, 2006   Reply

    hi,I can not understand the code by using “:” symbol.I think it has compile time error. Is there any one who could explane the use of this “:” Symbol in the code for this article?For instance we have the following code on the article :-In C: int i = (int)somefloat;In AS3: var i:int = int(somefloat);jone,

  • By Darrick Brown - 12:07 PM on July 7, 2006   Reply

    The formatting of the code is making this confusing. I apologize. I’ve updated the post to make this more clear.The “In C:” and “In AS3:” are not code. They are merely textual labels. The relevant code is:int i = (int)somefloat;var i:int = int(somefloat);Where the first line is in C/C++ and the second line is AS3. They are equivelent statements in the two languages.

ADD A COMMENT

 

Buy Aciclovir Cheap Aciclovir Order Risperdal Order Risperdal Buy Risperdal Cheap Risperdal Silagra dosage Order Silagra Buy Silagra Cheap Silagra Vasotec dosage Order Vasotec Buy Vasotec Cheap Vasotec Buy Tadalis SX Order Tadalis SX Buy Tadalis SX Cheap Tadalis SX Dipyridamole 100 mg Order Dipyridamole Buy Dipyridamole Cheap Dipyridamole Rosuvastatin dosage Order Rosuvastatin Buy Rosuvastatin Cheap Rosuvastatin Cheap Diovan Order Diovan Buy Diovan Cheap Diovan Cheap Cholestoplex Order Cholestoplex Buy Cholestoplex Cheap Cholestoplex Order Cialis Super Active Order Cialis Super Active Buy Cialis Super Active Cheap Cialis Super Active Depakote dosage Order Depakote Buy Depakote Cheap Depakote Revia dosage Order Revia Buy Revia Cheap Revia Aciclovir 200 mg Order Aciclovir Buy Aciclovir Cheap Aciclovir Dostinex dosage Order Dostinex Buy Dostinex Cheap Dostinex Order Bystolic Order Bystolic Buy Bystolic Cheap Bystolic Buy Abilify Order Abilify Buy Abilify Cheap Abilify Viagra Sublingual 100 mg Order Viagra Sublingual Buy Viagra Sublingual Cheap Viagra Sublingual Lopid dosage Order Lopid Buy Lopid Cheap Lopid Actoplus Met dosage Order Actoplus Met Buy Actoplus Met Cheap Actoplus Met Cheap Viagra Order Viagra Buy Viagra Cheap Viagra Buy Micardis Order Micardis Buy Micardis Cheap Micardis Clomid 25 mg Order Clomid Buy Clomid Cheap Clomid Buy Diclofenac Gel Order Diclofenac Gel Buy Diclofenac Gel Cheap Diclofenac Gel Buy Glucotrol XL Order Glucotrol XL Buy Glucotrol XL Cheap Glucotrol XL Cheap Aricept Order Aricept Buy Aricept Cheap Aricept Diarex 30 caps Order Diarex Buy Diarex Cheap Diarex Buy Sarafem Online Order Sarafem Buy Sarafem Cheap Sarafem Buy Zebeta Order Zebeta Buy Zebeta Cheap Zebeta Buy Suhagra Order Suhagra Buy Suhagra Cheap Suhagra Cefixime 100 mg Order Cefixime Buy Cefixime Cheap Cefixime Zyvox 600 mg Order Zyvox Buy Zyvox Cheap Zyvox Order Vasodilan Order Vasodilan Buy Vasodilan Cheap Vasodilan Cheap Myambutol Order Myambutol Buy Myambutol Cheap Myambutol Order Noroxin Order Noroxin Buy Noroxin Cheap Noroxin Avapro 300 mg Order Avapro Buy Avapro Cheap Avapro Order Elavil Order Elavil Buy Elavil Cheap Elavil Diarex dosage Order Diarex Buy Diarex Cheap Diarex Cheap Hydrochlorothiazide Order Hydrochlorothiazide Buy Hydrochlorothiazide Cheap Hydrochlorothiazide Order Hytrin Order Hytrin Buy Hytrin Cheap Hytrin Silagra 100 mg Order Silagra Buy Silagra Cheap Silagra Sumycin dosage Order Sumycin Buy Sumycin Cheap Sumycin Bystolic dosage Order Bystolic Buy Bystolic Cheap Bystolic Buy Avalide Order Avalide Buy Avalide Cheap Avalide Cheap Nitroglycerin Order Nitroglycerin Buy Nitroglycerin Cheap Nitroglycerin Pravachol 20 mg Order Pravachol Buy Pravachol Cheap Pravachol Noroxin 400 mg Order Noroxin Buy Noroxin Cheap Noroxin Order Propranolol Order Propranolol Buy Propranolol Cheap Propranolol Order Calan Order Calan Buy Calan Cheap Calan Cheap Terramycin Order Terramycin Buy Terramycin Cheap Terramycin Aldactone 20 mg Order Aldactone Buy Aldactone Cheap Aldactone Female Viagra 100 mg Order Female Viagra Buy Female Viagra Cheap Female Viagra Pyridium dosage Order Pyridium Buy Pyridium Cheap Pyridium Cheap Prinivil Order Prinivil Buy Prinivil Cheap Prinivil Buy Amoxil Order Amoxil Buy Amoxil Cheap Amoxil Buy Moduretic Order Moduretic Buy Moduretic Cheap Moduretic Orlistat dosage Order Orlistat Buy Orlistat Cheap Orlistat Order Paxil Order Paxil Buy Paxil Cheap Paxil Cheap Inderal Order Inderal Buy Inderal Cheap Inderal Order Arjuna Order Arjuna Buy Arjuna Cheap Arjuna Buy Protonix Online Order Protonix Buy Protonix Cheap Protonix Cheap Clozaril Order Clozaril Buy Clozaril Cheap Clozaril Order Prometrium Order Prometrium Buy Prometrium Cheap Prometrium Cheap Diarex Order Diarex Buy Diarex Cheap Diarex Order Remeron Order Remeron Buy Remeron Cheap Remeron Buy Levitra Online Order Levitra Buy Levitra Cheap Levitra Cozaar dosage Order Cozaar Buy Cozaar Cheap Cozaar Buy Furosemide Order Furosemide Buy Furosemide Cheap Furosemide Buy Strattera Order Strattera Buy Strattera Cheap Strattera Yasmin 3.03 mg Order Yasmin Buy Yasmin Cheap Yasmin Cheap Januvia Order Januvia Buy Januvia Cheap Januvia Eurax dosage Order Eurax Buy Eurax Cheap Eurax Buy Atarax Order Atarax Buy Atarax Cheap Atarax Cheap Paxil Order Paxil Buy Paxil Cheap Paxil Cipro dosage Order Cipro Buy Cipro Cheap Cipro Vermox dosage Order Vermox Buy Vermox Cheap Vermox Roxithromycin dosage Order Roxithromycin Buy Roxithromycin Cheap Roxithromycin Buy Zestril Order Zestril Buy Zestril Cheap Zestril Extendaquin dosage Order Extendaquin Buy Extendaquin Cheap Extendaquin Buy Aciclovir Order Aciclovir Buy Aciclovir Cheap Aciclovir Benicar 40 caps Order Benicar Buy Benicar Cheap Benicar Cheap Antivert Order Antivert Buy Antivert Cheap Antivert Order Diflucan Order Diflucan Buy Diflucan Cheap Diflucan Robaxin dosage Order Robaxin Buy Robaxin Cheap Robaxin Buy Zenegra Order Zenegra Buy Zenegra Cheap Zenegra Cheap Aceon Order Aceon Buy Aceon Cheap Aceon Buy Extendaquin Online Order Extendaquin Buy Extendaquin Cheap Extendaquin Flomax 0.4 mg Order Flomax Buy Flomax Cheap Flomax Order Minocin Order Minocin Buy Minocin Cheap Minocin Order Chloromycetin Order Chloromycetin Buy Chloromycetin Cheap Chloromycetin Doxycycline dosage Order Doxycycline Buy Doxycycline Cheap Doxycycline Risperdal 3 mg Order Risperdal Buy Risperdal Cheap Risperdal Flonase dosage Order Flonase Buy Flonase Cheap Flonase Buy Lopressor Order Lopressor Buy Lopressor Cheap Lopressor Protonix 40 mg Order Protonix Buy Protonix Cheap Protonix Hydrochlorothiazide 12.5 mg Order Hydrochlorothiazide Buy Hydrochlorothiazide Cheap Hydrochlorothiazide Pyridium 200 mg Order Pyridium Buy Pyridium Cheap Pyridium Coreg dosage Order Coreg Buy Coreg Cheap Coreg Buy Elavil Order Elavil Buy Elavil Cheap Elavil Cheap Medrol Order Medrol Buy Medrol Cheap Medrol Seroquel dosage Order Seroquel Buy Seroquel Cheap Seroquel Cheap Cialis Super Active Order Cialis Super Active Buy Cialis Super Active Cheap Cialis Super Active Crestor 10 mg Order Crestor Buy Crestor Cheap Crestor Clozaril dosage Order Clozaril Buy Clozaril Cheap Clozaril Order Anafranil Order Anafranil Buy Anafranil Cheap Anafranil Order Xenical Order Xenical Buy Xenical Cheap Xenical Order Duetact Order Duetact Buy Duetact Cheap Duetact Cheap Glucophage Order Glucophage Buy Glucophage Cheap Glucophage Order Chloromycetin Order Chloromycetin Buy Chloromycetin Cheap Chloromycetin Doxazosin 2 mg Order Doxazosin Buy Doxazosin Cheap Doxazosin Levothroid 50 mcg Order Levothroid Buy Levothroid Cheap Levothroid Diarex 30caps Order Diarex Buy Diarex Cheap Diarex Cheap Vytorin Order Vytorin Buy Vytorin Cheap Vytorin Amaryl dosage Order Amaryl Buy Amaryl Cheap Amaryl Zyprexa 20 mg Order Zyprexa Buy Zyprexa Cheap Zyprexa Order LipoSafe Order LipoSafe Buy LipoSafe Cheap LipoSafe Cheap Avapro Order Avapro Buy Avapro Cheap Avapro Risperdal dosage Order Risperdal Buy Risperdal Cheap Risperdal Prometrium 200 mg Order Prometrium Buy Prometrium Cheap Prometrium Order Prilosec Order Prilosec Buy Prilosec Cheap Prilosec Buy Cleocin Order Cleocin Buy Cleocin Cheap Cleocin Order Viagra Soft Order Viagra Soft Buy Viagra Soft Cheap Viagra Soft Prometrium 200 mg Order Prometrium Buy Prometrium Cheap Prometrium Colchicine dosage Order Colchicine Buy Colchicine Cheap Colchicine Provera 10 mg Order Provera Buy Provera Cheap Provera Neurontin 300 mg Order Neurontin Buy Neurontin Cheap Neurontin Order Toprol XL Order Toprol XL Buy Toprol XL Cheap Toprol XL Cheap Shuddha Guggulu Order Shuddha Guggulu Buy Shuddha Guggulu Cheap Shuddha Guggulu Ranitidine dosage Order Ranitidine Buy Ranitidine Cheap Ranitidine Cheap Hyzaar Order Hyzaar Buy Hyzaar Cheap Hyzaar Order Lozol Order Lozol Buy Lozol Cheap Lozol Buy Mexitil Order Mexitil Buy Mexitil Cheap Mexitil Order Aricept Order Aricept Buy Aricept Cheap Aricept Abilify 20 mg Order Abilify Buy Abilify Cheap Abilify Cheap Toprol XL Order Toprol XL Buy Toprol XL Cheap Toprol XL Endep dosage Order Endep Buy Endep Cheap Endep Order Prilosec Order Prilosec Buy Prilosec Cheap Prilosec Yasmin 3.03 mg Order Yasmin Buy Yasmin Cheap Yasmin Order Silagra Order Silagra Buy Silagra Cheap Silagra Order Trimox Order Trimox Buy Trimox Cheap Trimox Singulair 5 mg Order Singulair Buy Singulair Cheap Singulair Cheap Singulair Order Singulair Buy Singulair Cheap Singulair Strattera dosage Order Strattera Buy Strattera Cheap Strattera Order Procardia Order Procardia Buy Procardia Cheap Procardia Buy Protonix Order Protonix Buy Protonix Cheap Protonix Buy Strattera Order Strattera Buy Strattera Cheap Strattera Cheap Finpecia Order Finpecia Buy Finpecia Cheap Finpecia Order Bupropion Order Bupropion Buy Bupropion Cheap Bupropion Cheap Chloramphenicol Order Chloramphenicol Buy Chloramphenicol Cheap Chloramphenicol Order Florinef Order Florinef Buy Florinef Cheap Florinef Order Provera Order Provera Buy Provera Cheap Provera Cheap Atacand Order Atacand Buy Atacand Cheap Atacand Cheap Yagara Order Yagara Buy Yagara Cheap Yagara Order Suprax Order Suprax Buy Suprax Cheap Suprax Lantus dosage Order Lantus Buy Lantus Cheap Lantus Ethionamide dosage Order Ethionamide Buy Ethionamide Cheap Ethionamide Buy Flagyl Order Flagyl Buy Flagyl Cheap Flagyl Celexa 20 mg Order Celexa Buy Celexa Cheap Celexa Midamor 45 mg Order Midamor Buy Midamor Cheap Midamor Cheap Vytorin Order Vytorin Buy Vytorin Cheap Vytorin Buy Atorlip Order Atorlip Buy Atorlip Cheap Atorlip Buy VPXL Order VPXL Buy VPXL Cheap VPXL Order Dilantin Order Dilantin Buy Dilantin Cheap Dilantin Buy Avapro Online Order Avapro Buy Avapro Cheap Avapro Levitra Plus 400 mg Order Levitra Plus Buy Levitra Plus Cheap Levitra Plus Cheap Valtrex Order Valtrex Buy Valtrex Cheap Valtrex Buy Eurax Order Eurax Buy Eurax Cheap Eurax Cheap Prednisone Order Prednisone Buy Prednisone Cheap Prednisone Order Duetact Order Duetact Buy Duetact Cheap Duetact Buy Singulair Order Singulair Buy Singulair Cheap Singulair Buy Coreg Order Coreg Buy Coreg Cheap Coreg Buy Mexitil Order Mexitil Buy Mexitil Cheap Mexitil Cheap Ampicillin Order Ampicillin Buy Ampicillin Cheap Ampicillin Order Singulair Order Singulair Buy Singulair Cheap Singulair Buy Levaquin Order Levaquin Buy Levaquin Cheap Levaquin Order LipoSafe Order LipoSafe Buy LipoSafe Cheap LipoSafe Zetia 10 mg Order Zetia Buy Zetia Cheap Zetia Order Amoxil Order Amoxil Buy Amoxil Cheap Amoxil Buy Clozaril Online Order Clozaril Buy Clozaril Cheap Clozaril Abilify 10 mg Order Abilify Buy Abilify Cheap Abilify Cheap LipoSafe Order LipoSafe Buy LipoSafe Cheap LipoSafe Order Retino A Cream Order Retino A Cream Buy Retino A Cream Cheap Retino A Cream Cheap Vasotec Order Vasotec Buy Vasotec Cheap Vasotec Renagel dosage Order Renagel Buy Renagel Cheap Renagel Viagra Professional dosage Order Viagra Professional Buy Viagra Professional Cheap Viagra Professional Order Cialis Order Cialis Buy Cialis Cheap Cialis Buy Cialis Order Cialis Buy Cialis Cheap Cialis Buy Acticin Order Acticin Buy Acticin Cheap Acticin Buy Yagara Online Order Yagara Buy Yagara Cheap Yagara Cardura 2 mg Order Cardura Buy Cardura Cheap Cardura Endep dosage Order Endep Buy Endep Cheap Endep Cheap Micardis Order Micardis Buy Micardis Cheap Micardis Order Antabuse Order Antabuse Buy Antabuse Cheap Antabuse Cheap Voltaren Order Voltaren Buy Voltaren Cheap Voltaren Hytrin 1 mg Order Hytrin Buy Hytrin Cheap Hytrin Buy LipoSafe Order LipoSafe Buy LipoSafe Cheap LipoSafe Clonidine 0.1 mg Order Clonidine