Note: no assignment
See Chapter 2 for reference
Lecture:
- Miscellaneous Basics
- Variables and Data Types
- conditionals
- Loops
- Arrays
- Custom Objects
Last week we talked a lot about the division between older versions of ActionScript (AS1 & AS2) and the newest version of ActionScript (AS3), and how they were somewhat incompatible. We talked about how AS3 was re-written from the ground up, rather than just adding more functionality to AS2. We didn’t really talk too much about their similarities.
All (full version, i.e. AS1, AS2, and AS3) versions of ActionScript are based upon a scripting language standard known as ECMA-262. This is the same standard that JavaScript is based upon. The core language concepts really have not changed from one version of ActionScript to the next, in fact if you have used some JavaScript or older versions of ActionScript you will immediately see similarities. I would compare ActionScript 3 to dining in a 4 star restaurant, whereas ActionScript 2 would be dining in a family style restaurant, and ActionScript 1 is like going for fast food. You’ll get a meal at all 3, but how you behave at each restaurant changes. The changes between the language are about formality. The more proper you do things, the less chance you have for creating bugs, and when you do its easier to track them down and correct them.
The following concepts are core concepts to a large number of scripting and programming languages, and we will be relying upon them throughout the semester and beyond. There is no assignment tonight except to get familiar with the concepts presented in the previous bullet list. They will help keep your organized and speed you along in your projects. We are just getting familiar with these concepts tonight, we will practice them throughout the semester.
Miscellaneous Basics
Execution Order – this is typically top to bottom, left to right. A general rule of thumb
Use of the Semicolon; – this is technically not required in ActionScript, but signifies the end of a line. Good to use to allow your code to be readable to other programmers.
Evaluating an Expression -
would not be some scary solve for x algebraic equation, but a way to increment the value of x by 1.
Use of the Trace Statement – a good quick way to test and debug small parts of your code while writing ActionScript, we saw an example in last weeks class.
Variables and Data Types
Number: Any number (would also include negative and floating point values).
int: Any whole number (or integer) negative or positive.
uint: An unsigned integer (basically any positive whole number).
String: A word, many words, or a string of characters (can include numbers and symbols).
Boolean:
A value either true or false.
Array: a numbered list of values in a single variable.
Object: A named unordered list of values stored in a single reference (similar to an array, less rigid).
Conditionals
A conditional is the basic way to test and see if one something or many somethings are true or false. Its the core way we have of setting up rules in our code. If something is true, then do the following, otherwise do something else. We have 3 basic types of conditionals, if (and else), switch and the ? operator.
if
If Something proves true, then act accordingly, otherwise do something else.
Lets set up a few variables
//var a:Number = 10;
var b:String = "cool";
//var b:String = "hot";
var c:Boolean = false;
//var c:Boolean = true;
Test if a equals 1:
if(a == 1){
trace("a does = 1 !!!!!!");
}
Logical Operators:
AND (&&): Both items of the condition need to test true
if(a == 1 && b == "cool"){
trace("both are true!");
}
OR (||): Either item of the condition need to test true
if(a == 1 || b == "hot"){
trace("something is true, maybe both things!");
}
NOT (!): this item cannot be true.
if(a != 1){
trace("a does not = 1 !!!!!!");
}
switch
When you have to check against a large list of conditions, if/else if/else statements can quickly become cumbersome; and switch comes to the rescue.
switch (_letter) {
case "A" :
trace("you pressed A");
break;
case "a" :
trace("you pressed a");
break;
case "E" :
case "e" :
trace("you pressed E or e");
break;
case "I" :
case "i" :
trace("you pressed I or i");
break;
default :
trace("you pressed some other key");
break;
}
}
test("A");
? operator
var y:Number = 10;
var z = (x < 6) ? x : y;
trace ( "z = " + z )
Loops
for Loops
trace("i = "+i);
}
while Loops
while (num < 5){
trace(num);
}
-caveat: these looping statements will run in less than a frame. Although these are great for doing a bunch of iterative calculations really quickly, these do not work at all well for screen based updates (i.e. like moving a ball programmaticly across the screen). We will be looking into other ways to preform that later in the semester.
Arrays
-a numbered list
trace(myArray[1]);
//returns two in the output screen since the first element in the array starts with 0
Functions
trace function
conversion function
To convert from Farenheit to Celsius, use this equation:
C=(F-32) x 5/9
To convert from Celsius to Farenheit, use this equation:
F=(9/5 x C) + 32
return (F - 32)* (5/9);
}
return (9/5 * C) + 32;
}
trace(convertToCelsius(56));
Custom Objects
like an array, but named instead of numbered elements, elements are named and there is no particular order
myObject.Count = 20;
myObject.Color = "red";
trace("count: "+myObject.Count);
trace("color: "+myObject.Color);
