Actionscript 3 101: Introduction to Variables.

ActionScript 3 (AS3) 101 is a series that will cover the fundamental use of actionscript in the process of building interactive projects. We will start with an elementary overview of terminology and the basic elements that make things happen in actionscript. The first few posts in this series will be an extended version of AS3 101, pharm visit this site The First ActionScript I Ever Learned. If you feel you are able to jump in at that post and move forward than you will be able skip a lot of extra reading. These first few posts should enable someone with a very little to no knowledge of Flash itself to start building projects in AS3.

The most basic script element is the variable. It is best to describe it with an example.

x + 2 = y

Yes, purchase just like pre-algebra. x and y are variables, they can represent any number, y’s value is determined by the value of x. To write this equation in ActionScript we have to declare the type of elements these variables represent which are numbers.

var x:Number = 2;
var y:Number = x + 2;
trace(y); // compiler error

The word “var” tells the compiler that we are about to declare a variable for the first time. Following the name is a colon and the type of variable we are declaring. This seems like a lot of extra typing, but it helps the compiler find potential errors in your code.

Very important to note, this code will not compile in Flash ActionScript 3 because x and y are reserved for the x and y coordinates of the DisplayObject class. So lets take this sample and rewrite it into something that really works. Type the following into the Flash Actions palette.

?View Code ACTIONSCRIPT
var num:Number = 2;
var product:Number = num + 2;
trace(product); // 4

**Note: Below we will use some examples and variable names more than once. To avoid compiler errors please only do one example at a time into the Actions palette.

There are some examples where compilers errors are expected and will be noted in the comments. Just delete the offending code, which will be noted, and move on. If there are two examples in the Actions panel at once, there is a chance the same variable name will be declared twice which will also cause compiler errors.

This current revision will compile perfectly and produce a number 4 in the Output panel. Now lets do a little playing around to explore some of the more common variable types.

1. Let’s change the value of num to a decimal, something like 2.5. The result will now show in the Output panel as 4.5

?View Code ACTIONSCRIPT
var num:Number = 2.5;
var product:Number = num + 2;
trace(product); // 4.5

2. Now take the same equation and swap the variable types to “int” for integer instead of “Number”.

?View Code ACTIONSCRIPT
var num:int = 2.5;
var product:int = num + 2;
trace(product); // 4

Integers only work in whole numbers and ignore the decimal value, always rounding down. This is important because sometimes you want whole numbers and sometimes you do not. Integers can only represent a value range of -2,147,483,647 to 2,147,483,647 according to Adobe’s documentation, though I was only able to get 991,158,271 in my tests.

You can mix and match numbers and integers in an equation, but the interger will prevail and round down the product.

?View Code ACTIONSCRIPT
var num:Number = 2.8;
var product:int = num + 2;
trace(product); // 4

Here is another thing to try, integers will always resolve to a whole number value even if the a value is not assigned. Numbers must always have a value assigned or it will cause things to break.

Example 1:

?View Code ACTIONSCRIPT
var num:int;
trace(num); // 0

Example 2:

?View Code ACTIONSCRIPT
var num:Number;
trace(num); // NaN (not a number)

Example 3:

?View Code ACTIONSCRIPT
var num:Number;
var product:Number = num + 2;
trace(product); // NaN

Example 4:

?View Code ACTIONSCRIPT
var num:Number = 0;
trace(num); // 0

Strings:
Strings can represent characters or text, numbers can also be used in strings.
Example 1:

?View Code ACTIONSCRIPT
var str:String = "some text";
trace(str); // some text

To assign a place holder for a string use empty quotes, otherwise it will resolve to null .
Example 2:

?View Code ACTIONSCRIPT
var str:String = "";
trace(str) // (shows nothing, but better than null);

Example 3:

?View Code ACTIONSCRIPT
var str:String;
trace(str);// null

Booleans:
Represents true or false, resolving to false by default.
Example 1:

?View Code ACTIONSCRIPT
var bool:Boolean = true;
trace(bool);// true

Example 2:

?View Code ACTIONSCRIPT
var bool:Boolean;
trace(bool); // false;

