* Google News *

Saturday, September 5, 2009

Masking Dynamic Text and Device Fonts

Masking Dynamic Text and Device Fonts

Why can't I mask dynamic text or device fonts yell's the inspiring flash developer!
It makes no sense! I can mask static text, why not dynamic text or device fonts?
Well Macromedia has a somewhat of an explanation here, but I'm going to try and explain this the best I can.

Basically you can only mask device fonts by using an actual movie clip as a mask.
You must also embed your dynamic text into a movie as well. You might not be used to this method as it's so convenient to use the timeline mask and most of the work you probably did in the past never required masking dynamic data loaded into a text field or text area. It's becoming so easy to generate external data in flash.

Here is an example of what we will create. A simple dynamic textarea with some text inside it with a mask applied to it:

[ an example of what you will create ]

Here is How
The following steps will explain what this tutorial will be trying to explain.

  1. Create a new flash document.

  2. Create a new layer and call it textarea.

  3. Draw a text area where you're dynamic text will be stored with your text tool.

[grab your text tool, click on the stage and draw your textarea]

  1. When your finished drawing the text area, open your properties panel
    and select "Dynamic Text" and "Multiline".


  2. Ok now we are going to convert the dynamic textarea into a movieclip . So hit F8 on your keyboard and call the movieclip main_mc.

    Also, you should embed your fonts. Click the Character button. The Character Options dialog box will appear. Check the option for Specify Ranges and select Uppercase, Lowercase, and Numerals if your data may contain numbers:



[ make sure to embed your fonts ]

  1. Give your movieclip and instance name of main_mc as well.

  2. Create an additional layer in your timeline and call it mask. Draw a box covering the main_mc movieclip with the proper dimensions.
  3. Again we are going to convert the box to a movieclip. So hit F8 and call our movieclip mask_mc. Also be sure to give your new movieclip an instance name of mask_mc.
  4. Now we just have to add some actionscript to make it all work. Create a new layer and call it actions. Click inside the 1st frame of your timeline and open your actions pallet. Type the following script into your actions panel:
main_mc.setMask(mask_mc);

Details about setMask:


Availability : Flash Player 6

Usage : main_mc.setMask(mask_mc)

Parameters:
main_mc The instance name of a movie clip to be masked.
mask_mc The instance name of a movie clip to be a mask.

Pausing Animation

Pausing Animation

Ever wanted to pause at the best bit in your animation? Or maybe let the viewer pause, and look in detail at your well crafted animation? Well in this tutorial, I shall be showing you how to add a Pause, and play button into your animation!

[This is what you will be creating]

How To Make It Happen

  1. Firstly, download and open the .FLA file into flash. This will have the bare bones of what we need to make in the example. If you wish to not use the example at all, this will also be very easy to do, and i shall explain what to do along the way. We now have to go to our library folder, so go to Window/ Library, or press CTRL+L to access it alternatively. Now drag and drop the two buttons into place in the Button layer, and put them where you feel necessary*.
  2. Now that we have the animation open, you will see that I have already created the animation for you! This is where, if you are not following the example file, you can go wild and create something really cool and special! But please make sure to stay within the animations layer.

    [ Try to stay only within the animations layer for your designs]

  3. We now have to go to our library folder, so go to Window/ Library, or press CTRL+L to access it alternatively. Now drag and drop the two buttons into place in the Button layer, and put them where you feel necessary*.

[*If you are not following the .FLA File , You should create two buttons called Play and Pause]

  1. You now have to highlight the Pause button, and input this code into the Actions Panel.
on (press) {
stop();
}
  1. Highlight the Play button and input this code into the Actions panel
on (press) {
play();
}
  1. Time to test your animation. You should see a blue square morph into an orange circle and back again, and if you press the pause button, it should stop the animation on its current frame, then press play to resume the action.

Congratulations! You have just incorporated buttons into your animation!

The Theory

on (press) {
stop();
}

What this does, is it tells the animation to stop on it's current frame. OnRelease tells us that, when the button is released, it will do the next command. The next command is Stop(); which tells it to stop on the current frame.

on (press) {
play();
}

This tells the movie to play again. As explained above, OnRelease tells the button that, once it is released, it should do the next command. Play(); tells the animation that it can start to run again.

Note
If your animation didn't work, are you sure that you stuck to the guidelines of the tutorial clearly?

Did you try to jump into the deep end straight away and try to make the whole thing all by yourself? Why not complete the tutorial as it is meant to be done, and then make it your own way?

You can download the final source also:

Download ZIP

Complex Button RollOver/RollOut Effects

Complex Button RollOver/RollOut Effects

It seems everywhere you look now has some form of complex button. Most buttons that have an animation when rolled over and an animation when you roll out are done with some form of this concept that I intend to share with you.

