Difference between revisions of "Actionscript"

From Wikicliki
Jump to: navigation, search
(See Also)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
actionscript, that infuriatingly intractable marshmellow fluff...
+
http://ntt.cc/2008/04/09/over-30-useful-as3-open-source-projects.html
so far i have only understood the most basic of concepts, but now i will try to learn it well.
 
  
*[[Actionscript Sandbox]]
+
== Basics ==
*[http://moock.org/lectures/groundUpAS3/ Moock's Actionscript 3.0 from the Ground Up]
+
=== Operators ===
  
*http://www.kirupa.com/developer/actionscript/spring.htm
+
* + for addition
*http://www.bit-101.com/tutorials/elasticity.html
+
* - for subtraction
 +
* asterisk for multiply
 +
* / for divide
  
 +
these two are the same:
 +
<pre>myNum = myNum + 6;
 +
myNum += 6;</pre>
  
== alpha ==
+
* ++ add one
<pre>
+
* -- decrease by one
// set movie clip's alpha (transparency) to 50% immediately
 
myMC._alpha = 50%
 
  
// set up alphacounter, if alphacounter is undefined then it sets it to 99. otherwise alphacounter is to decrease to 0 and delete itself.
+
those can be postfix or prefix operators but remember that only prefix affects it immediately.
myMC.onEnterFrame = function (){
+
<pre>myNum = 5;
this.alphaCount = (this.alphaCount == undefined) ? 99 : --this.alphaCount;
+
trace(myNum++);      // displays 5 (adds one to next trace)
this._alpha = this.alphaCount;
+
trace(myNum);       // displays 6 (has added one from the postfix)
if (this._alpha <= 0) {
+
trace(++myNum);       // displays 7 (adds one as its a prefix)
delete this.onEnterFrame;
+
</pre>
}
 
}
 
  
// change it to ++ to make it go up
+
use this incremental thing to modify property over time:
</pre>
+
<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 ===
  
== Codehinting ==
+
* == checks for logical equality
 +
*  != checks for logical inequality
 +
* === checks for strict logical equality
 +
* !== checks for strict logical inequality
  
to keep with good practice you should keep to these common suffixes so that any programmer can read your code easily without finding it utterly incomprehensible.
+
<pre>logical equality test
 +
trace(5 == 6);      // false             
 +
trace(6 == 6);      // true
 +
trace(6 == "6");      // true
  
variables:
+
logical inequality test
*must start with letters, underscore or dollarsign.
+
trace(5 != 6);      // true             
*no hyphens or other punctuation besides the underscore.
+
trace(6 != 6);      // false             
*cannot start with a number.
+
trace(6 != "6");      // false</pre>
*are case-insensitive, but keep it constant or you might get confused
 
  
*xxx_str  // means the variable contains a string
+
notice how with or without the quotation marks its okay... but with the strict logical operators, its not the case.
*xxx_arr // means the variable contains an array
 
  
*[http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001151.html More about using suffixes to trigger code hints]
+
<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
  
== Conditionals ==
+
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>
  
<pre>var message = "yellow";
+
you have to remember that you shouldn't use just one = to test if its equal as the single = assigns values to things.
trace ("big " + message + " truck");                // prints "big yellow truck"
 
if (message == "yellow") {
 
trace ("yes, i did say big yellow truck.");        // prints "yes, i did say big yellow truck"
 
}</pre>
 
  
<pre>= assigning value
+
=== Checking Datatype ===
== comparing values</pre>
 
  
== loops ==
+
<pre>var myVar = 5;
 +
if (typeof myVar == "number") {
 +
  trace("Its a number");
 +
}</pre>
  
<table><tr><td valign=top>this sequence... <pre>trace (101);
+
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)  
trace (102);
 
trace (103);
 
trace (104);
 
trace (105);</pre></td>
 
<td valign=top>is equal to:
 
<pre>var x = 101;
 
trace (x);
 
x = x + 1;
 
trace (x);
 
x = x + 1;
 
trace (x);
 
x = x + 1;
 
trace (x);
 
x = x + 1;
 
trace (x);</pre></td>
 
<td valign=top>is also equal to:
 
<pre>var x = 101;
 