Objects:
The object is a very flexible type, representing a Display Object like a MovieClip or Button as well as non visual container for data.

Combining Numbers and Strings:
In most cases mix matching variable types will result in a compiler error, but there are a few combinations that can come in handy. We already saw what happens when combining Numbers and integers. What about numbers and strings?

Combining numbers and strings are useful for naming conventions, dates, timers displays and many other things. There are a few rules or warnings to keep in mind though.

1. The result of a string/number combination must always be a string. Strings can include numbers but numbers can not include strings.

?View Code ACTIONSCRIPT
var numStr:String = "4";
var num:int = 2;
var combo1a:Number = numStr + num; // (delete)
trace(combo1a); // will not compile due to coercion error (delete).
 
 
var combo1b:String = num + numStr;
trace(combo1b); // 24 (notice the result was not 6, but concatenated 2 and 4)

Note: Remember, compiler errors are nothing to be afraid of, they happen to the most experienced coders. One of the most common is the coercion error, it pops up when two incompatible variable types come together. These errors can be a real frustration to new developers, so face your fears and make this important de-bugging feature your friend.

2. String first, as a general rule

?View Code ACTIONSCRIPT
var str:String = "moveclip";
var num:int = 2;
var combo2:String = str + num;
trace(combo2);// movieclip2

3. To display numbers with commas or zero digit place holders use a string.
Example 1:

?View Code ACTIONSCRIPT
var numComma1:Number = 2,000;
trace(numComma); // will not compile due to coercion error (delete)

Example 2:

?View Code ACTIONSCRIPT
var numComma:String = "2,000";
trace(numComma); //2,000
 
var numZero:int = 0;
var num:int = 2;
var combo3a:int = numZero + num;
trace(combo3a); // 2 (no zero place holder)
 
var numZeroStr:String = "0";
var combo3b:String = numZeroStr + num;
trace(combo3b); // 02

3. numbers convert to strings a lot easier the strings to numbers

?View Code ACTIONSCRIPT
var ZeroNumStr:String = "02";
var numCombo1:int = ZeroNumStr; // (delete)
trace(numCombo1); // will not compile due to coercion error (delete)
 
var numCombo2:Number = ZeroNumStr; //(delete)
trace(numCombo2); // will not compile due to coercion error (delete)
 
var numCombo1:int = int(ZeroNumStr);
trace(numCombo1); // 2 (forced string to integer)
 
var numCombo2:Number = Number(ZeroNumStr);
trace(numCombo2); // 2 (forced string to integer)

Try changing the value of ZeroNumStr to 4.2, the conversion still works as expected.

If the value of ZeroNumStr contains any commas or any other non numerical characters, the results will fail. The integer value will result in a 0 and the Number value will be NaN.

Where to go from here:
This may not seem like a big deal, wow! we have some numbers and strings showing up in an Output panel. But you now have a very strong foundation on the most fundamental programming tool you will use. Another important tool in your coding toolbox is writing out equations, here is a simple counter that you can experiment with. You can change the values of num1 manually or hook up some buttons. Try multiplication, division and even square roots. Add more variables to increase functionality.

?View Code ACTIONSCRIPT
var num1:Number = 0;
 
var product:Number = num1 += 1;
 
trace(product);

sample button code:

?View Code ACTIONSCRIPT
//add a button to the stage with the instance name of "btn1";
btn1.addEventListener(MouseEvent.CLICK, function(event:MouseEvent){ num1 += 1;trace(num1);});

Actionscript 3 101: Introduction to Variables.

ActionScript 3 (AS3) 101 is a series that will cover the fundamental use of actionscript in the process of building interactive projects. We will start with an elementary overview of terminology and the basic elements that make things happen in actionscript. The first few posts in this series will be an extended version of AS3 101, cialis 40mg The First ActionScript I Ever Learned. If you feel you are able to jump in at that post and move forward than you will be able skip a lot of extra reading. These first few posts should enable someone with a very little to no knowledge of Flash itself to start building projects in AS3.

The most basic script element is the variable. It is best to describe it with an example.

x + 2 = y

Yes, link just like pre-algebra. x and y are variables, hospital they can represent any number, y’s value is determined by the value of x. To write this equation in ActionScript we have to declare the type of elements these variables represent which are numbers.

var x:Number = 2;
var y:Number = x + 2;
trace(y); // compiler error

The word “var” tells the compiler that we are about to declare a variable for the first time. Following the name is a colon and the type of variable we are declaring. This seems like a lot of extra typing, but it helps the compiler find potential errors in your code.

Very important to note, this code will not compile in Flash ActionScript 3 because x and y are reserved for the x and y coordinates of the DisplayObject class. So lets take this sample and rewrite it into something that really works. Type the following into the Flash Actions palette.

?View Code ACTIONSCRIPT
var num:Number = 2;
var product:Number = num + 2;
trace(product); // 4

**Note: Below we will use some examples and variable names more than once. To avoid compiler errors please only do one example at a time in the Actions palette.

There are some examples where compilers errors are expected and will be noted in the comments. Just delete the offending code, which will be noted, and move on. If there are two examples in the Actions panel at once, there is a chance the same variable name will be declared twice which will also cause compiler errors.

This current revision will compile perfectly and produce a number 4 in the Output panel. Now lets do a little playing around to explore some of the more common variable types.

1. Let’s change the value of num to a decimal, something like 2.5. The result will now show in the Output panel as 4.5

?View Code ACTIONSCRIPT
var num:Number = 2.5;
var product:Number = num + 2;
trace(product); // 4.5

2. Now take the same equation and swap the variable types to “int” for integer instead of “Number”.

?View Code ACTIONSCRIPT
var num:int = 2.5;
var product:int = num + 2;
trace(product); // 4

Integers only work in whole numbers and ignore the decimal value, always rounding down. This is important because sometimes you want whole numbers and sometimes you do not. Integers can only represent a value range of -2,147,483,647 to 2,147,483,647 according to Adobe’s documentation, though I was only able to get 991,158,271 in my tests.

You can mix and match numbers and integers in an equation, but the interger will prevail and round down the product.

?View Code ACTIONSCRIPT
var num:Number = 2.8;
var product:int = num + 2;
trace(product); // 4

Here is another thing to try, integers will always resolve to a whole number value even if the a value is not assigned. Numbers must always have a value assigned or it will cause things to break.

Example 1:

?View Code ACTIONSCRIPT
var num:int;
trace(num); // 0

Example 2:

?View Code ACTIONSCRIPT
var num:Number;
trace(num); // NaN (not a number)

Example 3:

?View Code ACTIONSCRIPT
var num:Number;
var product:Number = num + 2;
trace(product); // NaN

Example 4:

?View Code ACTIONSCRIPT
var num:Number = 0;
trace(num); // 0

Strings:
Strings can represent characters or text, numbers can also be used in strings.
Example 1:

?View Code ACTIONSCRIPT
var str:String = "some text";
trace(str); // some text

To assign a place holder for a string use empty quotes, otherwise it will resolve to null .
Example 2:

?View Code ACTIONSCRIPT
var str:String = "";
trace(str) // (shows nothing, but better than null);

Example 3:

?View Code ACTIONSCRIPT
var str:String;
trace(str);// null

Booleans:
Represents true or false, resolving to false by default.
Example 1:

?View Code ACTIONSCRIPT
var bool:Boolean = true;
trace(bool);// true

Example 2:

?View Code ACTIONSCRIPT
var bool:Boolean;
trace(bool); // false;

Objects:
The object is a very flexible type, representing a Display Object like a MovieClip or Button as well as non visual container for data.

Combining Numbers and Strings:
In most cases mix matching variable types will result in a compiler error, but there are a few combinations that can come in handy. We already saw what happens when combining Numbers and integers. What about numbers and strings?

Combining numbers and strings are useful for naming conventions, dates, timers displays and many other things. There are a few rules or warnings to keep in mind though.

1. The result of a string/number combination must always be a string. Strings can include numbers but numbers can not include strings.

