|
||||||
|
||||||
131Intermediate
Flash
Compatibility: MX 2004 Only For Flash MX Click Here
Editor: Phil Schulz
Written by: Joseph Levin of: Portal Web Services Email Joe
Length: 2100 words
Assumed Knowledge: Getting around the Flash menus, basic movie clips and some basic ActionScript.
Level: Easy / Medium
Access: Free Tutorial
![]()
Learn how to make a panoramic style view of an image that will scroll in as the mouse cursor moves. This same technique can be used to make a scrollable menu with a series of buttons.
Example: Download the Flash file
Int 131a
Example of the Panorama Movie with a Menu.
Note: This technique only works in Flash MX 2004 and not earlier versions.
Example: Download the Flash file Int 131b
Example of the Panorama Movie without a Menu.
Step One: Create the Layers





Step Two: Setting up the Scrollable Image
Note: You may use any image, graphic or even text so long as:
- It is much longer than the Stage or
- It is much taller than the Stage if you wish to scroll up and down.
Additional Notes: While it is not necessary to do so (but helpful if you want some more interactivity), you may also place additional movie clips, graphics, or text within the panorama_mc movie clip. In our case, we have borrowed clips from the "Tsunami Menu" tutorial and have roughly centered them about the center of the panoramic image; you may use your own clips, but here are the Tsunami Menu clips up close (do not worry about any Actionscript code for these clips yet except for what is explained later, below):
Cross Ref: Tsunami Menu
Note: panorama_mc is the Movie Clip's Instance Name that will be referred to later in ActionScript. The Movie Clip's Library Name (Panorama) cannot be used in the ActionScript.
You may place additional layers within your Movie Clip and these can include any content that you wish such as buttons.
Step Three: The textured background
You can see that the image ends up being curved and there is small
amount of background that is visible above and below the scrolling image. This
curve is false and if you wish you can leave out this section and have your
image scroll in a standard rectangle as below:
Example of the Flash Movie with the Curved top and bottom.
Example: Download the Flash file Int 131c
Example of the Flash Movie without the Curved top and bottom.
In the curved illustration above I you will place textured
background in the area. It is not essential as you could just have a plain
background colour or crop the movie so that the curvature is not visible.

Note: You may use any image you can right click (Mac Ctrl click) and save the image.






Step Four: The Mask

