It’s actually sort of amazing that we haven’t had this property in older versions of Flash Player and AIR. MovieClip
instances are unique in that they contain their own timeline, independent from the main timeline. Often, a developer will want to know whether or not a specific MovieClip
instance is actually playing or not, and this has traditionally involved monitoring the current frame of the MovieClip
to determine whether or not it is changing over time.
Making use of this new functionality is very direct, as MovieClip.isPlaying
is simply a property of every MovieClip
instance which, when invoked, returns a Boolean
value of true
for playing and false
for stopped. In the following example, we create a MovieClip
add it to the DisplayList
, and then write the isPlaying
property out onto a TextField
.
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class CheckPlaying extends Sprite {
private var face:MovieClip;
private var traceField:TextField;
public function CheckPlaying() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
face = new AngryFace() as MovieClip;
face.x = stage.stageWidth/2;
face.y = stage.stageHeight/2;
face.stop();
face.addEventListener(MouseEvent.CLICK, toggleFacePlaying);
addChild(face);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 26;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.7;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
stage.addEventListener(Event.ENTER_FRAME, checkPlaying);
}
protected function toggleFacePlaying(e:MouseEvent):void {
if(face.isPlaying){
face.stop();
}else{
face.play();
}
}
protected function checkPlaying(e:Event):void {
traceField.text = "MovieClip is playing? => " + face.isPlaying;
}
}
}
The result of this code can be seen fully rendered in Figure 1-4. When clicking upon the MovieClip
, its playback is toggled and the isPlaying
Boolean is measured and written onto the screen.
Note that in this example, we are employing a MovieClip
object that was animated in Flash Professional CS5.5, exported as part of a SWC, and linked into Flash Builder 4.5. There are other ways of doing this, but this method is very direct if you are not working within Flash Professional already.