while (x <= 105) {    // as long as x is less than 105...
 
trace (x);
 
x = x + 1;          // ...continue to trace x but add one each time
 
}</pre></td>
 
<td valign=top>they all print:
 
<pre>101
 
102
 
103
 
104
 
105</pre></td></tr></table>
 
  
== functions ==
+
<pre>var myVar - 15 - "coffee";
 +
if (isNaN(myVar)) {
 +
trace("Thats not a number");
 +
}</pre>
  
sample:
+
=== Comparing Arrays ===
  
<pre>function area(height, width){
+
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...
return height * width;
 
}
 
area1 = area(10,15);
 
area2 = area(10,20);
 
area3 = area(10,20);
 
</pre>
 
  
== event based execution ==
+
<pre>arrayOne = newArray("wonk", "wink");
 +
arrayTwo = newArray("wonk", "wink");
 +
trace(arrayOne == arrayTwo); // displays false
  
<pre>someObject.onSomeEvent = someFunction;</pre>
+
arrayOne = newArray("wonk", "wink");
 +
arrayOne = arrayTwo;
 +
trace(arrayOne == arrayTwo); // displays true</pre>
  
for example:
+
=== Conditionals ===
<pre>skaGirl.onRelease = dirtyskanking;            // the object named skaGirl will dirtyskank when mouse is released
 
  
function dirtyskanking () {
+
<pre>var message = "yellow";                        // the equal sign assigns values to variables
this._parent._rotation = 360;                     // defines the rotation of the dirtyskanking.
+
trace ("big " + message + " truck");            // prints "big yellow truck"
}
+
if (message == "yellow") {                     // the double equal sign checks to see if variables are equal
</pre>
+
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)
 +
}</pre>
  
== sample ==
+
<pre>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
 +
}</pre>
  
=== quiz ===
+
=== Reusable functions===
  
<pre>
+
== Runtime Environment ==
button1.onRelease = function () {        // on pressing button 1
+
=== Include external actionscript file===
this._parent.q1answer = 1;                // assigning 1 to q1 answer
+
<pre>#include "filename.as"          // includes the .as file at runtime (must be in same folder as .fla)</pre>
this._parent.gotoAndStop("q2")           // after which we go to frame labelled as q2
 
}
 
  
if (q1answer == 3) {                      //  if answer was right
+
=== Detecting player/OS ===
  totalCorrect = totalCorrect++;          //  increase number of total correct qns by one
+
=== Setting movie dimensions ===
}
 
</pre>
 
  
== confused about root, this, and parent? so am i! ==
+
== Color ==
 +
=== Setting RGB ===
 +
=== Setting Transparency ===
 +
=== Transform and restore RGB ===
 +
=== Controlling RGB with sliders ===
  
this piece of code would work ON a frame on the main stage, one_mc being an object onnit.
+
== Drawing ==
  
<pre>function T1over() {
+
== Numbers ==
_root.one_mc.gotoAndStop(2);
+
=== Rounding Numbers ===
getURL('javascript:modifyT1();');
 
}
 
  
_root.one_mc.onRollOver = T1over;</pre>
+
== Text Field ==
  
== actionscript and javascript ==
+
== Arrays ==
 +
Oh no.
  
*[http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_15683 flash to/from javascript call]
+
== Strings ==
*[http://board.flashkit.com/board/archive/index.php/t-578023.html halp i am so confused]
 
  
== tips ==
+
== Date/Time ==
  
* use frame labels rather than frame numbers. you could call a starting frame "init".
 
* dont use too many functions
 
  
 
== See Also ==
 
== See Also ==
*[http://livedocs.adobe.com/flashlite/2/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000642.html preloader]
 
*[http://www.sephiroth.it/python/sepy.php SEPY actionscript editor]
 
*[http://www.mtasc.org/ Motion-Twin ActionScript 2 Open Source free compiler.]
 
*[http://code.google.com/p/swfobject/ SWFobject]
 
*[http://hosted.zeh.com.br/mctween/index.html MC Tween - Best way to tween]
 
*[http://code.google.com/p/tweener/ Tweener - New way to tween]
 
  
*[http://labs.adobe.com/wiki/index.php/ActionScript_3:resources:apis:libraries actionscript apis (youtube, odeo)]
+
[[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

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.

Strings

Date/Time

See Also