Login | Register

Telling the target - the right way (Flash 4)

By this tutorial, you should know how to use simple tellTarget commands to play and stop a MovieClip. This chapter will teach you more advanced use of targeting, and explain the full principles behind it.

28. AUGUST 2000, Jens Chr. Brynildsen Observe: this tutorial teaches Flash 4 style actionscript. It will be rewritten to Flash 5/MX style when time allows

Open the Excavator-file again. The targeting for the excavator works just the same way. If you would "play" the shovel on the excavator, your tellTarget would look like this:

tellTarget ("0/1/2/3") {
play ();
}


Drag a button to the stage, and try it out for yourself using what you learned in the previous chapter. While you are at it, add another button to stop the shovel from playing also. What does the "0/1/2/3" mean? Look at the illustration below. It's the content of the "Insert target path"-window, and displays the way MovieClips are nested in the excavator.fla document.

Here is a short overview of the nesting that you explored in the previous chapter:

this -> The stage of the "excavator.fla" document
0 -> The complete excavator
1 -> The arm of the excavator
2 -> The outer part of the arm the shovel
3 -> The shovel alone

The four "Lever" MovieClips are the levers that are used to control the timelines in the other MovieClips. When your buttons target is set to "0/1/2/3", any command placed between the beginning and ending parentheses of the tellTarget command will affect the last named MovieClip, in this case the shovel (3). If you want to play the MovieClip containing the movement of whole arm, your target would be "0/1".

This is the simple way of telling targets, now for the more advanced.

Absolute or Relative targets
If you have ever inserted a button inside a MovieClip, you may have found that when you open "Insert target path"-window, only MovieClips that are above this MovieClip in the hierarchy are displayed. The reason for this is that the MovieClip that you are working on, may reside many places in your finished movie, and a tellTarget that is valid for the shovel while its inside the other MovieClips, might not work at all when it's placed directly on the stage.

Look at the excavator file once more. The shovel MovieClip is also used below one of the levers to illustrate that this lever will move the shovel. If the shovel MovieClip contained a command that should do something to the arm above it, something weird might happen when it tried to find the same target from the instance that resides on the main stage.

This potential error could have been avoided if Absolute targeting were used.

There are two ways of telling targets, Absolute and Relative. If you know HTML, you will find this system very familiar. The logic is as follows:

Relative targeting:
../ => go back one level to the MovieClip below
../2 => go back one level and enter a MovieClip with an instance name "2"

Absolute targeting
/ => go to the root (the outermost level) of this movie
/2 => go to the root and enter a MovieClip with an instance name "2"

Let's look at this in our excavator example. Inside each of the levers, there are two MovieClips. Each of them contains buttons that will trigger some movement of the excavator. Open the left-right lever (instance name: "Lever0") by double-clicking it. Now open the leftmost MovieClip residing on the "Invisible buttons"-layer.

These MovieClips are not visible in the "Insert target path"-window since they don't have any instance names, but they are still there. This MovieClip consists of two frames, each containing an invisible button (displayed as cyan, half-transparent squares). Both buttons have the same commands.

Click one of the buttons and open the Actions panel. The script looks like this:

on (rollOver) {
tellTarget ("../../0") {
prevFrame ();
}
tellTarget ("../") {
gotoAndStop (3);
}
play ();
}

on (rollOut) {
tellTarget ("../") {
gotoAndStop (2);
}
}

Look at the "Insert target path"-window to the right. This button is placed inside "Lever0". To make this button affect the MovieClip named "0" we must go out two levels to get to the main stage "this", and then enter the "0" MovieClip. The command on the button says tellTarget ("../../0"). The first "../" will go out one level. If we add another, it will go out to the main stage. By adding the "0" we will enter the excavator MovieClip.

The exactly same effect could have been achieved by using this command: tellTarget ("/0"). This is less text to type. Why did we not do it this way instead? There are a few rules for when to use Absolute or Relative targeting. You'll discover most of these yourself, but on the next page, we have compiled a short list for you.

Use Relative when:
·This flash-movie is going to be loaded into another flash-movie. Any reference to the root of the movie ("/") will then be wrong
·It saves some typing

Use Absolute when:
·You want a MovieClip used in many places to always point to a specific target
·It saves some typing

Menu-example
Let's put your new knowledge to use. Download the file NavComplete.fla. This is a simple menu system. Try it out. Clicking the button at the bottom of the screen will make a console enter the stage from the left. Moving the mouse over the upper button will display a sub-menu and clicking it will retract the console.

