Publishers of technology books, eBooks, and videos for creative people

# The Flash 'Slide' Algorithm

• Print
This chapter is from the book

## Creating the Function for X and Y Movement

This function is basically the same as your previous function, except you specify a target X position and a target Y position for the movement. This means you can move panels diagonally as well as up and down or left and right. The X and Y target positions are defined as new parameters in your function definition as follows:

`function movePanel(x,y,whichClip,baseRate)`

When you call the function, you set the X and Y positions to where you want to move the panel as follows:

`function movePanel(100,300,this._name,1.6)`

Before, the panel headed towards the X position of the mouse, this time it moves towards the value of X and Y.

The full function follows. The Y movement is the same as the X movement, only this time you alter the _y property. Add the following code to the Script window:

```function movePanel(x,y,whichClip,baseRate) {
if (_root[whichClip]._x > x) {
difference = _root[whichClip]._x - x;
rate = difference / baseRate;
_root[whichClip]._x = _root[whichClip]._x - rate;
}
if (_root[whichClip]._x < x ) {
difference = x - _root[whichClip]._x;
rate = difference / baseRate;
_root[whichClip]._x = _root[whichClip]._x + rate;
}
if (_root[whichClip]._y > y) {
difference = _root[whichClip]._y - y;
rate = difference / baseRate;
_root[whichClip]._y = _root[whichClip]._y - rate;
}
if (_root[whichClip]._y < y ) {
difference = y - _root[whichClip]._y;
rate = difference / baseRate;
_root[whichClip]._y = _root[whichClip]._y + rate;
}
}```

#### Calling the Function

You need to create a script loop that constantly calls the function and moves the panels into their target positions when a button is clicked. You could do this with a conventional movie clip script loop, but instead you'll do what you did in the previous example and use an onClipEvent handler on the pictures movie clip itself:

1. Select the pictures movie clip. Use the Instance panel to name it pictures1.

2. With this movie clip still selected, press Ctrl+Alt+A (Cmd+Opt+A on Mac) to display the Object Actions panel for this movie clip. You can add the onClipEvent(enterFrame) handler in this Script window.

3. The main point of this script is to loop around and call the function each time so that it moves the panel to its target destination. But you only want it to do this when a button is clicked. Because of this you are going to use a variable of type boolean. This type of variable can only be set to true or false. When you click a button, you will set a variable called pressed to true. If this variable is true, you know you need to move the panel.

`if (_root.pressed == true) {`

Accordingly, call the function as follows:

`_root.movePanel(_root.targetx,_root.targety,this._name,1.5);`

The full script for this is as follows:

```onClipEvent(enterFrame) {
if (_root.pressed == true) {
_root.movePanel(_root.targetx,_root.targety,this
._name,1.5);
}
}```

Your code should look like Figure 3.24.

Figure 3.24. This is the code you will need to begin to call the functions.

Notice that when you call the function, you don't actually specify the target X or Y position. You tell it to use the values of two variables—targetX and targetY. These variables are set on the main timeline when a button is clicked, so you can easily alter the destination of the panels with the ActionScript you attach to the buttons.

Of course, you need to set the value of pressed to false when the movie first starts; otherwise, the panels will start to move immediately, rather than waiting for a button click. To set it to false, open the script in the functions layer and add this line of code above the function:

`_root.pressed = false;`

#### Triggering the Function with a Button

1. Create a new layer and name it buttons.

2. Draw a small box on the stage in this layer (see Figure 3.25).

3. Figure 3.25. Buttons can be programmed to call a variety of functions, including multiple functions with a single click.

4. Select this box and turn it into a button by pressing F8 and choosing the Button radio button. Name it button. Click OK.

When this button is clicked, it needs to do three things. First, it must set the value of the targetx variable. If you don't actually want to move it across the X-axis, you can simply tell it to stay put by using the panel's current X position as follows:

`_root.targetx = pictures1._x;`

Next, set the value of the targetY position as follows:

`_root.targety = 0;`

Finally, set the value of pressed to true as follows, causing the function to kick in:

`_root.pressed = true;`

This all needs to go in an on(release) event handler. To do this, select the button and press Ctrl+Alt+A (Cmd+Opt+A on Mac) to display the Object Actions panel for this button. Add the following script:

5. ```on (release) {
_root.targetx = pictures1._x;
_root.targety = 0;
_root.pressed = true;
}```

So far you've created a movie clip with your pictures in it, created the main function, and made a button to move the panel. For your transition effect to work, you need to have two identical panels—the second moving slightly more slowly than the first.

1. Select the pictures1 movie clip and copy it (Ctrl+C [Cmd+C on Mac]).

2. Underneath the layer with the pictures1 movie clip on it, create a new layer and name it pictures2. (see Figure 3.26).

3. Figure 3.26. Make sure this layer is underneath the pictures1 layer.

4. Paste the copied movie clip into this layer (Ctrl+Shift+V [Cmd+Shift+V on Mac]). You now have the second copy of the pictures movie clip in place. You might want to turn off the visibility for the pictures1 layer for the time being so you can just see this new clip (see Figure 3.27).

