Difference between revisions of "Actionscript 2 Sandbox"

From Wikicliki
Jump to: navigation, search
Line 96: Line 96:
 
</u></font>";
 
</u></font>";
  
 +
        /* LEARNING HOW TO DRAW */
  
 +
_root.lineStyle(1, 0x000000, 50);                        // line-thickness, colour, opacity
 +
_root.moveTo(50, 100);                                                // cross hatch drawing
 +
_root.lineTo(200, 100);                                                // first number is the horizontal value
 +
_root.moveTo(50, 150);                                                // second number is the vertical value
 +
_root.lineTo(200, 150);
 +
_root.moveTo(150, 50);
 +
_root.lineTo(150, 200);
 +
_root.moveTo(100, 50);
 +
_root.lineTo(100, 200);
  
</pre>
+
_root.lineStyle(5, 0x000000, 20);                        // drawing a square
 +
_root.moveTo(50, 50);
 +
_root.lineTo(200, 50);
 +
_root.lineTo(200, 200);
 +
_root.lineTo(50, 200);
 +
_root.lineTo(50, 50);
  
 +
_root.lineStyle(10, 0x000000, 100);          // drawing a deformed circley thing with a fill.
 +
                                              // note: to have no border, omit linestyle
 +
_root.moveTo(125, 50);                        // move starting point from 0,0 to elsewhere
 +
_root.beginFill(0x888888, 50);                // add a fill of 50% opacity. can
 +
_root.curveTo(200,50, 200,125);              // _root.curveTo(controlX, controlY, anchorX, anchorY);
 +
_root.curveTo(200,200, 125,200);
 +
_root.curveTo(50,200, 50,125);
 +
_root.curveTo(50,50, 125,50);
 +
_root.endFill
  
  
 +
        /* drawing program */
  
 +
_root.createEmptyMovieClip("line",1);
  
 +
_root.onMouseDown = function() {
 +
line.moveTo(_xmouse,_ymouse);
 +
line.lineStyle(1,0x000000,100);
  
 
+
this.onMouseMove = function() {
 
+
line.lineTo(_xmouse,_ymouse);
 
+
updateAfterEvent();
 
+
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
== 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
 
 
 
*[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]
 
 
 
== conditionals ==
 
 
 
<pre>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"
 
}</pre>
 
 
 
<pre>= assigning value
 
== comparing values</pre>
 
 
 
== loops ==
 
 
 
<table><tr><td valign=top>this sequence... <pre>trace (101);
 
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 ==
 
 
 
sample:
 
 
 
<pre>function area(height, width){
 
return height * width;
 
 
}
 
}
area1 = area(10,15);
 
area2 = area(10,20);
 
area3 = area(10,20);
 
</pre>
 
  
== event based execution ==
 
 
<pre>someObject.onSomeEvent = someFunction;</pre>
 
 
for example:
 
<pre>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.
 
}
 
</pre>
 
  
== sample ==
 
  
=== quiz ===
 
  
<pre>
 
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
 
}
 
 
</pre>
 
</pre>
 
== 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.
 
 
<pre>function T1over() {
 
_root.one_mc.gotoAndStop(2);
 
getURL('javascript:modifyT1();');
 
}
 
 
_root.one_mc.onRollOver = T1over;</pre>
 
 
== actionscript and javascript ==
 
 
*[http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_15683 flash to/from javascript call]
 
*[http://board.flashkit.com/board/archive/index.php/t-578023.html halp i am so confused]
 

Revision as of 16:46, 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"
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
}


        /* 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
}                                         //    you could even do something like
                                          //    if ( 3 < 300 ) { do something }
                                          //    if ( "a" < "z" ) { do something }
                                          //

        /* 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) {                                 // this placed on one_mc
        gotoAndStop(2);                         // is the same as above
        getURL('javascript:modifyT1();');       // OPTIONALLY use frame labels instead of frame no.
}

        /*  ROTATION  */
        
on (rollOver) {
        this._rotation = + 45;          // Values from 0 to 180 represent clockwise rotation;
}                                       // values from 0 to -180 represent counterclockwise rotation


        /* LOADING IMAGES IN */
        
this.createEmptyMovieClip("image_mc", 1);       // to create a dynamically created mc
image_mc.loadMovie("giraffe.jpg");              // mc loads an image

image_mc.removeMovieClip();                     // to remove the dynamically created mc


        /* CREATING TEXT FIELDS */
        
var score = 99;
this.createTextField("score_txt", 1, 150, 200, 200, 20);
score_txt.text = "  "+score+" points";  // prints 99 points in score_txt field
score_txt.textColor = 0xFF0000;                        // text color is red

                                // myMovieClip.createTextField (instanceName,depth,x,y,width,height)
                                //        instanceName  A string that identifies the instance name of the new text field.
                                //        x An integer that specifies the x coordinate of the new text field.
                                //        y An integer that specifies the y coordinate of the new text field.
                                
myFormat.underline  = true;
myFormat.Color = 0xFF0000;                  // alternatively, score_txt.textColor also works
myFormat.bold = true;
myFormat.size = 16;
myFormat.font = "Georgia";                  // if you choose to set a font, make sure you include dummy field offstage
                                            // which uses your font, has font embedding turned on (character)
                                            // and if you use bold/italic/regular variation you must include each type
                                            
score_txt.setTextFormat(myFormat);          // applies format to all of score_txt
score_txt.setTextFormat(12, 27, myFormat);  // applies format to only the 12-27th character in score_txt. HARDCORE....


this.createTextField("more_txt", 1, 180, 250, 200, 20);
more_txt.html = true;                                           // creating a dynamic (highlightable) txtfield
more_txt.htmlText = "look its a
<font color='#00ccff'><u>                                       // you will have to format it all in html
<a href='http://www.sg'>link</a>
</u></font>";

        /* LEARNING HOW TO DRAW */

_root.lineStyle(1, 0x000000, 50);                        // line-thickness, colour, opacity
_root.moveTo(50, 100);                                                // cross hatch drawing
_root.lineTo(200, 100);                                                // first number is the horizontal value
_root.moveTo(50, 150);                                                // second number is the vertical value
_root.lineTo(200, 150);
_root.moveTo(150, 50);
_root.lineTo(150, 200);
_root.moveTo(100, 50);
_root.lineTo(100, 200);

_root.lineStyle(5, 0x000000, 20);                        // drawing a square
_root.moveTo(50, 50);
_root.lineTo(200, 50);
_root.lineTo(200, 200);
_root.lineTo(50, 200);
_root.lineTo(50, 50);

_root.lineStyle(10, 0x000000, 100);           // drawing a deformed circley thing with a fill.
                                              // note: to have no border, omit linestyle
_root.moveTo(125, 50);                        // move starting point from 0,0 to elsewhere
_root.beginFill(0x888888, 50);                // add a fill of 50% opacity. can
_root.curveTo(200,50, 200,125);               // _root.curveTo(controlX, controlY, anchorX, anchorY);
_root.curveTo(200,200, 125,200);
_root.curveTo(50,200, 50,125);
_root.curveTo(50,50, 125,50);
_root.endFill


        /* drawing program */

_root.createEmptyMovieClip("line",1);

_root.onMouseDown = function() {
line.moveTo(_xmouse,_ymouse);
line.lineStyle(1,0x000000,100);

this.onMouseMove = function() {
line.lineTo(_xmouse,_ymouse);
updateAfterEvent();
}
}