Previous Page
Next Page

Tripod >> 3pod Tips & Learning and manuals for educations

Recipe 11.11. Controlling Movie Clip Playback from Different Timelines

Problem

You want to use ActionScript within one movie clip's timeline in order to control another movie clip.

Solution

Use a relative target path.

Discussion

You can control one movie clip from another movie clip's timeline by using the correct target path. A target path is the address by which Flash can locate a movie clip instance (or button or text field instance), and target paths come in two varieties: relative and absolute.

An absolute target path is a way for Flash to locate an instance from the top down. All absolute target paths begin with _root or _level0 (or _level1, _level2, and so on), which is the topmost structure in a Flash movie. For example, if a movie clip instance named mClip exists on the main timeline, you could reference it with the absolute target path of _root.mClip. Flash then begins looking first at _root, and then locates a movie clip instance named mClip within _root. If there is a movie clip instance named mNested nested within mClip, then an absolute target path would be _root.mClip.mNested.

Absolute addresses may seem like a good way to target instances. However, there are some drawbacks. One of the major drawbacks is that in many, if not most, cases, your code will stop working if and when you load the .swf file into another .swf file. Flash Player 7 and higher versions support a _lockroot property that enables you to correct that. However, if you are publishing to previous versions of Flash, you cannot work around it without changing your code. Another drawback of absolute addresses is that they tend to lend themselves to poor programming practices. If you have a lot of absolute addresses in your code, it can be a bit more difficult to read. What's more, if you later want to nest a movie clip inside another movie clip for one reason or another, you may need to change your code to accommodate the change.

A relative target path is a way for Flash to locate an instance relative to the timeline from which the command is issued. When you reference a movie clip in the fashion that has been used throughout most of this book, Flash assumes that you want it to look for a movie clip with that instance name (exp 3pod.com) within the current timeline. For example:

	mShape._rotation = 20;

In the preceding example, Flash looks for a movie clip named mShape within the timeline from which the command was issued. Additionally, if you add such a reference within a function, Flash will look for a movie clip with that instance name (exp 3pod.com) within the timeline for which the function is defined. For example:

	function rotateMovieClip():Void {
	  mShape._rotation = 20;
	}

In the preceding example, Flash looks for a movie clip named mShape that exists on the same timeline in which the rotateMovieClip( ) function is defined.

Likewise, the same applies to movie clip references within event handler methods. For example, the following code looks for a movie clip named mShape that exists within the same timeline in which the code is defined:

	btRotate.onRelease = function():Void {
	  mShape._rotation += 20;
	};

In addition, there are a few special keywords you should know when working with relative paths. One of those is the this keyword. The this keyword allows an object or timeline to refer to itself. Therefore, the following two statements are equivalent:

	mShape._rotation = 20; 
	this.mShape._rotation = 20; 

Additionally, if you use the this keyword within a regular function, it will refer to the timeline within which the function is defined. For example, the following function definition is the equivalent of the previous rotateMovieClip( ) function example:

	function rotateMovieClip():Void {
	  this.mShape._rotation = 20;
	}

There is an exception with functions and the keyword this: when the function is an interval function. In that case, the this keyword returns undefined.


However, within event handler methods, the this keyword refers to the object for which the event handler method is being definednot the timeline within which the code exists. For example, for the following to work, mShape would need to be nested within btRotate. Otherwise, the reference is undefined.

	btRotate.onRelease = function():Void {
	  this.mShape._rotation += 20;
	};

In addition to being able to refer to themselves, movie clips (and buttons) can refer to the movie clip object (if any) that contains them using the _parent property. For example, if mClip is on the main timeline, then mClip can target the main timeline to do things such as control the timeline playback. The following code applies an onPress( ) event handler method to mClip such that when it is clicked, the main timeline would stop playback:

	mClip.onPress = function():Void {
	  this._parent.stop();
	};

Tripod >> 3pod Botom Tips & Learning and manuals for educations

Previous Page
Next Page

 

bluedot bluedots greydots pinkdots

Tripod >> 3pod Tips & Learning and manuals for educations