First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Thomas Watson | Website: http://hikercentral.net

Accessing Multiple Buttons in UI Components #2 Message Box

Next, just for fun, drag a "Push Button" onto the stage from the "Components" Panel. Open it's "Properties" Panel and give it an instance name, and make up a "Click Handler" name. I named my instance "myButton", labeled it "Try Again", and called the "Click Handler" "tryButton."

Now unlock the message box, and un-hide it. The images on your stage should look something like this.

Our dynamic text field is underneath the message box, but will become visible when the box closes. The box will close as soon as a button is clicked as part of it's "Close Handler" function.

By default, the message box starts off with a _visible property of true meaning the box is visible on the screen. This makes it easy to get rid of the box and bring it back just by changing it's _visible property-- no shuffling it off and on the screen, no switching back and forth between frames. In most cases in a "real" application you will probably start with the box hidden-- that is with it's property set to false.

Everything else we need to take care of to make this work is in the "Close Handler" function, so let's go take a look at that. First we want to "close" the box by changing it's _visible property to false.

Now the tricky part. I won't go into everything I went through or everything I did or didn't discover along the way, but will pretty much stick to the basics here. From what notes there are on this component and from digging through the debugger, I did discover that the buttons are all stored in an array, and each entry in this array gets a corresponding movie clip of the button component. Flash names the movie clips "buttons_mc" plus the array index of the button. Flash actually stacks these components together to make a single unit. Reading through the debugger also told me that these buttons had a "state" that could be read. The "state" indicates if the button as been clicked or not, and is expressed as either true or false.

So the first if block has the condition of checking the first button-- button "0" (remember this is an array that starts numbering with "0")-- to see if it's state is true -- meaning "has it been clicked or not?" If it has been clicked, we set the text of our dynamic text box to "Yes", corresponding to the first button in our set. The second statement in that if block sets the state of the button back to it's unclicked state-- false. You must be sure and reset the button state-- it does not automatically reset.

The other two if blocks simply repeat these actions, testing the other two buttons.

I did say I wouldn't go into everything I discovered about this but one piece of information that could be useful in some cases, is that Flash will return two parameters to the "Close Handler" function like so: myCloseHandler(messageBox, buttonIndex). However, for this particular situation, I didn't find that helpful. I only have one message box, so I don't need the instance name, which you might need if you have more than one box and wish to write a generic handler for them. Also, unless you only call the box once, you still have to reset the state of the buttons, which you have to do using the method show here. I could not find a method that would reset the button using only the button index. Not resetting the buttons after each use could lead to unpredictable results, since the state of more than one button would be true. However, there are some situations where this could be useful information.

One last piece of business is to add a function that is out "Click Handler" for the "Try Again" button. Pretty simple stuff here-- just make the window visible again and clear the text in the dynamic box.

Use Control->Test Movie to run this. Click a button and the message box closes, showing the name of the button you clicked on the screen, demonstrating that we actually captured the button click correctly. Enjoy!

» Level Intermediate

Added: 2002-05-17
Rating: 7 Votes: 12
(10 being the highest)
» Author
Tom Watson is a Flash developer currently residing in Nashville, Tennessee. He's been an independant contractor in the computer business since 1989, working on web projects since the web was introduced to the Internet in 1994.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.