?View Code ACTIONSCRIPT
var numStr:String = "4";
var num:int = 2;
var combo1a:Number = numStr + num; // (delete)
trace(combo1a); // will not compile due to coercion error (delete).
 
 
var combo1b:String = num + numStr;
trace(combo1b); // 24 (notice the result was not 6, but concatenated 2 and 4)

Note: Remember, compiler errors are nothing to be afraid of, they happen to the most experienced coders. One of the most common is the coercion error, it pops up when two incompatible variable types come together. These errors can be a real frustration to new developers, so face your fears and make this important de-bugging feature your friend.

2. String first, as a general rule

?View Code ACTIONSCRIPT
var str:String = "moveclip";
var num:int = 2;
var combo2:String = str + num;
trace(combo2);// movieclip2

3. To display numbers with commas or zero digit place holders use a string.
Example 1:

?View Code ACTIONSCRIPT
var numComma1:Number = 2,000;
trace(numComma); // will not compile due to coercion error (delete)

Example 2:

?View Code ACTIONSCRIPT
var numComma:String = "2,000";
trace(numComma); //2,000
 
var numZero:int = 0;
var num:int = 2;
var combo3a:int = numZero + num;
trace(combo3a); // 2 (no zero place holder)
 
var numZeroStr:String = "0";
var combo3b:String = numZeroStr + num;
trace(combo3b); // 02

3. numbers convert to strings a lot easier the strings to numbers

?View Code ACTIONSCRIPT
var ZeroNumStr:String = "02";
var numCombo1:int = ZeroNumStr; // (delete)
trace(numCombo1); // will not compile due to coercion error (delete)
 
var numCombo2:Number = ZeroNumStr; //(delete)
trace(numCombo2); // will not compile due to coercion error (delete)
 
var numCombo1:int = int(ZeroNumStr);
trace(numCombo1); // 2 (forced string to integer)
 
var numCombo2:Number = Number(ZeroNumStr);
trace(numCombo2); // 2 (forced string to integer)

Try changing the value of ZeroNumStr to 4.2, the conversion still works as expected.

If the value of ZeroNumStr contains any commas or any other non numerical characters, the results will fail. The integer value will result in a 0 and the Number value will be NaN.

Where to go from here:
This may not seem like a big deal, wow! we have some numbers and strings showing up in an Output panel. But you now have a very strong foundation on the most fundamental programming tool you will use. Another important tool in your coding toolbox is writing out equations, here is a simple counter that you can experiment with. You can change the values of num1 manually or hook up some buttons. Try multiplication, division and even square roots. Add more variables to increase functionality.

?View Code ACTIONSCRIPT
var num1:Number = 0;
 
var product:Number = num1 += 1;
 
trace(product);

sample button code:

?View Code ACTIONSCRIPT
//add a button to the stage with the instance name of "btn1";
btn1.addEventListener(MouseEvent.CLICK, function(event:MouseEvent){ num1 += 1;trace(num1);});

iPhone Application (Game) Reviews.

We currently have 4 apps in the store, remedy 2 games, medications one lite version of a game and a companion app to the book The Business of Design by our founder Joe Desetto. Though we had plans to have at least one new app in the store and another one really close to being submitted by now, store I was pulled away from developing for the iPhone to work on other projects. I hope to be back to work soon.

Note: TapDots is now a free app and has done really well now with it’s gratis price, in the education category.

In the name of research and planning for new apps we downloaded and played some of the newer 99¢ games on our iPhones. Here are some brief reviews on a few of those apps.

Research or Play?:
Flight Control is like the gold standard in the 99¢ iPhone game market, it is what every game developer strives to put into the store. It is a simple, challenging and addictive game with great design and replay value. Unfortunately, like with all great things there are many copycats. There are a whole new line to line drawing/prioritizing games now, must are fun to play, but it is hard to enjoy them as much of the original. Too much of a good thing and all that….

Line Drawing Games:

Traffic Rush:
This was a fun game to play for a few minutes, it gets really hard really fast. More of a timing/flicking game, once the game gets going you only have a second to figure out which cars go first. The motorcycles are really hard to see, which I guess is an accurate representation of real life. The gameplay is simple, flick cars, trucks or motorcycles through the intersection before traffic coming from other directions smashes into them. You can also stop the vehicles for a couple seconds, but what fun is that, plus after a second or two they automatically start up again whether the coast is clear or not.
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=322423174&mt=8

