Difference between revisions of "Actionscript"
(→Operators) |
(→See Also) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | http://ntt.cc/2008/04/09/over-30-useful-as3-open-source-projects.html | ||
+ | |||
== Basics == | == Basics == | ||
=== Operators === | === Operators === | ||
Line 14: | Line 16: | ||
* -- decrease by one | * -- decrease by one | ||
− | those | + | those can be postfix or prefix operators but remember that only prefix affects it immediately. |
<pre>myNum = 5; | <pre>myNum = 5; | ||
trace(myNum++); // displays 5 (adds one to next trace) | trace(myNum++); // displays 5 (adds one to next trace) | ||
Line 20: | Line 22: | ||
trace(++myNum); // displays 7 (adds one as its a prefix) | trace(++myNum); // displays 7 (adds one as its a prefix) | ||
</pre> | </pre> | ||
+ | |||
+ | use this incremental thing to modify property over time: | ||
+ | <pre>my_mc.onEnterFrame = function () { | ||
+ | this._rotation += 5; // makes mc rotate by 5 degrees clockwise for each tick of frame rate | ||
+ | };</pre> | ||
+ | |||
+ | === Checking/Comparing Values === | ||
+ | |||
+ | * == checks for logical equality | ||
+ | * != checks for logical inequality | ||
+ | * === checks for strict logical equality | ||
+ | * !== checks for strict logical inequality | ||
+ | |||
+ | <pre>logical equality test | ||
+ | trace(5 == 6); // false | ||
+ | trace(6 == 6); // true | ||
+ | trace(6 == "6"); // true | ||
+ | |||
+ | logical inequality test | ||
+ | trace(5 != 6); // true | ||
+ | trace(6 != 6); // false | ||
+ | trace(6 != "6"); // false</pre> | ||
+ | |||
+ | notice how with or without the quotation marks its okay... but with the strict logical operators, its not the case. | ||
+ | |||
+ | <pre>strict logical equality test | ||
+ | trace(5 === 6); // false | ||
+ | trace(6 === 6); // true | ||
+ | trace(6 === "6"); // false | ||
+ | trace("6" === 6); // false | ||
+ | trace("6" === "6"); // true | ||
+ | |||
+ | strict logical inequality test | ||
+ | trace(5 !== 6); // true | ||
+ | trace(6 !== 6); // false | ||
+ | trace(6 !== "6"); // true | ||
+ | trace("6" !== 6); // true | ||
+ | trace("6" !== "6"); // false</pre> | ||
+ | |||
+ | you have to remember that you shouldn't use just one = to test if its equal as the single = assigns values to things. | ||
+ | |||
+ | === Checking Datatype === | ||
+ | |||
+ | <pre>var myVar = 5; | ||
+ | if (typeof myVar == "number") { | ||
+ | trace("Its a number"); | ||
+ | }</pre> | ||
+ | |||
+ | NaN is when its Not a Number but you can't just check it against the NaN. You have to use the special isNaN function (conversely, you can also use !isNaN to find a number) | ||
+ | |||
+ | <pre>var myVar - 15 - "coffee"; | ||
+ | if (isNaN(myVar)) { | ||
+ | trace("Thats not a number"); | ||
+ | }</pre> | ||
+ | |||
+ | === Comparing Arrays === | ||
+ | |||
+ | when arrays are compared to see if they are the same, it has to be the exact same thing ie: can't just contain the same items, it has to be equalled, like literally... | ||
+ | |||
+ | <pre>arrayOne = newArray("wonk", "wink"); | ||
+ | arrayTwo = newArray("wonk", "wink"); | ||
+ | trace(arrayOne == arrayTwo); // displays false | ||
+ | |||
+ | arrayOne = newArray("wonk", "wink"); | ||
+ | arrayOne = arrayTwo; | ||
+ | trace(arrayOne == arrayTwo); // displays true</pre> | ||
=== Conditionals === | === Conditionals === | ||
Line 41: | Line 109: | ||
== Runtime Environment == | == Runtime Environment == | ||
+ | === Include external actionscript file=== | ||
+ | <pre>#include "filename.as" // includes the .as file at runtime (must be in same folder as .fla)</pre> | ||
+ | |||
=== Detecting player/OS === | === Detecting player/OS === | ||
=== Setting movie dimensions === | === Setting movie dimensions === | ||
Line 63: | Line 134: | ||
== Date/Time == | == Date/Time == | ||
+ | |||
+ | |||
+ | == See Also == | ||
+ | |||
+ | [[Category:Programming]] |
Latest revision as of 03:39, 1 June 2009
http://ntt.cc/2008/04/09/over-30-useful-as3-open-source-projects.html
Contents
Basics
Operators
- + for addition
- - for subtraction
- asterisk for multiply
- / for divide
these two are the same:
myNum = myNum + 6; myNum += 6;
- ++ add one
- -- decrease by one
those can be postfix or prefix operators but remember that only prefix affects it immediately.
myNum = 5; trace(myNum++); // displays 5 (adds one to next trace) trace(myNum); // displays 6 (has added one from the postfix) trace(++myNum); // displays 7 (adds one as its a prefix)
use this incremental thing to modify property over time:
my_mc.onEnterFrame = function () { this._rotation += 5; // makes mc rotate by 5 degrees clockwise for each tick of frame rate };
Checking/Comparing Values
- == checks for logical equality
- != checks for logical inequality
- === checks for strict logical equality
- !== checks for strict logical inequality
logical equality test trace(5 == 6); // false trace(6 == 6); // true trace(6 == "6"); // true logical inequality test trace(5 != 6); // true trace(6 != 6); // false trace(6 != "6"); // false
notice how with or without the quotation marks its okay... but with the strict logical operators, its not the case.
strict logical equality test trace(5 === 6); // false trace(6 === 6); // true trace(6 === "6"); // false trace("6" === 6); // false trace("6" === "6"); // true strict logical inequality test trace(5 !== 6); // true trace(6 !== 6); // false trace(6 !== "6"); // true trace("6" !== 6); // true trace("6" !== "6"); // false
you have to remember that you shouldn't use just one = to test if its equal as the single = assigns values to things.
Checking Datatype
var myVar = 5; if (typeof myVar == "number") { trace("Its a number"); }
NaN is when its Not a Number but you can't just check it against the NaN. You have to use the special isNaN function (conversely, you can also use !isNaN to find a number)
var myVar - 15 - "coffee"; if (isNaN(myVar)) { trace("Thats not a number"); }
Comparing Arrays
when arrays are compared to see if they are the same, it has to be the exact same thing ie: can't just contain the same items, it has to be equalled, like literally...
arrayOne = newArray("wonk", "wink"); arrayTwo = newArray("wonk", "wink"); trace(arrayOne == arrayTwo); // displays false arrayOne = newArray("wonk", "wink"); arrayOne = arrayTwo; trace(arrayOne == arrayTwo); // displays true
Conditionals
var message = "yellow"; // the equal sign assigns values to variables trace ("big " + message + " truck"); // prints "big yellow truck" if (message == "yellow") { // the double equal sign checks to see if variables are equal trace ("yes, i did say big yellow truck."); // prints "yes, i did say big yellow truck" trace (typeof message); // prints "string" var x = a; // prints "number" trace (typeof a) }
var x = 1; while (x <= 5) { // as long as x is less than 5... trace (x); // return 1 in output x = x + 1; // return 2 3 4 5 in output }
Reusable functions
Runtime Environment
Include external actionscript file
#include "filename.as" // includes the .as file at runtime (must be in same folder as .fla)
Detecting player/OS
Setting movie dimensions
Color
Setting RGB
Setting Transparency
Transform and restore RGB
Controlling RGB with sliders
Drawing
Numbers
Rounding Numbers
Text Field
Arrays
Oh no.