If you understand basic Flash concepts and just a smidgen of Actionscript, you will catch on in no time.

Before we get started, take a look at what will be created. It may not look like much, but from this one simple concept, sites like " The Life Aquatic" movie site are possible.

[ an example of what you will create ]

Here is How

  1. Create a new Flash document.

  2. Open up the document properties by pressing ctrl+j (cmd+j on Mac) and change the stage size to 100x100. Change the Frame Rate to 30.

  3. Rename layer 1 on the main timeline to complexButton. The main timeline should now look like this:

  1. Create a new movieclip by pressing ctrl+F8 (cmd+F8 on Mac). Name it mc_complex.

  2. Double click mc_complex in the library to edit the symbol. Select the Oval Tool(o) and make a 30x30 circle on the stage. In the properties panel set the x and y values for that circle to 0.

  3. Double click the circle to select the Fill and Stroke. Press F8 to convert it to a symbol so we can tween it. Make it into a graphic symbol and name it gr_circle:

  1. Still editing the mc_complex movieclip select frame 15 and press F6. This inserts a keyframe. Resize the circle a little bigger than what is there. I went with 40x40.

  2. Right click on any frame between the two keyframes on the timeline and select Create Motion Tween.

  3. Create a new layer and call it actions. Insert a blank keyframe(F7) on frame 15. The timeline for mc_complex should now look like this:

  1. Select the first frame of the actions layer. Type this code in the actions panel:

//stop();
this.onEnterFrame = function(){
if(rewind == true){
prevFrame();
}
}
this.onRollOver = function(){
rewind = false;
play();
}
this.onRollOut = function(){
rewind = true;
}
this.onRelease = function(){
getURL("http://www.kirupa.com","_blank");
}
  1. Select frame 15 of the actions layer, Type this code in the actions panel

stop();
  1. Navigate back to the root timeline. Put an instance of mc_complex from the library onto the stage.

  2. Save the flash document and test your movie(ctrl + enter). There you have it, a button that animates on rollover and on rollout.

Now lets look at the code:

stop();
this.onEnterFrame = function(){
if(rewind == true){
prevFrame();
}
}

The first stop command just stops the movieclip from playing (so the animation doesnt loop). What the onEnterFrame event handler is doing is checking to see if rewind is set to true. If it is, the animation plays backwards until it reaches a stop.

this.onRollOver = function(){
rewind = false;
play();
}

The onRollOver event handler does two things, it sets rewind to false so that the onEnterFrame event handler doesn't execute the prevFrame script. The other thing the onRollOver does is play the animation. So when you roll over the button, rewind gets set to false, then the animation plays and you see the button grow until the playhead hits the stop command on frame 15.

this.onRollOut = function(){
rewind = true;
}
this.onRelease = function(){
getURL("http://www.kirupa.com","_blank");
}

This last chunk of code is rather simple. When you roll out of the button, rewind gets set to true. So now the onEnterFrame will execute the prevFrame (becuase rewind is equal to true) until it reaches a stop. The onRelease event is where you place your code for when the button is clicked.

Download FLA

Duplicate Background

Duplicate Background

While scanning through the various threads in the Flash MX 2004 forum, there was a question asking whether it was possible to tile a background in Flash. This tutorial is an extension of the answer I wrote for that question, and I think there are some interesting concepts in the code that may benefit you in more applications beyond simple background duplication.

The following is an example of a small background image automatically tiled using some ActionScript in Flash:

[ an example of what you will create ]

Here's how:

  1. First, create a new animation in Flash. Set the width of your movie to 300 pixels and set the height of your movie to 200 pixels.
  2. Now that you have your movie setup, you will need to find a background tile. Copy and paste the following image into Flash:

[ pattern courtesy of squidfingers ]

  1. After you have pasted that image in Flash, select the image and press F8 (Insert | Convert to Symbol). The Convert to Symbol dialog box will appear. Give the movie clip the name 'tile'. Don't press the OK button just yet:

[ the Convert to Symbol dialog box ]

  1. While still on the Convert to Symbol dialog box, press the Advanced button to display more options. Check the box for "Export for ActionScript" and enter square in the field for Identifier. After you have done that, press OK to close the Convert to Symbol dialog box.

[ enter square in the Identifer field for your tile movie clip ]

  1. Your newly converted image should still be selected. Check your Properties panel's Width and Height text fields towards your bottom left to see what the width and height of your image is.

    If you used my background tile, your image should have a width and height of 34 pixels! Make a mental note of that number for now.
  2. Delete the image from your stage by pressing the Delete key. Don't worry, all our work has not been wasted. Your movie clip and its Linkage Identifier data should be saved in your Library.
  1. Alright, time to make this tutorial worthy of being in the ActionScript section. Select the first frame in your timeline, press F9 or Window | Development Panels | Actions to display the Actions window. Copy and paste the following code inside it:

    tileBG = function () {
    tile_width = 34;
    tile_height = 34;
    //
    x_max = Math.round(Stage.width/tile_width);
    y_max = Math.round(Stage.height/tile_height);
    trace(x_max);
    trace(y_max);
    for (x=0; x<=x_max; x++) {
    for (y=0; y<=y_max; y++) {
    bg = _root.attachMovie("square", "bg"+x+y, this.getNextHighestDepth());
    bg._x = tile_width*x;
    bg._y = tile_height*y;
    }
    }
    };
    tileBG();

