Toggle Buttons and Event Listeners

Toggle Buttons and Event Listeners
As we mentioned earlier, when creating FlashR apps for mobile devices, it's very important to remove event listeners as soon as they are no longer needed. In this tutorial, we will code two functions that will run when the Menu Overlay is moved on or off the stage. We will call these functions loadListeners and removeListeners.

So what will these functions do when the Menu Overlay is moved? First, let's discuss what will happen with we click the toggle button to remove the menu from the stage, which is the removeListeners() function.

For each button or hyperlink on the menu, we will want to turn everything off. For example, we will set the buttonMode and useHandCursor properties to false. Also, we don't want Flash to be listening for the button click events associated with these menu buttons when the menu is not on the stage. So we will remove the event listeners from these buttons. The example below is for the website button.

websiteBtn.buttonMode = false;
websiteBtn.useHandCursor = false;
websiteBtn.removeEventListener(MouseEvent.CLICK, goWebsite);

When the menu is toggled back onto the stage we run the loadListeners function which set the buttonMode and useHandCursor properties back to true and add event listeners to each button on the menu. For the websiteBtn we will do this.

websiteBtn.buttonMode = true;
websiteBtn.useHandCursor = true;
websiteBtn.addEventListener(MouseEvent.CLICK, goWebsite, false, 0, true);

Here is the complete code.

function removeListeners():void {

websiteBtn.buttonMode = false;
websiteBtn.useHandCursor = false;
moreAppsBtn.buttonMode = false;
moreAppsBtn.useHandCursor = false;
creditsBtn.buttonMode = false;
creditsBtn.useHandCursor = false;
indexBtn.buttonMode = false;
indexBtn.useHandCursor = false;
musicBtn.buttonMode = false;
musicBtn.useHandCursor = false;

websiteBtn.removeEventListener(MouseEvent.CLICK, goWebsite);
moreAppsBtn.removeEventListener(MouseEvent.CLICK, goMoreApps);
creditsBtn.removeEventListener(MouseEvent.CLICK, goCredits);
indexBtn.removeEventListener(MouseEvent.CLICK, goIndex);
musicBtn.removeEventListener(MouseEvent.CLICK, stopMusic);


function loadListeners():void {

websiteBtn.buttonMode = true;
websiteBtn.useHandCursor = true;
moreAppsBtn.buttonMode = true;
moreAppsBtn.useHandCursor = true;
creditsBtn.buttonMode = true;
creditsBtn.useHandCursor = true;
indexBtn.buttonMode = true;
indexBtn.useHandCursor = true;
musicBtn.buttonMode = true;
musicBtn.useHandCursor = true;

websiteBtn.addEventListener(MouseEvent.CLICK, goWebsite, false, 0, true);
moreAppsBtn.addEventListener(MouseEvent.CLICK, goMoreApps, false, 0, true);
creditsBtn.addEventListener(MouseEvent.CLICK, goCredits, false, 0, true);
indexBtn.addEventListener(MouseEvent.CLICK, goIndex, false, 0, true);
musicBtn.addEventListener(MouseEvent.CLICK, stopMusic, false, 0, true);


← Back

IMPORTANT: These app development tutorials are written with the Flash novice in mind. You will need to optimize your app beyond what is covered in these tutorials before finalizing your app for the app store.

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

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

Content copyright © 2022 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.