Difference between revisions of "Actionscript 2 Sandbox"
From Wikicliki
Line 1: | Line 1: | ||
the readable stuff is at the [[Actionscript]] page, the sandbox is the-messing-around-with-snippets-section. | the readable stuff is at the [[Actionscript]] page, the sandbox is the-messing-around-with-snippets-section. | ||
− | <pre>#include "filename.as" // includes the .as file at runtime (must be in same folder as .fla) | + | <pre> |
+ | #include "filename.as" // includes the .as file at runtime (must be in same folder as .fla) | ||
Line 42: | Line 43: | ||
getURL('javascript:modifyT1();'); | getURL('javascript:modifyT1();'); | ||
} | } | ||
+ | |||
+ | 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> | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
== code hinting suffixes/names== | == code hinting suffixes/names== | ||
Revision as of 14:20, 17 June 2008
the readable stuff is at the Actionscript page, the sandbox is the-messing-around-with-snippets-section.
#include "filename.as" // includes the .as file at runtime (must be in same folder as .fla) /* 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" } /* BUTTONS IN A QUIZ */ button1.onRelease = function () { // on pressing button 1 this._parent.q1answer = 1; // assigning 1 to q1 answer this._parent.gotoAndStop("q2") // after which we go to frame labelled as q2 } if (q1answer == 3) { // if answer was right totalCorrect = totalCorrect++; // increase number of total correct qns by one } /* FUNCTIONS IN DIFFERENT LAYERS/SCENES */ function T1over() { // this code is placed on main stage _root.one_mc.gotoAndStop(2); // but refers to a one_mc on the stage getURL('javascript:modifyT1();'); } _root.one_mc.onRollOver = T1over; // this would be same on (rollOver) { gotoAndStop(2); getURL('javascript:modifyT1();'); } 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 }
Contents
code hinting suffixes/names
to keep in good form you should keep to these common suffixes so that any programmer can read your code easily without finding it utterly incomprehensible.
variables:
- must start with letters, underscore or dollarsign.
- no hyphens or other punctuation besides the underscore.
- cannot start with a number.
- are case-insensitive, but keep it constant or you might get confused
- xxx_str // means the variable contains a string
- xxx_arr // means the variable contains an array
conditionals
var message = "yellow"; 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" }
= assigning value == comparing values
loops
this sequence... trace (101); trace (102); trace (103); trace (104); trace (105); |
is equal to:
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); |
is also equal to:
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 } |
they all print:
101 102 103 104 105 |
functions
sample:
function area(height, width){ return height * width; } area1 = area(10,15); area2 = area(10,20); area3 = area(10,20);
event based execution
someObject.onSomeEvent = someFunction;
for example:
skaGirl.onRelease = dirtyskanking; // the object named skaGirl will dirtyskank when mouse is released function dirtyskanking () { this._parent._rotation = 360; // defines the rotation of the dirtyskanking. }
sample
quiz
button1.onRelease = function () { // on pressing button 1 this._parent.q1answer = 1; // assigning 1 to q1 answer this._parent.gotoAndStop("q2") // after which we go to frame labelled as q2 } if (q1answer == 3) { // if answer was right totalCorrect = totalCorrect++; // increase number of total correct qns by one }
confused about root, this, and parent? so am i!
this piece of code would work ON a frame on the main stage, one_mc being an object onnit.
function T1over() { _root.one_mc.gotoAndStop(2); getURL('javascript:modifyT1();'); } _root.one_mc.onRollOver = T1over;