Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m

# Enhance Your Flash Layouts with Draggable Windows

Difficulty:IntermediateLength:LongLanguages:

In this tutorial you'll learn how to create draggable windows using Flash and AS3. I'll cover the basics of drag&drop, window bounds, adding content, and how to minimize the window.

## Final Result Preview

Let's take a look at the final result we will be working towards:

## Step 1: Create a New File

Okey, let's gooo! Create a new document by hitting CTRL+N and selecting Flash File (ActionScript 3). Set the stage size to 600x400 px and the background color to #EEE. In the Properties window, set the class to windows and save your file as windows.fla.

## Step 2: Design the windowArea

Select the Rectangle Tool (R) and draw a rectangle with 280x90 px. Select your shape, go to the Color palette (Window > Color) and create a gradient from #FFF to #CCC.

Hit F for the Gradient Tool, select your gradient, rotate it 90 degrees (by holding the Shift button while you rotate) and shorten it so it adapts the rectangle.

Select all the shape, hit F8 to Convert to Symbol, name it windowArea and set the registration point to the top left.

Select the symbol and in the Properties window set the instance name to windowArea.

For the border, use the Glow filter, with a 2px blur, strength 500%, color #666666. I used a glow because if you use a stroke, when you resize the Window the stroke will also resize.

## Step 3: Design the windowBar Button

Select the Rectangle Tool (R) again and draw a rectangle with 280x22 px and a 1px stroke with the color #666. Create another rectangle on top of this, but this time in the Rectangle Options set the Rectangle corner radius to 5.

Now, eliminate the necessary corners like on the image below.

Then, paint your shape, select it, go to the Color palette (Window > Color) and create a gradient from #999 to #333. Rotate the gradient 90 degrees, like we did on the previous step.

Select the all shape, hit F8. Name: windowBar; Type: Button; Registration: top left.

Select the symbol and in the Properties window set the instance name to windowBar.

Double click the shape, create keyframes for the Over and Down states. Now change the colors for each one.

I've chosen:

• Over: gradient from #FF9900 to #CC6633
• Down: gradient from #9933CC to #660099

## Step 4: Create the Minimize Button

Create a round rectangle with 14x11 px, 5px radius, and change it so it looks something like below.

The minus sign you do by creating a rectangle 5x2 px with the color #999. Set the instance name to minimizeBtn.

I used the same colors from the windowBar, but the UP State with a 40% alpha for the gradient.

## Step 5: Create the Window

Arrange all shapes in form of a window, hit F8 and create a MovieClip with the name window and registration: top left.

In the Library, right click the Window and go to Properties. Set the class to window. This way the window will be assigned to the class we'll be creating later.

Select the symbol and in the Properties window set the instance name to window. In the Filters, click Add filter (the circled button in the image below), and add a Drop Shadow with the color #333 like below:

Double-click the symbol to edit it. Select all (CTRL+A), Right Click > Distribute to layers.

In a new layer, write the text for the window's title with Arial, 11pt, color #CCC, letter-spacing: 1. Set it to Dynamic Text and name it windowTitle.

## Step 6: Create the ActionScript File

Now that the design is complete, let's start coding our scene. The first thing we'll do is create our ActionScript file.
Hit Ctrl+N or go to File > New and select ActionScript File. Save it in the same directory and with the same name (windows.as) of your FLA file.

Now let's create our package and import the necessary classes:

## Step 7: Add Event Listeners

Now you need to assign functions to our buttons. You'll need a function to when we start to drag the windowBar, another to stop the drag, one to bring the window to top when you click it, and another to minimize it.

Add these Event Listeners to the public function Window() in our code:

The public var title is used for the window's title. We'll be using this later.

Since the windowTitle is a dynamic text we disable its mouse functions so that they won't affect the dragging area of the windowBar.

## Step 8: onWindowStartDrag Function

This is where we start having fun! Copy the following code after the public function Window():

The first thing we do here is create variables to the widths and heights of the Window and the windowBar.

