Building a mobile portfolio can be a gentle introduction into mobile development. If you don't have experience in the mobile medium, this is a good first project. If you already have existing Macromedia Flash work, you're take the same content and present it on a device. You don't have to create new content from scratch.
The portfolio application you will build in this tutorial lets you load external SWF files. It resizes the content to fit as much of it as possible on your mobile device. It aligns your movies horizontally and vertically. You can also display your content on your device sideways, if necessary. Before going into the file, select Control/Test Movie to examine the look and function of the portfolio:
Figure 1. The mobile portfolio interface
Download the sample file archive to get a completed FLA, mobile_portfolio.fla,
along with some sample FLA and SWF movies to test with your portfolio.
Inside the mobile_portfolio.fla file, find a movie clip called menu_mc.
This movie clip contains a simple menu layout with sample buttons that
load external SWFs into the portfolio application.
Figure 2. The menu_mc movie clip, Frame 10.
The portfolio application code consists of four main functions:
You can find these functions in the first frame of the mobile_portfolio.fla file. To examine the code, open mobile_portfolio.fla, select the first frame in the Actions layer, and choose Window > Actions from the menu.
The menu_mc movie clip contains sample invisible buttons that use of
the getMovie, clearMovie, and quitPortfolio
functions. Here's the menu_mc movie clip on the main timeline:
Figure 3. menu_mc movie clip in main timeline
To examine the code on these buttons, double-click the menu_mc movie clip on the main timeline. (It is the small triangle in the upper left corner of the canvas.) To enter the timeline of the menu movie clip, select one of the buttons on the invisible buttons layer in the “on” section of the timeline (Frames 6-10), and choose Window > Actions.
Depending on your mobile device, the screen dimensions of your display may be landscape (wider than tall) or portrait (taller than wide). When building your portfolio, orient your content similarly. The first thing you need to do is to set up the framework for your mobile portfolio to play in. The initMobilePortfolio function defines the playback orientation, the background color of your movie, and some other settings. Here’s a step-by-step guide to coding the function:
Use the function with the following two parameters:
initMobilePortfolio ("horizontal", "vertical");
Declare the initMobile function and define two orientation parameters as follows:
function initMobilePortfolio (menuOrientation, uprightOrientation) {
Store the values for widths and heights of the movie clips you will load SWFs into:
horizontalPlaceHolderWidth = horizontalPlaceHolderTemplate._width; horizontalPlaceHolderHeight = horizontalPlaceHolderTemplate._height; verticalPlaceHolderWidth = verticalPlaceHolderTemplate._width; verticalPlaceHolderHeight = verticalPlaceHolderTemplate._height;
Duplicate the menu at a higher level so that it will appear above the movies you are going to load:
duplicateMovieClip ("menuTemplate", "menu", 100);
Hide the menu template movie clip:
menuTemplate._visible = false;
Store the value of the uprightOrientation parameter:
standardOrientation = uprightOrientation;
Store the y position after the rotation for the menu or loaded movie so that if the user rotates the mobile device, the movie will display properly while you hold it sideways:
if (standardOrientation == "vertical") {
yPositionAfterRotation = verticalPlaceHolderHeight;
} else {
yPositionAfterRotation = horizontalPlaceHolderHeight;
}
Rotate the menu if your portfolio should display sideways:
isRotateMenu = (menuOrientation != standardOrientation);
if (isRotateMenu) {
menu._rotation = -90;
menu._y = yPositionAfterRotation;
}
Set the default background color that the app uses whenever you clear a movie:
defaultBackgroundColor = 0x000000;
Instantiate a color object for the background color:
background_color = new color (solidColoredBackground);
Finish the function:
} //End function initMobilePortfolio
Here is the complete, commented code for the initMobilePortfolio function:
//menuOrientation - How you want the menu displayed. Landscape ("horizontal") or Portrait ("vertical")
//Accepts either "vertical" or "horizontal" as parameters
//uprightOrientation - When you hold the device in its regular position, is it Landscape ("horizontal") or Portrait ("vertical")
//Accepts either "vertical" or "horizontal" as parameters
function initMobilePortfolio (menuOrientation, uprightOrientation) {
//Store the values of the widths and heights of the movie clips we are going to load our swfs into
horizontalPlaceHolderWidth = horizontalPlaceHolderTemplate._width;
horizontalPlaceHolderHeight = horizontalPlaceHolderTemplate._height;
verticalPlaceHolderWidth = verticalPlaceHolderTemplate._width;
verticalPlaceHolderHeight = verticalPlaceHolderTemplate._height;
//We duplicate the menu template to a higher depth is so that it will appear above
//the movies we are going to load
duplicateMovieClip ("menuTemplate", "menu", 100);
//Hide our menu template
menuTemplate._visible = false;
standardOrientation = uprightOrientation;
//Store the y position that for the menu or loaded movie if it needs to be
//rotated so that it will display on your device while you hold it sideways
if (standardOrientation == "vertical") {
yPositionAfterRotation = verticalPlaceHolderHeight;
} else {
yPositionAfterRotation = horizontalPlaceHolderHeight;
}
//Rotate the menu if your portfolio is supposed to be displayed sideways
isRotateMenu = (menuOrientation != standardOrientation);
if (isRotateMenu) {
menu._rotation = -90;
menu._y = yPositionAfterRotation;
}
//Set the default background color - this gets used whenever you clear a movie
defaultBackgroundColor = 0x000000;
//Instantiate a color object for the background color
background_color = new color (solidColoredBackground);
}//End function initMobilePortfolio
When you trigger one of the buttons in your mobile portfolio to load a movie, call this function. It will do all the work for you. The getMovie Function will:
Here’s a step-by-step guide to coding the function:
Declare the get movie function and define the parameters as follows:
function getMovie (fileName, orientation, fileWidth, fileHeight, fileBackgroundColor, horizontalAlignment, verticalAlignment) {
Set the widths and heights for the SWF depending upon the orientation you are using (horizontal or vertical). Duplicate the vertical or horizontal placeholder and name the instance placeHolder. Duplicate the placeholder to the same depth so it will immediately overwrite any former pre-existing placeholder; this is easier than always tracking which separate placeholder you are using (vertical or horizontal). Insert a trace statement to warn you if you didn't specify the movie orientation as horizontal or vertical.
if (orientation == "horizontal") {
var orientationWidth = horizontalPlaceHolderWidth;
var orientationHeight = horizontalPlaceHolderHeight;
duplicateMovieClip ("horizontalPlaceHolderTemplate", "placeHolder", 1)
} else if (orientation == "vertical") {
var orientationWidth = verticalPlaceHolderWidth;
var orientationHeight = verticalPlaceHolderHeight;
duplicateMovieClip ("verticalPlaceHolderTemplate", "placeHolder", 1)
} else {
trace ('WARNING!!!: Did not specify the movie orientation as "horizontal" or "vertical"');
}
Reset the placeholder to the upper left corner:
placeHolder._x = 0; placeHolder._y = 0;
Decide if you should scale the movie. (Scale the movie if the movie you are loading has a larger width or height than the placeHolder movie clip.) Store the ratio of the dimensions of the movie you are loading relative to the placeHolder movieclip:
var fileToOrientationWidthRatio = fileWidth/orientationWidth; var fileToOrientationHeightRatio = fileHeight/orientationHeight; var isLoadedMovieWiderThanPlaceHolder = fileToOrientationWidthRatio > 1; var isLoadedMovieTallerThanPlaceHolder = fileToOrientationHeightRatio > 1; var isScaleLoadedMovie = isLoadedMovieWiderThanPlaceHolder || isLoadedMovieTallerThanPlaceHolder;
If you scale the loaded movie, determine whether you should scale the movie to the maximum width or height available. If the ratio for the movie width to the placeHolder movie clip width is greater than or equal to the ratio movie height to the placeHolder movie clip height, then scale the movie by the maximum width available. Otherwise, scale the movie by the maximum height available. Scale the container movie clip that is inside the placeHolder movie clip:
if (isScaleLoadedMovie) {
isScaleByWidthRatio = (fileToOrientationWidthRatio >= fileToOrientationHeightRatio);
if (isScaleByWidthRatio) {
var amountToScale = fileToOrientationWidthRatio;
} else {
var amountToScale = fileToOrientationHeightRatio;
}
var xScaleFactor = Math.floor( (100/amountToScale) );
var yScaleFactor = Math.floor( (100/amountToScale) );
placeHolder.container._xscale = xScaleFactor;
placeHolder.container._yscale = yScaleFactor;
}
Determine the width of the loaded movie and scale the movie horizontally:
var widthOfLoadedMovie = (fileWidth * (xScaleFactor/100)) || fileWidth;
switch (horizontalAlignment) {
case "left":
break;
case "center":
var widthDifference = Math.floor ( (orientationWidth-widthOfLoadedMovie)/2 );
placeHolder.container._x += widthDifference;
break;
case "right":
var widthDifference = Math.floor (orientationWidth-widthOfLoadedMovie);
placeHolder.container._x += widthDifference;
break;
default:
trace ("WARNING!!!: Trying to horizontally align the movie w/o a correct alignment parameter");
}
Determine the height of the loaded movie and scale the movie vertically:
var heightOfLoadedMovie = (fileHeight * (yScaleFactor/100)) || fileHeight;
switch (verticalAlignment) {
case "top":
//No need to do anything. By default, it is aligned vertically to the top
break;
case "middle":
var heightDifference = Math.floor ( (orientationHeight-heightOfLoadedMovie)/2 );
placeHolder.container._y += heightDifference;
break;
case "bottom":
var heightDifference = Math.floor ( orientationHeight-heightOfLoadedMovie);
placeHolder.container._y += heightDifference;
break;
default:
trace ("WARNING!!!: Trying to horizontally align the movie w/o a correct alignment parameter");
}
Color the background of the portfolio to match the color of the loaded movie:
background_color.setRGB (fileBackgroundColor);
Rotate the movie if the orientation for the loaded movie is not the same as the standard orientation for your device:
var isRotateLoadedMovie = (orientation != standardOrientation);
if (isRotateLoadedMovie) {
placeHolder._rotation = -90;
placeHolder._y = yPositionAfterRotation;
}
Load your movie:
loadMovie(fileName, "placeHolder.container");
Finish the function:
}//End function getMovie
Here is the complete, commented code for the getMovie function:
//filename - the name of the SWF file that you would like to load
//orientation - either "vertical" or "horizontal". Which template the SWF will load into and how the movie will be displayed.
//fileWidth - the original width of the SWF you are loading. You can find this by opening the FLA and selecting MODIFY/DOCUMENT.
//fileHeight - the original height of the SWF you are loading. You can find this by opening the FLA and selecting MODIFY/DOCUMENT.
//fileBackgroundColor - the background color of your movie as an rgb code. Again, you can find this by opening the FLA and selecting MODIFY/DOCUMENT.
//horizontalAlignment - "left", "center", or "right". How you want the file aligned horizontally.
//verticalAlignment - "top", "middle", or "bottom". How you want the file aligned vertically.
function getMovie (fileName, orientation, fileWidth, fileHeight, fileBackgroundColor, horizontalAlignment, verticalAlignment) {
//Set the widths and heights for the SWF depending upon which orientation you are using
//And we duplicate our vertical or horizontal placeholder and name the instance "placeHolder"
//We duplicate it to the same depth so it will immediate overwrite any former placeholder that
//was there. This is easier than always tracking which separate placeholder we are using (vertical or horizontal)
if (orientation == "horizontal") {
var orientationWidth = horizontalPlaceHolderWidth;
var orientationHeight = horizontalPlaceHolderHeight;
duplicateMovieClip ("horizontalPlaceHolderTemplate", "placeHolder", 1)
} else if (orientation == "vertical") {
var orientationWidth = verticalPlaceHolderWidth;
var orientationHeight = verticalPlaceHolderHeight;
duplicateMovieClip ("verticalPlaceHolderTemplate", "placeHolder", 1)
} else {
trace ('WARNING!!!: Did not specify the movie orientation as "horizontal" or "vertical"');
}
//Reset the placeholder to the upper left corner
placeHolder._x = 0;
placeHolder._y = 0;
//Figure out if we should scale the movie and if so, how
var fileToOrientationWidthRatio = fileWidth/orientationWidth;
var fileToOrientationHeightRatio = fileHeight/orientationHeight;
var isLoadedMovieWiderThanPlaceHolder = fileToOrientationWidthRatio > 1;
var isLoadedMovieTallerThanPlaceHolder = fileToOrientationHeightRatio > 1;
var isScaleLoadedMovie = isLoadedMovieWiderThanPlaceHolder || isLoadedMovieTallerThanPlaceHolder;
if (isScaleLoadedMovie) {
//Determine whether we should scale horizontally or vertically
isScaleByWidthRatio = (fileToOrientationWidthRatio >= fileToOrientationHeightRatio);
if (isScaleByWidthRatio) {
var amountToScale = fileToOrientationWidthRatio;
} else {
var amountToScale = fileToOrientationHeightRatio;
}//End if (isScaleByWidthRatio)
var xScaleFactor = Math.floor( (100/amountToScale) );
var yScaleFactor = Math.floor( (100/amountToScale) );
//Notice that the placeHolder isn't scaled. The movie clip instance inside it called "container" is scaled.
placeHolder.container._xscale = xScaleFactor;
placeHolder.container._yscale = yScaleFactor;
}//End if (isScaleLoadedMovie)
//Align the movie horizontally
var widthOfLoadedMovie = (fileWidth * (xScaleFactor/100)) || fileWidth;
switch (horizontalAlignment) {
case "left":
//No need to do anything. By default, it is aligned horizontally to the left
break;
case "center":
var widthDifference = Math.floor ( (orientationWidth-widthOfLoadedMovie)/2 );
placeHolder.container._x += widthDifference;
break;
case "right":
var widthDifference = Math.floor (orientationWidth-widthOfLoadedMovie);
placeHolder.container._x += widthDifference;
break;
default:
trace ("WARNING!!!: Trying to horizontally align the movie w/o a correct alignment parameter");
}//End switch (horizontalAlignment)
//Align the movie vertically
var heightOfLoadedMovie = (fileHeight * (yScaleFactor/100)) || fileHeight;
switch (verticalAlignment) {
case "top":
//No need to do anything. By default, it is aligned vertically to the top
break;
case "middle":
var heightDifference = Math.floor ( (orientationHeight-heightOfLoadedMovie)/2 );
placeHolder.container._y += heightDifference;
break;
case "bottom":
var heightDifference = Math.floor ( orientationHeight-heightOfLoadedMovie);
placeHolder.container._y += heightDifference;
break;
default:
trace ("WARNING!!!: Trying to horizontally align the movie w/o a correct alignment parameter");
}//End switch (verticalAlignment)
//Color the background to match the color of the loaded movie
background_color.setRGB (fileBackgroundColor);
//Rotate the movie if we need to
var isRotateLoadedMovie = (orientation != standardOrientation);
if (isRotateLoadedMovie) {
placeHolder._rotation = -90;
placeHolder._y = yPositionAfterRotation;
}//End if (isRotateLoadedMovie)
//Load the SWF
loadMovie(fileName, "placeHolder.container");
}//End function getMovie
Sometimes you may want to remove a movie that's playing in your portfolio. The clearMovie function will load an empty movie in the place one that may be playing and change the background color of the portfolio to match the default background color for the portfolio. Here’s a step-by-step guide for coding the function:
Declare the function:
function clearMovie () {
Load an empty movie into the container if you used the placeHolder:
if (placeHolder) {
loadMovie("movies/null.swf", "placeHolder.container");
}//End if (placeHolder)
Color the background of the portfolio:
background_color.setRGB (defaultBackgroundColor);
Finish the function:
}//End function clearMovie
Here is the complete, commented code for the clearMovie function:
function clearMovie () {
if (placeHolder) {
loadMovie("movies/null.swf", "placeHolder.container");
}//End if (placeHolder)
background_color.setRGB (defaultBackgroundColor);
}//End function clearMovie
Call the quitPortfolio function when you trigger the Quit button. The way you quit your portfolio will depend on what mobile device you are using. Here’s a step-by-step guide for coding the function:
Declare the function:
function quitPortfolio () {
Finish the function:
}//End function quitPortfolio
Here is the complete, commented code for the quitPortfolio function:
function quitPortfolio () {
//Place whatever code you need to execute when you quit the portfolio
}//End function quitPortfolio
Gather all of your SWF movies that you would like to be able to show on your mobile portfolio. Copy them all into a directory called movies that is in the same directory where you placed the mobile_portfolio.fla file. Change the paths to the SWFs in the button actions so your movies load. Adjust all the necessary parameters, such as original width, height, color, desired orientation, alignments, etc. For example, here is the code on one of the buttons located inside the menu_mc movie clip on the Invisible Buttons layer:
on (press) {
_parent.getMovie ("movies/movie9.swf", "vertical", 500, 200, 0x990066, "right", "bottom");
}
Once you've tested the portfolio on your desktop or laptop computer, you're ready to transfer it to your mobile device. Please refer to the CDK (content development kit) for your device as the procedure will differ depending on which mobile device you own. You can find information on content development kits at the Macromedia DevNet Mobile Topic Center.
A mobile portfolio can help you easily explain what you do for a living. It can help you get business and position yourself for exciting mobile Macromedia Flash projects.
Showing your work on a mobile device can also be a great way to see how your current and potential clients react to mobile content. People are usually more impressed when viewing content on a mobile device. You can gauge the client’s desire to deliver mobile content without investing too much time in new development. Plus, it can be a great way to justify buying that new mobile device you've been eyeing!
If a picture is worth a thousand words, imagine the value of having a mobile portfolio of your best work in your pocket. Create your own mobile portfolio today.