350 likes | 364 Views
Learn how to use Action Script in Flash to create interactive games and non-linear presentations. This guide covers animation control, using buttons and movie clips, and implementing conditional statements.
E N D
"The games of a people reveal a great deal about them.“ Marshall McLuhan Intro to Action Script
The programming language inside Flash. Allows Flash objects react to events such as the user choices or random events. Controls animation in non-linear presentations, interactive applications and games. Actions scripts are lists of instructions for Flash to follow. Action Script Action Script places in the Flash movie Frames Buttons Movie clips
Window > Actions ( F9 ) Action Script panel
to frames select frame in the timeline then select (type in) function in the Actions panel to buttons select button in the stage then select (type in) function in the Actions panel to movie clips name movie clip in the properties window select movie clip in the stage then select (type in) function in the Actions panel To Apply Scripts
Controlling Flash playback gotoAndPlay(“frame”); command to jump and play specific frame in the movie gotoAndStop (“frame”); command to jump and stop at the specific frame in the movie Frame scripts
Create a new movie with 4 keyframes and name them Animation Aesthetics Concept Fun Frame scripts
Select “Animation” frame and apply script gotoAndPlay (“fun”); Select “fun” frame and apply script gotoAndPlay (“concept”); Select “concept” frame and apply script gotoAndPlay (“aesthetics”); Select “aesthetics” frame and apply script gotoAndPlay (“animation”); Test the movie Control > test movie Frame scripts
Make a new layer “buttons” Create graphics for two buttons on the stage play and stop Select button play and convert it into the button symbol Modify > convert to symbol > button Edit individual button frames in the individual button timelines for rollover and press effects Select play button on the stage and apply the script in the actions window: on (press) { play(); } Button scripts
Select stop button on the stage and apply the script in the actions window: on (press) { stop(); } Test the movie Button scripts
Make a new layer “movie_clips” Create graphics for movie clip character on the stage Select graphics and convert it into the movie clip symbol Modify > convert to symbol > movie clip Drag 2 instances of the movie clip to the stage and name them : char1 and char2 Movie clip scripts
Select char1 on the stage and apply the following script using Actions window Movie Clip Control onClipEvent (enterFrame) { this._x+=5; } this refers to the movie clip itself .dot after the object means you want to access object property _x horizontal position of the object += move object 5 pixels at a time Movie clip scripts
Select char2 on the stage and apply the following script using Actions window Movie Clip Control onClipEvent (enterFrame) { this._x=_root._xmouse; this._y=_root._ymouse; } _root addresses the main timeline _xmouse horizontal position of the mouse cursor on the stage _ymouse vertical position of the mouse cursor on the stage Movie clip scripts
Storage units to store numbers or string of characters allowing dynamic data transactions Must be declared and initialized before use myVar = 5; myVar +=2; myVar2=“hello AD305”; Arithmetic operators: + addition - subtraction / division * multiplication -- decrement ++ increment % modulo (remainder of division) variables
A=7; B=5; C=A+B; trace(C); Math functions built in Flash > Math Math.random() function MyRandNum = int(Math.radom()*10); cerates random number from 0 to 9 D=“hello”; E=“world”; F=D+E; (F=“helloworld”) trace(F); G=D+” “ +E; (G=“hello world”) trace(G); variables
Control structure Sequence structure Loop structure Structured programming
A == B A is equal to B The result is true if the expressions are equal. A != B A is not equal to B Returns true if the operands are not the same. A < B A is less than B Returns true if the left operand is mathematically smaller than the right operand. A > B A is greater than B Returns true if the left operand is mathematically larger than the right operand. Relational operators
A <= B A is less than or equal to B Returns true if the left operand is mathematically smaller than or the same as the right operand. A >= B A is greater than or equal to B Returns true if the left operand is mathematically larger than or the same as the right operand. Relational operators
Conditional statement in its simplest form is specified by keyword IF If (this condition is true) { execute this statement ; } Can contain several statements If (this condition is true) { do this ; and this ; and this ; } If (a == 7) { gotoAndPlay (“special_frame”) ; } Conditional statements
To extend the scope of the IF control statement we can use keyword ELSE If (this condition is true) { execute this statement ; } else { this statement ; } If (a == 7) { gotoAndPlay (“special_frame”) ; } Else { gotoAndPlay (”another_frame”) ; } Conditional statements
If (a == 7) { gotoAndPlay (“special_frame”) ; } Else if (a == 12) { gotoAndPlay (”another_frame”) ; } Else if (a == 15) { gotoAndPlay (”very_special_frame”) ; } Else { gotoAndPlay (”not_so_special_frame”) ; } Conditional statements
If statement is fundamental building block for all programs To change variables Branch to other frames Decisions on the score etc. Conditional statements
Used to combine relational operators compare Boolean values (true and false) and return a third Boolean value and adding two operands produces a true result only when both operands are true or ORing two operands produces a true result is either operand is true && Returns true only if both the left and right operands are true || Returns true if either the left or right operand is true !NOT: Returns the logical (Boolean) opposite of the operand. The logical NOT operator takes one operand. Logical Operators
If ((a == 7) and (b == 12)) { gotoAndPlay (”another_frame”) ; } If ((a == 7) and (username == “alex”)) { gotoAndPlay (”alex_frame”) ; } Logical Operators
A loop is a control structure that allows one or more events to occur as long as a given condition remains true For loop allows automatic incrementation of a variable For (initialization; condition ; increment/decrement) { statement ; } For (i=0 ; i<10 ; i++) { trace (i) ; } Loops
Create an instance of the char3 movie clip on the stage For ( i = 1 ; i <= 8 ; i++ ) { _root . char3 . _x++ ; } Loops
An autonomous module that performs tasks Piece of code that can be reused function sum ( m , o ) { L = m + o ; return L ; } trace ( sum ( 3 , 15 )) ; Functions
Arrays allow to store a sequence of variables of the same type. Fruit = [“apple”, “orange”, “peach”, “plum”] ; 0 1 2 3 myItem = Fruit[1] ; To create an empty array and add items: Fruit = new Array(); Fruit.push(“apple”); Fruit.push(“orange”); Fruit.push(“peach”); Fruit.push(“plum”); Arrays
while ( Fruit . Length > 0 ) { trace ( Fruit . pop ()) ; } .push adds element to array .pop returns element of the array .sort to list array elements in alphabetical or numerical order Fruit . Sort ( ) ; Arrays
Fruit = new Array(); Fruit.push(“apple”); Fruit.push(“orange”); Fruit.push(“peach”); Fruit.push(“plum”); Fruit . Sort ( ) ; trace ( Fruit . toString ( )) ; Arrays
Fruit = [“apple”, “orange”, “peach”, “plum”] ; 0 1 2 3 Fruit . splice (2 , 1 ) ; 2 –element position to start removing (“peach”) 1 – how many elements to remove (1) .splice removes elements from the array Fruit . splice (2 , 1, “pear” ) ; “peach” is removed and “pear” is inserted into that position Arrays
Fading trail of objects behind the mouse cursor Make new movie with movie clip “cursor trail” In the library right mouse button click on the properties of the movie clip and check Linkage export for Action Script Make a dummy movie clip outside of the stage and apply the following action script Mouse trail
onClipEvent(load) { // creates empty array to hold references for the trail movie clips trail = new Array(); trailNum = 0; // variable trailNum keeps track of the number of next movie clip to be created // variable speed is used to determine how fast the trail movie clips shrink and fade speed = 2; } Mouse trail
onClipEvent(enterFrame) { // new trail // creates a new movie clip from the library trailNum // allows any movieclip in the library which has been given a linkage id to be // attached to an existing movieclip (or the main timeline) in the movie // while it is running. // Note that if you attach a movieclip at the same depth as something that // already exists in the timeline at that depth, the previous thing will be deleted. _root.attachMovie("cursor trail","cursor trail"+trailNum,trailNum); “Linckage ID”, “clipname”, depth var mc = _root.attachMovie("cursor trail","cursor trail"+trailNum,trailNum); Mouse trail
// set position to the current cursor position mc._x = _root._xmouse; mc._y = _root._ymouse; // add to array // places new movie clip trailNum into array and array is incremented for the next element trail.push(mc); trailNum++; Mouse trail
// loops through the movie clips stored in array trail // if any ends up with alpha property less than 0, then they are removed from array and from the screen for(var i=trail.length-1;i>=0;i--) { // reduce alpha and scale trail[i]._alpha -= speed; trail[i]._xscale -= speed; trail[i]._yscale -= speed; // if this is one invisible, remove it if (trail[i]._alpha <= 0) { // remove array trail.splice(0,1); // remove movie clip trail[i].removeMovieClip(); } } } Mouse trail