Next, the window's boundaries. We'll create two boundaries: one when the window is normal (if windowArea is visible) and another when it is minimized. You do this by creating a rectangle with the size of the stage. The rectangle supports four attributes (x, y, width, height). It'll start in the corner of the screen (x: 0; y: 0) and extend to the other corner of the screen. Because the bounds are related to the registration point of the window, we'll need to subtract the window width and height (or the windowBar's in case the window is minimized).

After the bounds have been set, we'll set the window to drag. We'll drag this, the Window class. In other words, the whole window.

The startDrag function supports two attributes: lockCenter and bounds. If you don't want any bounds just write this:

If we have several windows we'll need to bring the current window to the top of the display. We do this with the addChild by adding it again to stage:

## Step 9: onWindowStopDrag Function

This one is really simple. We'll use the stopDrag function here. Just copy the following to your code after the previous onWindowStartDrag function:

## Step 10: Bring Window to Front

Again, really simple. When we click the window we'll bring it to front using addChild.

## Step 11: minimizeWindow Function

To minimize/show the window, we'll toggle the visibility of the windowArea like this:

You can improve this by fading out the window and hiding it, and vice-versa:

The Tween supports the following values:

For more extensive reading, use the LiveDocs.

In our case, what we're doing is, if the windowArea is visible (meaning: not minimized), it will fade out the windowArea and when the tween finishes (TweenEvent.MOTION_FINISH), it will hide the windowArea. Vice versa if it's minimized.

## Step 12: Set the Title

We'll use the variable t to change the windowTitle. The other two lines are just to resolve a letter spacing issue. If you don't write them Flash will reset the letter spacing to zero.

This function will be used later like this:

## Step 13: Set the Size

This function will receive two attributes: the Width and Height of the window. If neither is filled, it will be set to the default size (280x112 px)

What we do here is change the width of the windowBar, the windowArea and the windowTitle. For the height we just change the windowArea's, leaving the height of the Window to its default size, just like a normal window.

When resizing we'll have to reset the position of the Minimize Button. Which is equal to the window's width minus the button's width and 6px.

This function will be used later like this:

## Step 14: Give it a Nice Shadow

Remember when we placed a Drop Shadow under the Window? Well, if you place the window by code you'll need to add the drop shadow by code as well.

All of the above attributes can be set by code. Copy the function and change the values according to your own taste:

This function will receive a String with the color's hexadecimal code. If this is not filled, the default color value will be #333333.

The quality can go from 1 to 15, being 1 2 3 - low, medium and high. We used low, here.

The color must be converted from a String to an int.

After defining the attributes we must add the filter to the window like in the last line. This will create an Array of filters. Which means you could add other filters to the Window as well.

This function will be used later like this:

## Step 15: Adding a Window by Code

Now change to your Flash file, create a new layer, name it as3 and hit F9 for the ActionScript (or go to Window > Actions).

Adding a Window is very simple. All you need to do is create a variable (let's call it myWindow), assigning it to the Window class and the add the Window to stage:

This will produce a Window with its default values.

## Step 16: Changing the Values

You can change several attributes of the Window:

• Title
• Size
• X and Y position
• Content

Again, the first thing to do is to create the variable assigned by the Window class:

Then you can start changing the attributes:

#### Adding the window to stage:

The all code will be like this:

## Step 17: Using Additional Windows

Here's another example with two windows added by code and a default one in the stage:

If you need to know the window's title, you can use this:

## Milestone

Well done for following so far! You should now have achieved a result similar to what we looked at in the beginning:

## Step 18: Different Content in the Windows

If you notice by now, you can change all the window's attributes but the content remains the same. So let's create the content.

Open the Window's MovieClip, create a new layer for the content and go to Insert > New Symbol (CTRL+F8). Choose MovieClip, name it content and hit OK. Now place it at X:0, Y:22.

Select the new content symbol you just created, and set its instance name to CONTENT.

Double-click the content, name the existing layer content and create another one named as3. In the latter, hit F9 and write:

This will be our default content. Meaning, nothing!

Now create another keyframe and place an image for example. Bear in mind the size of the window you'll be using. Add another keyframe and write some text in it. Now we have 3 keyframes: 1. no content, 2. image, 3. text.

Really simple. We'll just say which keyframe we'll want to go to.

You'll also need to change the minimizeWindow function to this:

## Step 19: Changing the Content in the Code

In the previous example, add this to the code:

Here is the complete code:

## Conclusion

So, there's a simple way to create a draggable window (much like we see on Windows). If you want to go further, you can change the content MovieClip, adding text, buttons, images, etc. You could add scrollbars, call content through XML, or whatever you can think of. The possibilities are endless!

Here's another awesome example:

This is eXOTRik, and I hope you found this useful. It's my first tutorial, hope to bring you some more ninja tricks. Ayaaaa!