Transitioning from C++ to Actionscript using FlashDevelop

Transitioning from C++ to Actionscript using FlashDevelop

Transitioning from a C++ background to Actionscript is fairly easy, but some of the differences can be surprising, as well as aggravating.

In this article, I try to outline some of the differences and tips/ things to watch out for when coding in Actionscript 3.0 vs C++/ Java.

Variable Types

In C++, you have a variety of basic types to chose from.
In Actionscript, you have more clear choices:

int (any number without a decimal)
Number (any number with a decimal)
String (any character string)
Boolean (true / false)

Pretty nice, eh? No character strings, or trying to decide between using a float and a double here!
Example:

var my_age:int=104;
var electric_bill:Number =  216.29;
var favorite_food:String = "Pizza!";
var working_from_home:Boolean = false;

Pretty straight forward.

To debug out a variable, use the function: “trace()”.

Example:

var i:Number=200;
i*=(100*i+27/i-432.72*i);
trace("i = "+i);

Tracing out variables is used to check the value in them, and mainly used for debugging to see what is actually going on in your program if you run into problems.

Some notes about trace:

Combine strings with ‘+’
Example:
trace(“Your favorite food is: ” + fav_food + “, your age is: ” + age);

WATCH OUT for adding variables in a trace statement- it will convert them to strings.
Example:

var my_age:int = 104;
var your_age:int = 6;
trace("Our ages together add up to:"+my_age+your_age);
//outputs '1046'

the correct way to do it is to add parenthesis around any math.
Example:
trace(“Our ages together add up to:”+(my_age+your_age));

Some differences are:

For loops formats
For loops can be created in two ways – that classic c++ way:

for(var i=0;i < array.length ; i++)
     trace("value is = " + array[i]);

And a quicker Actionscript method:

for(var i in array)
     trace("value is = " + array[i]);

Classes have to be in individual files.
In C++, you can have a file with several related class definitions; in Actionscript, every class has to be in a new file.

Classes have to be a part of “packages”
Not hard when using FlashDevelop – it creates the basic file for you. But in flash, you have to remember to always have a class inside of ‘package{}’.
Example:
In root folder:

package
{
     public class Main()
     {
     }
}

In sub folder game/objects

package game.objects
{
     public class Tree()
     {
     }
}

Variables declared in a for loop exist outside of it
In C++, there is the concept of scope. If you create a variable as part of a for loop, function, object, ect, it stays inside the scope that it was created.
In Actionscript for for loops, this isn’t true.
Example:

for(var i=0; i < array1.length; i++)
trace(array1[i]);

for(var i=0; i < array2.length; i++)
trace(array[2]);

//compiler complains that 'i' is already defined previously.

Values are assigned by reference, not by value.
You’ll run into this often. You’ll make an object, assign a value to one of it’s variables from an existing object, and find that when you change the value for this new object, it will change for the previous one as well.
So you think you’ll be modifying “life value of new guy”, but in fact will be modifying both objects values.

To be able to code less strictly:

In flash develop, go to project properties-> compiler options
Change ‘Enable Strict Mode’ to ‘False’
Change ‘Show ActionScript Warnings’ to ‘False’
Change ‘Enable All Warnings’ to ‘False’

This will make it much quicker, but sloppier to code, and also remove some of Actionscript’s annoyances.

For instance, now you can write proper ‘for’ loops!
Example:

for(var i in array1)
     trace(array1[i]);

for(var i in array2)
     trace(array2[i]);

//Yay, no complaints about 'i' already existing

You can also code faster without having to declare types for variables, though this is lazy programming since you can’t tell easily what type a variable is, and can even assign it other types without complaint.

Example:

var my_name = "Chris";
var my_age = 104;

my_name = 60;
my_age = "dinosaur";

trace("my name is = "+my_mane);

From the first two lines, you can see it’s easier/faster to code since you don’t need to specify variable types.

Where this gets into bad programming is being able to assign a varible the “wrong type”- something that you didn’t mean to do, or doesn’t make much sense, like the next two lines.

Where this gets very time consuming/frustrating is when you spell a variable wrong, or do any typing error- flash won’t complain.
So you’ll spend an hour trying to figure out why “my_name” = 0, where in fact you accidentally are creating the variable “my_mane”, but flash sees it as legit.

Math Calculations

One thing I run into sometimes is having to do some basic trig, or thinking of using an exponential (like 2 squared = 4).

Actionscript has the built in library ‘Math’. for this.
Example:

var pi:Number = Math.PI;
var sin30:Number = Math.sin(30);
var three_squared:int = Math.pow(3, 2);

Rounding “Issues”

This is a simple “Watch out!”. If something is not working correctly, there is a good chance it’s because you used an “int” integer for an equation with decimals/’Number’ type.

I run into this when I create game sprite’s with x and y values as integers instead of numbers. This will lead to any calculations used with these to be cast as integers. So if you see some weird math happening, try casting the integers as Numbers ( to cast, just surround variable with parenthesis and the type to cast: Number(table_legs);).

Bookmark the permalink.

One Comment

  1. Try this: Math.pow(-1,0.25);

Leave a Reply

Your email address will not be published. Required fields are marked *