How to select an item on the screen

by Paulo Caldeira.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Buttons allow the user to click and make an action occur. A different type of user interface element, however, allows the user to select an item on the screen.

The difference is that a user clicks to make a selection, and that movie clip changes its appearance. But nothing else happens. This way, the user can make or change her selections. After that, the user can click another button or perform another action.

We'll use selections as the first step toward learning how to drag and drop movie clips, the goal of this tutorial.

Button Inside the Movie Clip Method

A movie clip cannot simply react to a mouse click. Unlike a button, it cannot use an on (release) or on (press) handler.

So you have to be tricky. You put a button inside a movie clip. The button can handle the mouse clicks as long as it is big enough to cover the entire movie clip.

To turn this into a selectable movie clip, we'll have to make this into a multiframe movie clip. The first frame contains the button named Off Button. This button has the following script:

on (release) {
   this.gotoAndStop(2);
   }

By referring to this, the button is referencing the movie clip that it is in. Frame 2 of the movie clip contains a similar button named On Button. The difference is that the On Button is a little brighter, indicating that the movie clip has been selected. The script on this movie clip is similar:

on (release) {
   this.gotoAndStop(1);
   }

As you might guess, by clicking the button on frame 2, the movie clip goes to frame 1, where the original Off Button is located. By clicking the buttons in the movie clip over and over again, the movie clip goes back and forth between frames 1 and 2.

The only thing left is to place a stop(); command on the first frame of the movie clip.

hitTest Method

You can detect a mouse click in a movie clip without a button. However, this method is a little trickier. After you learn it, though, it is a much cleaner solution.

To detect a mouse click on a movie clip without a button, use the onClipEvent(mouseDown) or onClipEvent(mouseUp) movie clip handlers. For instance, you can place the following script on a movie clip:

onClipEvent (mouseUp) {
   this.gotoAndStop(2);
   }

Two frames are in the movie clip, each with a different colored circle. A stop(); command is on the first frame of the movie clip.

When you try this movie, you will see right away why the onClipEvent(mouseUp) handler is different from the on(release) handler used on buttons. If you click on one movie clip, they both react.

This is because all movie clips get the mouseUp event sent to them. It is not exclusive to just the movie clip under the cursor.

Determining Which Movie Clip Was Clicked

There is a way to determine which movie clip has been clicked. The hitTest function tests a mouse location with a movie clip to see whether the location is inside the movie clip. So, by modifying the script, we can only send the correct movie clip to its second frame.

onClipEvent (mouseUp) {
   if (this.hitTest(_root._xmouse, _root._ymouse)) {
   this.gotoAndStop(2);
   }
   }

The hitTest function can work a variety of different ways. In this case, it is fed the x and y values of the mouse location. It is prefaced with this so that it refers to the current movie clip. When the user clicks anywhere, the onClipEvent (mouseUp) handlers in all the movie clips get triggered. Then, both of the movie clips perform the hitTest test; only one that is under the mouse will test positive and jump to frame 2.

A Selection Script

To change this into a selection script, we have to allow the user to click the movie clip multiple times and change the state of the movie clip from off to on and back to off again.

The script has to determine which state the movie clip is currently in and then send the clip to the other frame. The script can determine the current state by looking at the current frame of the movie clip. This can be done with the aptly named _currentFrame property. This property reads 1 when the movie clip is on the first frame and 2 when it is on the second.

Here is the new script. This is a complex script because it first tests the location of the mouse and then tests the current frame of the movie clip.

onClipEvent (mouseUp) {
   if (this.hitTest(_root._xmouse, _root._ymouse)) {
   if (this._currentFrame == 1) {
   this.gotoAndStop(2);
   }  else {
   this.gotoAndStop(1);
   }
   }
   }

Now you have seen two completely different ways of making selectable movie clips. I like the second way better because you don't end up with the extra library symbols of the buttons. The advantage of using buttons, however, is that they can easily contain up, down, and over states, which are sometimes nice for user feedback as users make their choices

Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Transforming the Current Color of a Flash Movie Clip - You want to modify a movie clip's color relative to the current color transformation, instead of relative to the author-time color values. Use the getTransform( ) and setTransform( ) method (more...)
ActionScript: Repeating an Operation Many Times - You want to perform a task multiple times within a single frame. Use a loop to perform the same task multiple times within a single frame. For example, you can use a for statement: (more...)
Avoiding Conflicting Variables in ActionScript - You want to make sure that variables within a function do not interfere with variables in other functions or within the timeline in which the function is defined. Use the var keyword to declare loc (more...)
How to Exit a Function in ActionScript - You want to exit a function. Functions terminate automatically after the last statement within the function executes. Use a return statement to exit a function before reaching its end. The (more...)
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.