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

Home > Articles > Design > Adobe Creative Suite

Flash Usability, Part 2: Making the Flash Part

  • Print
  • + Share This
In Part 1 you set the design guidelines to make the text size button usable when designing your Web site with Flash. Now learn about the JavaScript that makes it work in Part 2 of this four-part series.
This is the second article in a 4-part series. Read the first article here.
From the author of

Because the Flash movie that will be used to determine the new size percentage is small, let's get that out of the way first. Start by making a new movie in Flash, and give it a width of 150 pixels and a height of 20 pixels. Now create two text fields on the stage. Name the first one tf_height and the second one tf_percent. Add some static text labels to the stage so that you can easily tell which text field does what. (See Figure 1.)

Figure 1Figure 1 It's a good idea to label your fields so that you don't forget which one is receiving which value.

Next, select the fields and labels on the stage, and make them into a movie clip symbol (choose Insert, Convert to Symbol, or press F8). Name the movie clip Output Fields, and give it an instance name of mc_fields. You will add some code later to change the visibility of this clip so that you can see the fields when debugging your project but hide them when your project is ready for prime time.

Now add a new layer and call it actions; this is where all the ActionScript for this movie will go. (See Figure 2.)

Figure 2Figure 2 The actions layer is where all the ActionScript for this movie will go.

Select the first frame in the actions layer and add this code:

Stage.scaleMode = "noScale";

This line references the Flash MX Stage object and sets the scaleMode of the movie to noScale. This enables you to change the height of the Flash movie in the Web page using its OBJECT or EMBED tag and to have the Stage.height property return the actual height of the movie. If you left out this line of code, the Stage.height property would return the height that you originally set the stage to—in this case, 20.

Add this code next:

if (debug != "true") {
  mc_fields._visible = false;
}

This statement checks the value of the debug variable. This variable is passed into the Flash movie via the movie's Query String (see the accompanying box, "Using the Query String," for more information on using the Query String). If the debug variable is not equal to the string true, the movie clip containing the testing fields is hidden. Otherwise, if the variable equates to true, the mc_fields movie clip is left visible on the stage.

Using the Query String

The debug variable actually gets passed in through the HTML Object or Embed tag. This is a great feature that has been in the Flash player for several versions. It enables you to easily pass variables to your Flash movie without needing to use JavaScript. Specifically, the debug variable gets passed into Flash via its Query String (the part of a URL that follows the question mark—for example: http://www.skip-intro.org/index.html?someVariable=someValue). Flash will see this variable as if you defined it in ActionScript, and you can access that variable in Flash to get the value of it.

Next, add this code:

function onEnterFrame() 
{
  var newHeight = Stage.height;
  
  if (oldHeight != newHeight) {
     var percentScale = ((newHeight / parseInt(startHeight)) * 100);
   
   var sizerLC = new LocalConnection();
  
   sizerLC.send("textSizerChannel", "sizeText", percentScale);
   delete sizerLC;
  
   mc_fields.tf_height.text = newHeight;
   mc_fields.tf_percent.text = percentScale + "%";

   oldHeight = newHeight;

  }   
}

The first line in this segment of code creates a function called onEnterFrame. This function is actually called by Flash automatically every time it "refreshes" the screen, which is based on the frame rate of your movie. Flash defaults to 12 frames per second; we haven't changed it for this movie, so the code in this function will be called 12 times a second, even though the movie has only 1 frame and is stopped. Wow, that's neat!

Next, set the newHeight variable to the current height of the stage. This variable is used in the next line when you compare the current height of the stage to the value of the oldHeight variable. If the two values are not equal, the code inside the if statement gets executed. Notice that you have not yet defined the oldHeight variable; when the movie starts playing, the body of this if statement gets executed at least once.

The next line sets the percentScale variable to the result of dividing the current height of the stage by another value passed in using the Flash movie's Query String, startHeight. You pass in this value to tell the Flash movie what the normal element size is so that you can adequately calculate the percentage by which the DIV element has changed. The percentScale value is passed in via the Query String because different browsers start off with different text heights as the normal size—but we'll cover that a little later.

Next you create a new LocalConnection object. This object enables you to send information to other Flash movies playing on the user's machine. That's right! Not just movies playing in the same Web page, or the same browser window, or the same browser at all. Any Flash MX movie can receive messages from any other Flash MX movie playing on the same computer using the LocalConnection object.

After creating the new LocalConnection object, you send the percentScaled value to your main movie using the send() function of the LocalConnection object you created. The send() function takes three parameters:

  • The name of the connection. This is like a channel that your main movie can subscribe to. In this case, you'll the channel textSizerChannel.

  • The name of the function to call. As you will see when you are ready to test the text size getter workaround, you must create a function in your main movie that will receive the messages from the sizeGetter movie. In this case, you will create a function called sizeText. You will make a sample main movie a little later.

  • The value to pass the function. Here you use the percentScaled variable that you calculated earlier. This value gets sent as a parameter to the function that you specify in the send parameter.

After sending the value to the remote function, you delete the sizerLC LocalConnection object, update the onscreen text fields so that you can see the numbers changing, and set the oldHeight variable to the current height of the stage, newHeight.

Believe it or not, that is all that needs to be done for the Flash part of this workaround. Be sure to save your movie. Because this movie relies on some information to be passed in via the Query String method mentioned above, testing your movie won't yield much. Still, it's a good idea to test it (Control+Enter in Windows, or Command-Enter on the Mac) and check the Output Window to see if you have any errors in your ActionScript. If you are error-free and ready to move on, export your movie as sizeGetter.swf.

Now let's move on to the JavaScript portion of the workaround.

  • + Share This
  • 🔖 Save To Your Account