Printer Friendly Version

BellaOnline's Flash and Animation Editor

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

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


Flash and Animation Site @ BellaOnline
View This Article in Regular Layout

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

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

BellaOnline Editor