
Double Click
Skill Level:
Intermediate
Knowledge Needed:
MovieClips, Instance Names, ActionScript
Number of Steps:
6
The Movie
-----------------------------------------------
------------------------------------------------
Download Flash File
Learn how to use getTimer() to make an event happen when a user double clicks.

First open up a new Flash document. For this example I set my stage to 200x150. We are going to be creating two objects. One button and one movie clip. First let's create the button. I made a black rectangle with no line color at 200x15. Now select the box and press
F8 to make it a symbol. Choose
button as the behavior type and name it button. Lastly give it an instance name of button_btn.

The next thing to do is to make our box movie clip that will be toggle being hidden and visible when the user double clicks on our button. For this example I made a dark gray box with no line color at 200x135. Now select the box and press
F8 to make it a symbol. Choose
movie clip as the behavior type and name it box. Finally give the box an instance name of box_mc.

Now all we have to do is add the code to toggle our box movie clip. Put the folling code in frame 1 of your root timeline.
//set initial variables
click = false;
First we are setting our declaring our variable
click and setting it equal to
false.

Next we are going to write the onPress function for our movie clip.
//function for the button press
button_btn.onPress = function() {
//if this is the first click
if (!click) {
timer = getTimer()/1000;
_root.click = true;
}
What we are doing here is saying if our variable click is equal to false then we will user getTimer() to find out how much time in miliseconds has passed since the movie started and divide it by 1000 to give us the number in seconds. Then we set our click variable to true so we can test if there is a double click.

Lastly we need to find out if there is a double click and perform some sort of action to show the user that they double clicked something. Add the following code right after the if statement.
else {
timer2 = getTimer()/1000;
//if it is a double click
if ((timer2-timer)<.25) {
//toggle the box mc on and off
if (_root.box_mc._visible==false) {
_root.box_mc._visible = true;
} else {
_root.box_mc._visible = false;
}
} else {
timer = getTimer()/1000;
_root.click = true;
}
}
};
What we are doing here is finding out how much time has passed since the movie opened again and then subtracting how much time had passed when the user first clicked. If that time is less than .25 seconds then we either hide the box or make it visible depending on it's current state. Then we set the click variable back to false so that we can start the function over the next time a user clicks. However, if the time is greater than .25 seconds we reset the variables as if it is the first click.
Where to go from here. This double click function is very easy to implement in your movies and can serve many purposes depending on what you are trying to accomplish. I am currently using this function in a program where a user logs in to a site the client sees a list of folders that they have access too. Then the user double clicks the folder to open it like he would in an explorer window. Double clicking doesn't have very many uses online because so many things are just set to single clicks, but this can be useful for toggling our box's visibility like before. If you have any questions be sure to email me or post in the discussion for this tutorial.
Copyright © 2000-2008 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.
kdfj