Harbor Master:
A very good adaptation of the Flight Control gameplay, almost the same game with a different theme. What makes this game unique is you have to dock the ships, wait for them to unload, then send them back out to sea to open the port for the next ship. The larger ships move slower in this game while the little vessels zip about. As you guide more and more ships to safety up to 4 additional harbors can be unlocked. Another feature that some will really like is profiles, you can work on your own scores and levels then hand it to a friend who can start a new profile to save their own progress. The hardest part will be taking turns.

Sea Captain:
This game is basically Flight Control for the sea port, where three different colored ships have to be directed to the correctly colored dock. Each boat fades away as it enters the dock clearing the way for the next vessel. Sea Captain includes 6 challenges and 3 seas to navigate. The graphics are great and really give it that salty dog feel. The game offers a couple nice features like subs, subs are cool, and wreck tolerance. The game does not end on the first crash. You have to be careful how you draw you lines, and make sure the line that displays is accurate. There are rocks around the mouth of each dock as well as an island that will blowup your ship if they run into them.
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=320662253&mt=8

Castle Defense Games:

Archmage:
Combines Castle Defense and Line drawing. Like most Castle defense games, the game play is very repetitive, you just do same things more and more, faster and faster. More of a castle defense game than Line drawing game. Graphics are really nice, It has an in-app purchase feature, which kind of scares me. Between each level you use points to buy stuff upgrades and health. I have to wonder am I spending real money, like Microsoft Points on the xBox? I am taking a break from this game until my next recept from the iTunes store just to make sure.
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=318793454&mt=8

Knights Onrush:
I think this is the most fun castle defense game I have played. I know some of you will want to stay with the old-school Stick Wars, but this game has a lot more to it then just flicking enemies away from your castle. It starts out like any other castle defense game, infantry attacking your castle. Your job is to flick them away, toss them into a fiery pit or hang them on a hook before the breach your door. Points are earned by kills and you can upgrade your fortress with reenforced doors. You have eight days for each level. That’s right levels, once you complete a level you get to defend a new castle, each with extra powers or features that add a variety of ways of dispensing your foes. Also the the attacking armies will send Calvary as well as bombers to your door, again adding some variety and a tiny bit of tactics.
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=312494232

Tunneling Games:

I DIg It: ($1.99)
This is a great app to escape with, I really like the graphics and layout of all the weathered controls. It does get tedious though, in the campaign you have to earn $100,000 by digging up stuff, called Digg’ems from under the farm. It is important to make a mining plan because going after items one at a time will produce a maze a tunnels making it harder to get back to the surface before your fuel runs out. The problem with this campaign is you have 3 hours, not game world hours, litteraly 3 hours to complete the challenge. I am about an hour and a half into the challenge after a few attempts. My mining plan is solid so now the game is really repetitive, digging the same old things over and over again occasionally finding something new. Dropping down to over a 1000 feet, I wonder how far down this thing will go?
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=314864297&mt=8

Number Games:

Nine Gaps:
This game is a mixed feeling of fun and homework. Even though it feels like school I have played it many times. The game goes like this, 9 squares (3 rows, 3 columns) that must be filled with nine numbers. They are separated by mathematical signs and must equate to the numbers on the left of each row and the bottom of each column. I like puzzle games, this is kind of a puzzle game, but mostly a math game. Like I said before, sometimes you may think to yourself, what I am doing play this game, it feels like homework.
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=317223443&mt=8

Big Top Ten:
A simple game with a old timey big top theme. Drag your finger around the grid selecting strings of numbers that add up to 10. The longer the string of numbers, the more points you earn. Bonus modifiers popup when lots of numbers are used in a string to make the game more interesting.
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=320461424&mt=8

All the games we tested were well done and definitely worth the 99¢ price tag.

What are some of your favorite iPhone games?

Technorati Tags: , , , ,

One Reply to “iPhone Watch August 2009”

Leave a Reply