[ copy and paste the above code into your Actions window ]

  1. Press Ctrl + Enter or Preview in HTML. Notice that your image is now tiled throughout your stage area.

ActionScript Explained
Let's take a look at the code and what causes our image to tile. There are some interesting concepts that you might find useful for other uses beyond just tiling!

tileBG = function () {

I create a new function, and I call it tileBG. Notice that the function takes in no arguments.


tile_width = 34;
tile_height = 34;

In the tile_width and tile_height variables, make sure to enter the width and height of your background tile. In our case, the tile graphic was conveniently 34 pixels wide by 34 pixels tall.


x_max = Math.round(Stage.width/tile_width);
y_max = Math.round(Stage.height/tile_height);

In the above two lines, I determine the total number of tiles that will be needed to cover the stage horizontally (x_max) and vertically (y_max). I do that by dividing the total width and height of our stage by the width and height of your background tile. I use Math.round() to round our values to the nearest whole number.


for (x=0; x<=x_max; x++) {
for (y=0; y<=y_max; y++) {
bg = _root.attachMovie("square", "bg"+x+y, this.getNextHighestDepth());
bg._x = tile_width*x;
bg._y = tile_height*y;
}
}

The two for loops conduct the horizontal and vertical sweep for creating our grid that will contain our background tiles. Let's arbitrarily set x_max to be = 3, and y_max to be 4. First, x is set to zero. For x = 0, the for loop for the y runs all the way until y <= y_max. Then x is set to 1, and the y loop runs again. The loops run until x <= x_max or, occasionally.

A good way to picture this would be to imagine the x value representative of the top row of a puzzle. For each x piece laid, a series of y pieces go straight down and fill up all the vertical spaces below the x piece. That process goes until you reach the end of your puzzle and can't lay any more x pieces.

That is similar to our code above. You will lay pieces symbolic of our background tile until you reach the edge of our stage.


bg = _root.attachMovie("square", "bg"+x+y, this.getNextHighestDepth());

In this line, I invoke our background tile by using the attachMovie function combined with our movie's linkage identifier, square. The attachMovie function called to a target (_root for us) takes in the following three arguments: the movie clip's identifier, the new movie clip's name, and the movie clip's depth.

For the depth, I use the getNextHighestDepth() function. It automatically places newly attached or created movies at one depth higher than the elements around it. You could use an arbitrary value incremented by x or y, but if you are working on a large animation with lots of dynamically created "stuff" occupying precious depths in your timelines, letting Flash keep track of the depths sounds like a good thing to do.


One last thing to note is that I am storing our newly attached movie clip in the variable bg.

bg._x = tile_width*x;
bg._y = tile_height*y;

Since bg represents our newly attached movie clip, I can use the _x and _y properties to specify the horizontal and vertical position of our bg movie clip. tile_width*x ensures that each subsequent image is shifted over by x. The same is true in the code used for shifting our movie clip in the vertical position.


Wrapping Up
If you want to learn more about grids, Pom's tutorial on grids and senocular's tutorial on isometric grids should help you out immensely.

You may be wondering, why go through the hassle of tiling an image when you could just take a screenshot of the tiled background, save it as an image, and then just import that one large image as opposed to tiling one small image dynamically in Flash!

You can create photo thumbnail galleries where each square in the grid is actually a small preview of a larger picture. For a project I was working on at MIT, I used the grid as a representation of a larger map to use as a minimap. Almost all of senocular's Isometry tutorials involve manipulations of the basic grid.

What I hope to convey through this tutorial, is that grids allow you to divide your stage area into small parcels of pixels that you can manipulate and use to your will. I have provided the source zip file that contains a working FLA of my example.

download source files

Full Screen Flash Page

A simple tutorial to teach you how to create full screen Flash page within a HTML page. This tutorial may be a bit different from the usual full screen effects you've seen. It doesn't scale the height and width of the Flash file, but merely expands the Flash page without scaling the elements inside. Thus if your using pixel fonts the text will still remain clear and crisp.


In this tutorial we'll be using Flash MX 2004 as well as a bit of CSS to get rid of the default margin value. Click here to see the final outcome.

Let's begin
  1. Open any file in Flash.

  2. Go to File > Publish Settings. Select the HTML tab.

  3. Click the Dimensions drop down box. Select Percent and type 100 for the Width and Height.

  4. Click the Scale drop down box and select No scale.

  5. Click Publish to publish the files.

[ Screenshot of the Publish Setting in Flash MX 2004. ]

  1. You should have two new files now, a SWF and a HTML file.

    The code for the HTML file should look similar to this:

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>myfiletitle>
head>
<body bgcolor="#ffffff">


<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/
shockwave/cabs/flash/swflash.cab#version=7,0,0,0"
width="100%" height="100%" id="myfile" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="myfile.swf" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param name="bgcolor" value="#ffffff" />
<embed src="myfile.swf" quality="high" scale="noscale" bgcolor="#ffffff" width="100%" height="100%" name="myfile" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go
/getflashplayer"
/>
object>
body>
html>

[ Code for the HTML file. ]

  1. This part is optional. Most browsers set a default value for the margin which often isn't zero, therefore we're going to use CSS to set it to zero.

    The code is quite self explanatory. It means set the height and width to 100% and set the margin value to 0. Add this piece of CSS code into the HTML file (inside the head tag).

<style type="text/css">

style>

[ Copy and paste the above code between the head tags. ]


Note
Note that the CSS may not work in some of the older browsers since they do not support it completely. Refer to this link for more detail.

That's it! I hope you enjoyed the tutorial. If you had any problems with creating the files above or didn't quite understand something. Here is a zip file containing the FLA, SWF and HTML file.

Download ZIP

Making an Object Transparent

Making an Object Transparent

In the tutorial listed below I will instruct you on how to make just about anything transparent. For this tutorial we will start off with a blank document:

Now, let's create our simple motion tween:

  1. First, draw something, import an image, etc. onto your drawing area.
  2. Once you finished the first step, we will now take what you drew/imported and convert it into a movie clip. To make it a movie clip, right click on your object and click on Convert to symbol. When the new window comes up, click the radio button horizontal to Movie clip. Press OK:

[ convert to symbol dialog box ]

  1. Click on the image once to select it.
  2. Now, on the properties bar, click the dropdown menu next to Color and select Alpha:

[ select the Alpha option ]

  1. Now all you have to do is enter a value or slide the slider to specify the alpha value you would like.

Creating Drop Down Menus

Creating Drop-Down Menus

So want to create a drop-down menu eh? It's not hard at all! I'm not going to go into details in this tutorial. I'll just show you the main idea of the menu. This tutorial requires only the basic knowledge of Flash. The following animation is an example of what you will be creating:

for example the right of the list of examples of animation -->

[ create a simple animation ]


First you should download the unfinished .fla file from the link in the following table:


So what do we have here? In the first frame there is the button that should open the menu and in frame 20 I've already copied out the buttons so you do not need to do that but if you will be creating your own drop-down menu some day be sure that there are no gaps between the menu buttons because whenever the mouse moves out of the button-area the menu will close.

There is only one button copied all over the place and the text on the buttons is in the "TxT" layer which is locked to make things easier. You can preview the animation if you'd like... nothing happens! The only action there right now is in the first frame of the "Actions" layer. As you can see it's a stop(); actions which just won't let the animation to start playing as you preview it.

Making the Menu Work:
The following steps will help you to create the drop-down menu:

  1. First double-click the symbol on the stage, go to frame 1 and click on the only button there and open up the actions tab! Now assign this piece of code to the button:

    on(rollOver) {
    gotoAndStop("open");
    }


    This just tells the timeline to go to the frame labeled "open" when the cursor rolls over the button! You can see that frame 20 in the "Buttons" layer is labeled "open" and frame 1 is labeled "closed" (you can see that by selecting a frame and opening up the properties panel for that frame. Now close the actions tab!
  2. Next go to frame 20, click on the "Invisible button!!!" layer and insert a new keyframe in frame 20 by selecting frame 20 and pressing F6 or right-clicking frame 20 and selecting Insert keyframe.
  3. Now select the Rectangle Tool and draw a big rectangle! Bigger than the stage itself!
  4. If it's not selected yet, select the rectangle you just created by double-clicking on it with the arrow tool and go to Insert->Convert to Symbol or press F8!
    In the "name" field type "Invisible button" or something similar and make sure the behavior is "Button". Click OK!

[ Converting it to a symbol ]

  1. Double click the new symbol, click and drag the first frame on the timeline to the fourth frame of the timeline! From the "UP" frame to the "Hit" frame!
  2. Now click on the new symbol and add this piece of code on it:

    on(rollOver) {
    gotoAndStop("closed");
    }


    This just tells the timeline to go to frame 1 (labeled "closed") when the cursor rolls over the symbol!
That's it! Wasn't that easy? You can test the move by pressing CTRL+ENTER or by choosing control->Test Movie.