5. Figure 3.27. Turn off the visibility of the pictures1 layer to see the layer underneath, or alternatively turn that layer into an outline.

6. With this new, copied panel selected, rename it in the Instance panel as pictures2. Each clip should have its own name so that it doesn't confuse the script.

7. Now that you have the second panel in place, you need it to move more slowly than the first panel. All you have to do is alter the movePanel function call attached to this clip.

8. With the clip still selected, press Ctrl+Alt+A (Cmd+Opt+A on Mac) to display the Object Actions panel. You should see that it already has an enterFrame event handler attached to it. This is because this clip is a copy of the original.

9. To slow this clip down, increase the number at the end of the function call. Remember, the higher the number, the more slowly the panel moves. The original was set to 1.5, so set this to 2 (see Figure 3.28).

Figure 3.28. Play around with these numbers to alter the speed of the effect.

You next need to create the actual mask shapes for the two picture layers. Remember that the two masks must form a whole when shown together. Other than that, they can be any shape you desire.

1. Create a new layer above the pictures1 layer and name it mask1. You will create the first of the two masks on this layer.

2. Draw a rectangle on this layer. Use the Info panel to make the rectangle 225x40 (see Figure 3.29).

3. Figure 3.29. The rectangle you create here begins the mask creation.

4. With this rectangle still selected, use the Align panel to align it to the upper edge and center it on the stage (see Figure 3.30).

5. Figure 3.30. After the rectangle is aligned on the stage, you then begin the replication.

This shape needs to be copied four times, with each of the five blocks spaced evenly across the vertical space of the stage.

6. With the rectangle selected, copy it (Ctrl+C [Cmd+C on Mac]).

7. Paste it in place (Ctrl+Shift+V [Cmd+Shift+V on Mac]).

8. Without deselecting this new rectangle, use the arrow keys to move it underneath the original (see Figure 3.31).

9. Figure 3.31. If your alignment matches this figure, you're ready to copy and space the new rectangles.

Repeat this procedure for this new rectangle until you have five rectangles spread out across the stage from top to bottom. Align the lower-most rectangle so it's at the bottom of the picture. You'll want to space them evenly across the stage. To do this, select all the rectangles. The best way to do this is to lock all the other layers and press Ctrl+A (Cmd+A on Mac) to select all the objects in the mask1 layer. Now use the Align panel and make sure the To Stage option is depressed. Click the Distribute Vertical Center button, and the rectangles will be spaced evenly in the vertical direction (see Figure 3.32).

Figure 3.32. Make sure the To Stage button is depressed.

10. To turn this layer into a mask for the pictures1 layer underneath, first deselect all the panels on the mask layer by pressing Ctrl+Shift+A (Cmd+Shift+A on Mac).

11. Right-click (Ctrl+click on Mac) the mask1 layer name and choose Mask from the popup (see Figure 3.33).

12. Figure 3.33. After you have masked off the picture...

The picture on the pictures1 layer is now masked off, with the only areas showing through now being the areas defined by the rectangles. To see the mask effect, turn off the visibility of the pictures2 layer (see Figure 3.34).

Figure 3.34.  ...you can evaluate the mask effect.

You also need the pictures2 layer to be masked in exactly the same way, but its mask should be the opposite of the pictures1 mask. That is, to the point where you can't see the image in pictures1; you should be able to see the image in pictures2.

13. Unlock the mask1 layer, and select all five rectangles and copy them (Ctrl+C [Cmd+C on Mac]).

14. Lock that layer again to turn the mask back on. Above the pictures2 layer, add a new layer and name it mask2.

15. Paste the copy (Ctrl+Shift+V [Cmd+Shift+V on Mac]) of the five rectangles into this layer. You won't see them appear, however, because they will be covered by the pictures1 layer above them.

16. Use the arrow keys to move all five rectangles down so you can see them (see Figure 3.35).
17. Figure 3.35. If you now are looking at all five rectangles, creating the mask is simple.

18. Finally, turn this layer into a mask for the pictures2 layer by repeating Steps 7 and 8.

Now you have your two movie clips masked off with alternate masks, and you have a button in place to move the strip upward. What you need now is a button to move the strip back down again.

1. In the button layer, select the small button, copy it (Ctrl+C [Cmd+C on Mac]), and then paste it in place (Ctrl+Shift+V [Cmd+Shift+V on Mac]).

2. Use the arrow keys to move it below the original button (see Figure 3.36).

3. Figure 3.36. Building the down button is the final step.

4. With this new button still selected, press Ctrl+Alt+A (Cmd+Opt+A on Mac) to open the Object Actions panel.

5. In the on(release) script, change the targetY position value to 300. This means that when this button is clicked, the panel is moved so that its Y position will equal 300. The full code for the button is as follows:

6. ```on (release) {
_root.targetx = pictures1._x;
_root.targety = 300;
_root.pressed = true;
}```
7. Now test the movie. Click on the top button and the panel will slide upwards, giving a venetian blind type transition effect. Click the bottom button and it slides back to its original position (see Figure 3.37).

8. Figure 3.37. If your code is in place, you should be able to move the panels up or down.