AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

Using playhead commands (continued)


Example 3: Controlling a continuous-loop movieclip (using play and stop)

In this example, we'll look at using the playhead commands play and stop to control a continuously looping movieclip. In the game above, which we'll re-create later in this section there are several movieclips which provide the motion and interactive elements of the movie. The bees are movieclips which contain bee graphics following a motion-guide path, the control buttons on the left are single-frame movieclips, and the flowers are also single-frame movieclips, which allows detection of collisions between a bee and one of the flowers. In the game, points are accrued when one of the bees is stopped while touching that particular flower.

Open beegame_start.fla (available via free download link at right) to follow along with the notes in this section. Look in the library of the fla (ctrl-L) and edit the flyingbee1 movieclip by double-clicking on its icon. (Alternately, you can double-click the instance bee1_mc on stage to edit it in place.) You'll see two layers in the movieclip: one layer with a bee graphic (containing another movieclip named wings_mc) and a motion guide layer. If you scrub along the timeline of the movieclip, you'll see that the bee follows that guide in a continuous loop. Since the point of the game is to use the controls to start and stop the bees, the playhead commands needed are play and stop, respectively.

To make start and stop controls for one of the bees, do the following:

This is the code you should have in frame 1:

beestop_mc.onRelease = function() {
   bee_mc.stop();
}
beestart_mc.onRelease = function() {
   bee_mc.play();
}

and the movie should look like this:

Addressing nested movieclips

Notice when you stop the bee in the movie above, its wings don't stop. That may be fine, but if you do want to stop the wings, you'll need to address them properly. Remember that our code is going in frame 1 of the main timeline and wings_mc is not on that same timeline so you can't just do wings_mc.stop() to make them stop.

Instead you need to provide a path-to-instance from the main movie to the nested wings movieclip. Look at the diagram below to see how to address (from the main timeline) a movieclip nested within another. (Notice that although wings_mc is actually nested inside a graphic that is nested inside bee_mc, the graphic is irrelevant in terms of actionscript -- graphic symbols are not addressable with actionscript, so nesting mc1->graphic->mc2 is the same as just nesting mc1->mc2 as far as the actionscript is concerned.)

Exercise 1:
Drag two more controls onto the stage and call them wingstop_mc and wingstart_mc. Add code to frame 1 to make the wings start and stop (independently from the movement of the bees along the timeline. The movie should look and function something like:

If you get stuck, open beewingstop.fla (available by subscription from link at right) and look at the code.

Exercise 2:
Change the code in frame 1 so that beestart_mc makes the bee move and the wings beat, and beestop_mc makes the bee stop and the wings stop (hint: each function will need two lines of code in it, not just one).

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads