170 likes | 367 Views
A Primer. ActionScript. ActionScript is…. Based on JavaScript Object-oriented programming Everything you do in ActionScript does something to some object* Some objects in Flash Movie Clip Graphic Button Objects are defined by classes in AS3.
E N D
A Primer ActionScript
ActionScript is… • Based on JavaScript • Object-oriented programming • Everything you do in ActionScript does something to some object* • Some objects in Flash • Movie Clip • Graphic • Button • Objects are defined by classes in AS3 *anything you can manipulate in some way
ActionScript Deconstructed • Class: a group of items that are similar in some way • Items are called objects or instances of the class • Objects can be concrete (like a button or graphic) or abstract (like a variable that is invisible, but hold data) • Objects have two aspects: form (properties) and function (methods) • Each property and method is defined by Actions (pieces of code that tell Flash how to manipulate a target object at any point in your movie).
ActionScriptMetaphor • Ball could be considered a class of items defined as “spherical things” • One object in this class is a movie clip called Tennis_ball • The properties of Tennis_ball might be the color neon green or a 20-pixel diameter • Its methods might be bounce, roll, and/or spin • A 2ndobject in the ball class might be Softball • Properties white…etc.
Basic Programming Concepts &Constructs • syntax • variables • constants • statements • assignment statements • keywords • operators • expressions • arrays • procedures • functions • arguments • control structures • loops • conditions • comments
What you can do with ActionScript? • control the playhead of a timeline • move things around • create and manage user interfaces • play sounds • control video • manage data (xml, lists, etc) • communicate with server-side applications (e.g., web servers)
Actions window panel • select frame 1 • type the following into the Actions panel: • var message = "Hello world!"; • that line of code constitutes a complete instruction, known as a statement • now create three more statements: varfirstName = "your name here"; trace(message); trace ("Hi there, " + firstName + ", nice to meet you."); • play the movie - select Control>>Test Movie
ActionScriptSyntax • ActionScript notation must end in ; • object.method (argument); like noun.verb (adjective); • Example 1root.gotoAndPlay(2);this.gotoAndPlay(2);gotoAndPlay(2); • Explanation: This script instructs the playhead on the main timeline (“root” or “this”) to go to and play on frame 2. Identifying “root” or “this” by name is optional—the current timeline is the default, so you could just say gotoAndPlay(2);
Write ActionScript in Flash • To write AS on Timeline: • Click on a key frame and then press F9.
Timeline Script (for AS3.0) Methods: • play(); • stop(); • gotoAndPlay(); • gotoAndStop(); • nextFrame(); • prevFrame(); • navigateToURL(); Properties • currentFrame • currentLabel • currentLabels • totalFrames
Using Symbols • Reusable content stored in Library • Create instance from symbols, and name
Exercise • Make a pictures slideshow (SlideShow.fla)
Solution (1) • Open a Flash file (AS3.0) • Import pics to library (File->Import) • Convert the pics to Movie Clip (Optional) • Make keyframes and place a pic to every Keyframe
Solution (2) • Create a button symbol and put it in a new layer • Make two instances (next, prev) from the button symbol • Give an instance name “nextButton” and another one “previousButton”
Solution (3) • Create a new layer and name it, say, “AS3” • Write code on the keyframe of the AS3 layer
Solution (4) – Code AS3.0 • stop(); • nextButton.addEventListener(MouseEvent.CLICK, onNextButtonClicked); • previousButton.addEventListener(MouseEvent.CLICK, onPreviousButtonClicked); • functiononNextButtonClicked(e:MouseEvent):void { • if (currentFrame == totalFrames) { • gotoAndStop(1); • } else { • nextFrame(); • } • } • functiononPreviousButtonClicked(e:MouseEvent):void { • if (currentFrame == 1) { • gotoAndStop(totalFrames); • } else { • prevFrame(); • } • }
Solution (4) – Code AS3.0 • stop(); • // add an event listener to the next button • nextButton.addEventListener(MouseEvent.CLICK, onNextButtonClicked); • // add an event listener to the previous button • previousButton.addEventListener(MouseEvent.CLICK, onPreviousButtonClicked); • // a function that goes to the next frame when the next button is clicked • functiononNextButtonClicked(e:MouseEvent):void { • if (currentFrame == totalFrames) { • gotoAndStop(1); • } else { • nextFrame(); • } • } • // a function that goes to the previous frame when the next button is clicked • functiononPreviousButtonClicked(e:MouseEvent):void { • if (currentFrame == 1) { • gotoAndStop(totalFrames); • } else { • prevFrame(); • } • }