Note: When all the layers in the Mask group are locked you will not see the rectangle that you just drew as it is now working as a Mask. Note this lock effect make no difference to the final Flash Movie.
Step Five: Add additional frames
![]()
Fig. 5-1: Inserting new frames for each layer.
![]()
Fig. 5-2: Inserting a blank keyframe in Frame 2 of the actions layer.
![]()
Fig. 5-3: Your timeline after inserting the blank keyframe in Frame 2 of the actions layer.
Step Six: (optional) Create a new scene to hold your website content
![]()
Fig. 6-1: Scene dialog prior to adding a new scene.
![]()
Fig. 6-2: Scene dialog after adding a new scene.
![]()
Fig. 6-3: Scene 2 should be made to look like so.
![]()
Fig. 6-4: Place the label name "item1" for Frame 20 of the labels layer in Scene 2 into the Properties dialog box as shown.
Step Seven: The ActionScript
This step involves creation of the ActionScript.
Select frame
1 of the: Actions Layer and place the following
script:
stop(); var dist:Number; var fraction:Number; var decel:Number; var halfWidth:Number=(Stage.width/2); var newposx:Number=halfWidth;
var currposx:Number; var range:Number=(_root.panorama_mc._width-Stage.width)/2; var currtime:Number; var now:Number; var menutriggerdist=150; //variable strictly for "tsunami menu" in the "menu options" layer in panorama_mc var menumaxscale=300; //ditto var menumultiplier=1.5; //ditto //place any sort of movie clip or button within panorama_mc on the "menu options" layer, so long as you //do not place events on panorama_mc (such as onPress, onRelease, etc.) and the menu clips will work.
_root.onEnterFrame=function() { if (_root.borders_mc.hittest(_xmouse, _ymouse, true)==false) { //if cursor not over the borders_mc clip... distancex=_root.panorama_mc._x-newposx; _root.panorama_mc._x=_root.panorama_mc._x-(distancex/decel); _root.onMouseMove=function() { currtime=getTimer(); dist=_root._xmouse-halfWidth; fraction=Math.abs(dist/halfWidth); decel=(fraction)*15+5; if (dist<0) { newposx=halfWidth+(fraction*range); } else { newposx=halfWidth-(fraction*range); }
_root.currposx=_root.panorama_mc._x;
}
} else { delete _root.onMouseMove; _root.panorama_mc.onEnterframe=function() { now=getTimer(); var delay:Number=5; //in seconds decel=35; if (now>currtime+(delay*1000)) { //trace("now=" +now+ " lasttime=" +currtime); distancex=_root.panorama_mc._x-halfWidth; _root.panorama_mc._x=_root.panorama_mc._x-(distancex/decel); }
}
}
}
Select frame 2
of the: Actions Layer and place the following
script:
_root.panorama_mc._x=_root.currposx;
Place the following
script on any movie clip within panorama_mc (in our case the leftmost clip on the menu options layer):
Note: In our example, we placed code from the Tsunami Menu into each of the clips placed on the menu options layer inside of panorama_mc. You do not have to worry about this code unless you wish to emulate the wobbly scaling up/down that corresponds to the Tsunami Menu in action. The code that is important is the following, which should be placed on any movie clip you wish to have the user click on. In our example, I have used an on press event to move the timeline play head to the frame labeled "item1", which resides in Scene 2. It is also important to note that the frame labeled "item1" could reside in Scene 1 (it really doesn't matter), but you would have to at least add in more frames in Scene 1 to make room for "item1" frame in Scene 1.
on (press) {
_root.gotoAndStop("item1");
}
Select frame 20 of the: Actions Layer in Scene 2 and place the following
script:
stop();
newposx=halfWidth;
Select the button you will place on frame 20 of Scene 2 and place the following
script on that button:
on(press){
_root.gotoAndStop(2);
}
ActionScript Explained:
For Frame 1:
stop();
Stops the play head from moving.var dist:Number;
dist represents the distance in pixels from the centre of the panorama_mc clip (as well as the stage) to the _xmouse coordinate.var fraction:Number;
fraction represents the ratio of distance to half of the stage width, as measured from the centre of the stage. In other words, if your mouse cursor is 5/6th of the way across the stage, as measured from the left edge, it is 2/3rd of the way across the right half of the stage as measured from the centre of the stage. We are only interested in the fractional distance away from the centre of the stage.var decel:Number;
decel is an arbitrary number that affects how fast the panorama_mc clip scrolls. The larger decel is, the slower the panorama_mc clip moves.
var halfWidth:Number=(Stage.width/2);
halfWidth represents the coordinate of the centre of the stage.var newposx:Number=halfWidth;
newposx represents the goal, or final position of the registration point of panorama_mc. Here it is set to the centre of the stage; it will change later depending on the mouse position.var currposx:Number;
currposx represents the _x position of panorama_mc every time the mouse is moved. It is stored so as to set the position of panorama_mc should the timeline play head ever land on Frame 2.
var range:Number=(_root.panorama_mc._width-Stage.width)/2;
range represents the total range of possible movement of the panorama_mc clip.var currtime:Number;
currtime represents a timer value at the time the mouse is movedvar now:Number;
now represents a timer value that increments with your PC's clockvar menutriggerdist=150;
var menumaxscale=300;
var menumultiplier=1.5;
menutriggerdist, et al. represents variables used strictly for the "tsunami menu" in the "menu options" layer in panorama_mc. Check out the tutorial for the "Tsunami Menu" on this website!_root.onEnterFrame=function() {
Define an onEnterFrame function that will check to see if the mouse cursor is "hitting" the borders_mc movie clip, and what to do in either case.if (_root.borders_mc.hittest(_xmouse, _ymouse, true)==false) {
If the mouse cursor's x and y position registers as false when a hittest is checked for the movie clip borders_mc (i.e. the mouse is not overtop of borders_mc but is over the panorama)s.....do the following:distancex=_root.panorama_mc._x-newposx;
distance represents the distance in pixels from the panorama_mc's registration point _x coordinate to the goal coordinate of newposx._root.panorama_mc._x=_root.panorama_mc._x-(distancex/decel);
Change the _x position of panorama_mc by distancex/decel, one time every frame. Initially, newposx and the panorama_mc._x are the same and no motion of panorama_mc occurs._root.onMouseMove=function() {
If the mouse is moved....currtime=getTimer();
Find out what time it is each time the mouse is moved.dist=_root._xmouse-halfWidth;
Find out the distance the mouse cursor is from the center of the stage , in the _x direction.fraction=Math.abs(dist/halfWidth);
Calculate what percent (as a decimal fraction) the mouse is at as compared to half of the stage width.decel=(fraction)*15+5;
We want the deceleration number to vary based on the fraction, but we want it to have a lower limit of 5. i.e., decel will range from 5 to 20. When the mouse is at 100% fraction (at either right or left edge of the stage), the decel will be 20 (slower movement, but greater dist). If close to the centre of the stage, the fraction will be approx. 0, and decel will be approx. 5 (faster movement, but smaller dist).if (dist<0) {
If dist is negative (i.e., to the left of the centre of the stage), we want newposx (the goal position of panorama_mc._x) to be to the right of the centre of the stage.newposx=halfWidth+(fraction*range);
If your mouse is at 100% fraction (i.e., at either edge of the stage in the _x direction), make your goal 10% of the range of motion so that the entire panorama_mc may be shown. Similarly, when your mouse position is at 0% fraction (i.e., at the centre of the stage), you want panorama_mc_x to move to the centre of the stage (newposx will equal halfWidth).} else {
newposx=halfWidth-(fraction*range);
Otherwise, set newposx to be to the left of the centre of the stage._root.currposx=_root.panorama_mc._x;
Set the currposx variable to hold the current value of the _x location of the panorama_mc when the mouse is moved. This variable will be used in the Actionscript code in Frame 2 so that if a movie clip (a "Tsunami Menu" clip, for example) is pressed and the timeline goes to a different frame, upon return to frame 2, the panorama_mc's _x position is reset to currposx so that the panorama will be at the position it last was at when you return to the main panorama scene.} else {
delete _root.onMouseMove;
If the mouse cursor is overtop of borders_mc (i.e., not over the panorama_mc clip), delete the _root.onMouseMove function. This is done as you don't want the panorama to respond to mouse movement when the mouse cursor is not overtop the panorama_mc clip. However, we want the panorama_mc to move back to its original position if no mouse movement happens for a while, so we do this:
_root.panorama_mc.onEnterframe=function() {
Define a new onEnterFrame function....now=getTimer();
Keep track of the time since the mouse rolled out from panorama_mc.var delay:Number=5;
In seconds, the amount of time you want to wait until the panorama_mc clip gets moved.decel=35;
Sets an arbitrary deceleration value.if (now>currtime+(delay*1000)) {
If the now time is greater than the currtime variable....distancex=_root.panorama_mc._x-halfWidth;
...calculate the new distance, replacing newposx by your new goal, halfWidth..._root.panorama_mc._x=_root.panorama_mc._x-(distancex/decel);
...and move panorama_mc.
For Frame 2:
_root.panorama_mc._x=_root.currposx;
Sets the _x position of the panorama_mc to where it was last when the mouse was last moved.
For Frame 20 of Scene 2:
stop();
Simple stop action.newposx=halfWidth;
This sets the new target goal to be equal to halfWidth, but will only get executed when the timeline play head goes back to frame 2 of Scene 1 after the user has clicked on the button placed in Frame 20 of Scene 2.
For the button that was placed in Frame 20 of Scene 2:
on(press){
_root.gotoAndStop(2);
}
When the button is pressed, the onPress event will take the timeline play head to Frame 2. Do not confuse this with the Frame 2 of Scene 2. Based on the syntax of the gotoAndStop method, the "2" used as its argument is understood to mean the "global" frame 2, as if all frames in the movie were placed from left to right, starting with the frames of Scene1 and then Scene 2, and numbering the frames sequentially from 1 to the last frame from left to right.
Note: I have found that if you wanted to go back to Frame 1 (i.e., frame 1 of Scene 1) by using a "1" as the argument of the gotoAndStop method, then the entire Actionscript fails.
As it does no harm to go to Frame 2 (which works fine!) just return to the main panorama scene (i.e., Scene 1) by returning to Frame 2.
That's it so test your movie! As always, thank you for your time!
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
154877 visitors to this page since
Oct 04 •
|
|
|

Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend:
Click here
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|