Button Disabling

On the previous page, we finished out with a simple website example. Clicking on each button would take you to the corresponding "page," or frame on the main timeline. However, you may want to give your buttons "disabling" behavior. Yeah, I know I wrote a tutorial on button disabling that uses classes. This, however, is going to be the quick version, for those cases where you want to program the behavior more directly.

Button disabling behavior means that if you are at the home page, for example, you want to disallow the home button from being clicked again. Whatever page you are at, you want the corresponding button to be disabled so that it cannot be clicked again. This scenario creates what might also be termed "radio buttons." When they are clicked, they are selected, and cannot be selected again until something else has been selected instead.

To do this is very easy. We already have code that uses a for loop to cycle through the array that holds the buttons. Each one is compared to the event.currentTarget to see which button was clicked. All we have to do is add code that disables that button, and perhaps includes some visual clue that the button can't be clicked on again.

To disable the button, we set its mouseEnabled property to false. This will cause the cursor to become an arrow instead of the hand cursor. For a further visual clue that the button is disabled, we will set its alpha to 0.5. At the same time, we need to enable all of the other buttons, and set their alpha back to 1, just in case one of them is the button that was formerly disabled. We can do this using an else block added to our if statement:

stop();
var clipArray:Array = [home_mc, about_mc, products_mc, services_mc, contact_mc];
var destArray:Array = ["home", "about", "products", "services", "contact"];
for (var i:int = 0; i < clipArray.length; i++) {
	clipArray[i].buttonMode = true;
	clipArray[i].addEventListener(MouseEvent.CLICK, clickHandler);
}
 
function clickHandler(event:MouseEvent):void {
	for (var i:int = 0; i < clipArray.length; i++) {
		if (event.currentTarget == clipArray[i]) {
			this.gotoAndStop(destArray[i]);
			clipArray[i].mouseEnabled = false;
			clipArray[i].alpha = 0.5;
		} else {
			clipArray[i].mouseEnabled = true;
			clipArray[i].alpha = 1;
		}
	}
}

So you can see that when the buttons in the clipArray are cycled through with the for loop, something happens to every one of them. If the button happens to be the one that was clicked, it is disabled. If not, the else block handles it, and the button is enabled. There is one last little thing. When the application first begins, the home page is on the screen, and so the home_mc button should really start out in a disabled state. So add these lines to the end of the code:

home_mc.mouseEnabled = false;
home_mc.alpha = 0.5;

With those changes in place, here is how the application behaves now:

website_simple_button_disabling.fla

That concludes this tutorial. I hope you can now see some of the possibilities for using arrays in your programming.

Great tutorial and very

Great tutorial and very informative. One question - can you call a button to be clicked in your code to start on the home page? For instance in AS2 you would just type: home_mc.onRelease();

Jody Hall's picture

Do you mean simulate a click

Do you mean simulate a click on the button? I think that's what you mean, because in AS2 that's how you would call the anonymous function that had been assigned to home_mc:

home_mc.onRelease = function() {
    //go to the home page
} 
//call the function manually:
home_mc.onRelease();

In AS3, you can call an event listener manually by passing a null in place of the event object:

home_mc.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
    //go to the home page
}
//call the function manually:
clickHandler(null);

This could backfire though, in those situations where you are actually making use of the event object in the body of the function.

Disable button with rollover

Love the tutorial - easy to understand and very useful.

I'm making an interactive map and wanted to add a rollover in this dynamic way.  I need the rollover alpha = 0.5, rollout alpha = 1 and if the user clicks it also set the alpha to 0.5 and disables the mouse.  The result being that a region is highlighted when clicked and stays highlighted but when you rollover the other regions they also highlight.  Does that make sense??

Thanks

I'm a total newbie, and your tutorials helped me with a massive project. I don't know how i would have managed without the MovieClip navigation using classes (probably would have lost a few nights of sleep trying to debug a timeline navigation).


I'll be sure to ask you if i come up with any problems. I hope you post more tutorials; you really teach well! I see i'm not the only one who thinks so.

Glad I did not need to learn to build a timeline navigation. MCs rock!

Flash for html menu

I have used a flash as2 menu to open html pages and the buttons are disabled according to the html page. The as2 uses _root.link = _root.button;
In the html you would have something like.... param name="movie" value="nav.swf?button=2".... and button 2 would be selected/disabled on that html page.
I have not found any as3 code that is capable of completing the same task. Any advice greatly appreciated.

Jody Hall's picture

Very interesting

Zippo,

I find that very interesting, although I have never been particularly good at the way Flash interfaces with HTML. But I do believe that it ought to be possible in AS3.

Jody