Now it's your turn to make this work. Download the file Nav.fla. Let's examine the components of this movie. Double-Click the console to look inside it. Click and drag above the timeline to examine the animation.

This is the animation of the console moving back and forth. In the "Actions" -layer, there are "Stop"-commands at the start and at the middle. The lower layer contain the bars "pushing" the console, and the middle layer contains the console itself. Open this MovieClip by double-clicking it.

The console consists of three layers. The upper level contains the five buttons, the middle contains the graphics that make up the console and the lower contains another MovieClip. This is the submenu moving in/out when the mouse is above the button.

To examine the submenu MovieClip, double-click it. (You may want to lock the two upper layers to be able to hit it) This animation is very similar to the movement of the console, containing "Stop"-commands at the start and in the middle. Go to the main stage by clicking the "Scene 0" button, above the timeline.

Now it's time to do some ActionScripting. Let's start by recapping the important things from the previous chapter.

1. The tellTarget command tells us which timeline we want to work with. All symbols in Flash have their own timelines, but only the timeline of MovieClips can be targeted.

2. Any command executed inside a tellTarget command will happen to the targeted MovieClip.

3. To be able to target a MovieClip, we must first give it an instance name.

If you want to try doing this without any guidance, stop reading here. If you want to go through it step by step, read on.

The first thing we want to do is to make the button start the animation of the console. To be able to do this we must first give an instance name to the MovieClip. Right-click the console to bring up the "Instance" menu. Name the instance of the complete console "complete".

Click the button at the bottom of the screen and bring up the Actions panel. Add the necessary tellTarget command:

on (release) {
tellTarget ("complete") {
play ();
}
}


Flash may suggest that you add a trailing slash ("/") to the target. We'll remove this slash to make sure our Flash-file can be loaded into another Flash-movie without any problems. Test the movie and see it work.

Now it's time to add the actions for the sub-menu. Enter the "complete console" MovieClip by double-clicking it. Then enter the "Console" MovieClip doing the same (If you want to, you can name this MovieClip also, but it's not needed for this example). You are now where the buttons are residing.

Click the upper button and enter the command that will return the menu in the Actions panel:

on (release) {
tellTarget ("../") {
play ();
}
}


What is done here is telling the target of the MovieClip residing below this MovieClip. As you remember the statement "../" will go back one level. The "Play"-command will start playing the timeline inside the "complete console". Test the movie to see it working.

Now we'll add the movement of the submenu. To be able to target it, we must first give the instance a name. In the "instance" panel, name it "sub1", then click the button that trigs it again. In the Actions panel, click the last line of script.

Unless you do this, your new tellTarget will appear within the "on (release)"-statement. Add a new tellTarget command, and choose the "sub1" MovieClip. Add a gotoAndPlay-command starting at frame 2. Then click the "on"-statement to make it react on "rollOver" instead of "release".

On (rollover) {
tellTarget ("sub1") {
gotoAndPlay (2)
}
}


Repeat as above for the "rollout":

On (rollout) {
tellTarget ("sub1") {
gotoAndPlay (8)
}
}


The complete script should now look like this:

on (release) {
tellTarget ("../") {
play ();
}
}
On (rollover) {
tellTarget ("menu1") {
gotoAndPlay (2)
}
}
On (rollover) {
tellTarget ("menu1") {
gotoAndPlay (8)
}
}


Test your movie and see it all work.

Now try it all for yourself
Create your own Flash-file containing multiple MovieClips nested and controlled with tellTarget commands. Try making a more advanced menu-system or maybe "the big machine that really does nothing - just looks important"?

It's important that you experiment with this on your own. This way, you will encounter problems that need to be solved, and this will extend your knowledge even further. As you start creating systems based on advanced use of MovieClips, you'll discover that you might need something to track if a button has been pressed or not. This will be covered in the next lesson about "variables".

 

About Jens C Brynildsen

Jens has been working with Flash since version 3 came out. Since then, he's been an active member of the Flash community. He's created more than a hundred Flash games (thus the name of his blog) but he also creates web/standalone applications, does workshops and other consulting. He loves playing with new technology and he is convinced that the moment you stop learning you die (creatively speaking). Jens is also the Editor of this website.

Get new stories first

Click to follow us on Twitter!

 

Comments

No comments for this page.

Submit a comment

Only registered members can comment. Click here to login or here to register