g Text Version
Beauty & Self
Books & Music
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
News & Politics
Religion & Spirituality
Travel & Culture
TV & Movies

Bored? Games!
Take a Quiz
Rate My Photo

Natural Living
Folklore and Mythology
Distance Learning

All times in EST

Full Schedule
g Flash and Animation Site

BellaOnline's Flash and Animation Editor


Toggle Button Event Listener

In the last tutorial, we discussed the ActionScript code needed for a toggle button. We discovered that an if statement is used to determine what action Flash should take each time the button is clicked based on the current Boolean value of a variable.

But how exactly do we attach the if statement to the user input (the button click). One way is to put the if statement inside a function and then use an Event Listener to run that function each time the button is clicked.

As we have already set up for our FlashR storybook app, the toggle button is named setTogBtn. Let's call this new function PositionSetMenu because it will position the settings menu overlay on or off the stage as needed.

setTogBtn.addEventListener(MouseEvent.CLICK, PositionSetMenu, false, 0, true);

In the code above, we are attaching a mouse click event listener to the setTogBtn button which will run the PosititonSetMenu function. There are three other parameters after the name of the function. The first two are the defaults for the useCapture and priority parameters. The third parameter sets the useWeakReference Boolean value to the non-default value of true.

By setting the useWeakReference parameter to true, we tell Flash to remove the event listener via the garbage-collection process. You may not have seen this used when programming for the web. However, in programming for mobile devices, we need to remove all event listeners as soon as they are no longer needed in order to conserve CPU and memory resources.

There are two other lines of code that we need to discuss before moving on. In mobile devices, you will not see the familiar hand cursor over a button, as you do in web browsers. The following two lines will tell Flash that we want to see that hand cursor over the buttons in our iPad app.

setTogBtn.buttonMode = true;
setTogBtn.useHandCursor = true;

At this point, we have our toggle button programmed to run a function (PositionSetMenu). Inside that funtion is an if statement that will determine which of two functions (moveSetMenuDown or moveSetMenuUp) will be run next, depending on the Boolean value of the showMenu variable. Now we need to code those two functions.

In our example, these two functions will position the settings menu overlay. However, you can program your toggle button to control other processes, such as playing/stopping background music or showing/hiding an object on the stage simply by what code you use in these two functions.

Join us in the Flash forum. | Join us in the Digital Art and Design forum.


Add Toggle+Button+Event+Listener to Twitter Add Toggle+Button+Event+Listener to Facebook Add Toggle+Button+Event+Listener to MySpace Add Toggle+Button+Event+Listener to Digg Toggle+Button+Event+Listener Add Toggle+Button+Event+Listener to Yahoo My Web Add Toggle+Button+Event+Listener to Google Bookmarks Add Toggle+Button+Event+Listener to Stumbleupon Add Toggle+Button+Event+Listener to Reddit


RSS | Related Articles | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map

For FREE email updates, subscribe to the Flash and Animation Newsletter

Past Issues

Printer Friendly
tell friend
Tell a Friend
Email Editor

Content copyright © 2018 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.


g features
Subtle Animation Website Trends

Wix Editor - Background Video

Video on Wix Websites

Archives | Site Map


Past Issues

Note: BellaOnline uses cookies to help provide a consistent user experience. Our advertisers may use cookies to help customize ads. Please contact us with any question about our cookie use.

Summertime Foods
Corn on the Cob
Burgers on the Grill
Apple Pie


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2018 Minerva WebWorks LLC. All rights reserved.

BellaOnline Editor