03programming is ultimately all about giving instructions to the computer. these instructions must be precise, employing specific values to indicate exactly how long or how much or how often, etc.
a distinct instruction to the computer is called a statement. it is terminated with a semi-colon (;).
x = 50;
but as in math, the statements we are typically interested in are more complicated than what we can trivially express directly, so we compose expressions and assemble them together to form statements of the complexity we require.
my tip equals fifteen percent of the total bill. (my tip) equals (fifteen percent) of (the total bill). myTip = .15 * billTotal();
expressions are built of three fundamental units: literals, operators, and variables.
true
3.1459
"a"
[1,2,3,4,5]
{x:5, y:2}
there are only five basic flavors of data in flash, and all other data structures are made from recipes using these ingredients
| Boolean | true, false |
| Number | 1, 2.345, -6700 |
| String | "abc", 'abc' |
| Array | [val0, val1, val2] |
| Object | { "name": val } |
17.96 / 2.48
-3
3 + 2 * 5
(3 + 2) * 5
math + - * / %
evaluation ( )
assignment = += -= *= /= %=
comparison == != < > <= >=
logical ! && ||
incremental ++ --
see the full list in the ActionScript Dictionary: Operators
variables are simple containers that allow us to access a value by name. this makes manipulation very convenient; we can let the computer keep track of the literal value while we apply various operations to it.
we define a variable with the keywordvar and a label, and we
use the assignment operator to associate a value with a variable.
var n = 2;
variables also let us collect synonymous literal values into a single location. anytime we need the value in our code, we use a reference to it. this allows us to make updates to the literal value in only one place while still being able to use the value in multiple places.
we can go from this:
var beginning = 5; var end = 5 + 17; var middle = 5 + (17 / 2);
to this:
var beginning = 5; var distance = 17;
var end = beginning + distance; var middle = beginning + (distance / 2);
by making the value of one variable dependent on an expression involving another, we can build up complex relationships that encode a process. we can then feed different inputs to the process, and harvest new results without rewriting any of the code.
var leftSide = 5; var interval = 3;
var mark1 = leftSide + interval; var mark2 = mark1 + interval; var mark3 = mark2 + interval;
// sometimes one line comments introduce the next line var x = 3; // sometimes they clarify a specific operation
// sometimes they just deactivate code without deleting it // var z = 11;
/*, */ pair for commenting out multiple lines of code.
note that the numpad has these keys right next to each other, so you can type the star without
using the SHIFT key.
/* multiline, or block, comments exclude everything between the comment markers from the compiler */ var x = 9;
/************************************************* * some people like to make boxes out of their * * comments. i think they're hard to maintain. * *************************************************/ var a = new Array(/* block comments don't kill the whole line */);
var);)// & /* */)TAB & SHFT-TAB)CTRL-T)CTRL-ENTER to preview in authoring environment (access to output window and compiler errors window)F12 to publish (create all types checked in the Formats tab of the Publish Settings Panel) and previewSHFT-F12 to publish only (no preview)
CTRL-T)
Var w = 5;
varx=6;
var y = 7 2;
var z = 83 - var w;
var n;
var total = n + 5;
this.loadMovie("notReallyThere.swf");
var a = [0, 1, 2];
var b = a[3];
var totalGrades = 98 + 83 + 95;
var average = totalGrades / 5;
✔ var s:String = "boo-haloo"; ✘ var n:String = 7;
the test need not be elaborate, or even physical, but at least mentally executing the lines of code and verifying they will do what is expected is a critical habit to get into.
it is extremely easy in flash to create a new document, write a quick method and a couple
lines to call it with various inputs, and print the output with trace().
once the code snippet performs as expected, copy and paste it back into the main program.
keep chunks of code small enough to fit on screen all at once. monolithic code is usually a haven for bugs.
code is arguably never perfect. but if it functions to specifications, it can be good enough. testing is the only way to find problems, so test early and test often, but tests are only useful if you know what the answer should be!