How to enable and disable a button with Actionscript

posted: 02-02-06
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 28567
comments: 3
total pages: 1
Download FlashMX2004 Source Files

Live Example


How to enable and disable a button with Actionscript

In this basic Flash tutorial i will show you how you can disable and enable a buttons functionality with a small bit of Actionscript. You can check the source files to see how i setup this test file, but in short what i did was:

I created a square with the rectangle tool. Then i converted it into a movieclip called "buttonTemplate" (select it and press F8, fill in the name and press ok). After i converted it i duplicated (drag it with "alt" selected) it 3 times on stage and made one of the three a bit bigger. I gave the 2 small ones an instance name of "buttonA" and "buttonB", the bigger one i gave an instance name of "buttonC". Then i just placed a bit of static text above the buttons describing what they do. (in the source file you will also see a bit of textbox filling ;))

Btw don't get confused by the term button. I always use movieclips, but because we are setting up button like functionality on those movieclips they act the same as buttons and therefore i call them buttons all the time. I can imagine that term like that can be a bit confusing when you start out in Flash. In Flash you can setup movieclip objects to do almost anything you want, but for some people it also good to have a visual way to setup button functionality so they don't convert to a movieClip but to an actual button which gives them the ability to visually set the button states like UP, OVER and DOWN. Because you are learning to do it the more powerful Actionscript way, you don't really need to use actual buttons. You can just use movieclips and give them button functionlity how you please with Actionscript, which is ofcourse way more powerful and quick to implement for more objects etc.

The Actionscript that makes it work

This first piece of the code enables you to press with your mouse on buttonA. You can target that button because it has an instance name "buttonA". You can check its name by selecting "buttonA" on Stage and look at it properties box, you will see that it has as name: "buttonA" You could also check the other names and you will see they will be "buttonB" and "buttonC". With the line "buttonC.enabled = true;" we set "buttonC" to be enabled, so when you now press on "buttonC", a trace is outputted and you will see a message on your screen.
buttonA.onPress = function(){
    buttonC.enabled = true;
}

This sets up the press functionality for "buttonB". And on press we disable "buttonC" and so no trace is outputted onto your screen when you press on "buttonC".
buttonB.onPress = function(){
    buttonC.enabled = false;
}

With this part we setup the press event for "buttonC" itself. When someone pressed on the button the trace function will output a message onto your screen as long as "buttonC" is enabled.
buttonC.onPress = function(){
    trace("hi there people");
}

The Full Code

buttonA.onPress = function(){
    buttonC.enabled = true;
}

buttonB.onPress = function(){
    buttonC.enabled = false;
}

buttonC.onPress = function(){
    trace("hi there people");
}



Please refer all questions to the forum as it is much easier to reply there instead of the comment box below. Thanks for reading and being here.





Comments

3 comment(s) found in 1 page  
viewing comment(s): 1 - 3

1 |

16-05-07:guest

can you teach me, how to disable a button, and enable it only if all fields (radio button and checkboxes) are answered. I am making a survey form, and the submit button should be disabled until all the radiobuttons and checkboxes are answered.

17-05-06:guest

Thanks a lot

02-02-06:PrimeVector

New tutorial



- Press here to view all available smilies!



Latest News

Amazing Webmaster Tool

If you run a MySpace or teens related site with visitors interested in GLITTERS you will need this great tool: Webmaster Glitter Tool It will generate backlinks at an amazing pace!

 

Useful Click To Clipboard Script

This is a nice little copy to clipboard script. You can find it here!

 

Google Pages

Create pages on google quick and easy with Google Pages!

 

My Funny Pictures Site

Have fun on this funny pictutures site i made, i hope youll enjoy it :) PixHumor

 

IS YOUR CHILD SAFE???

With this great program you can make sure your child will be a lot safer while browsing the Internet.

 

New Site

I created a fun site for MSN Messenger Stuff called MSN Freak. You can find the site here: Free Winks @ MSN Freak

 

Useful free unlocker tool

This tool is a very useful tool that runs in the background. Whenever you try to for example copy a folder and explorer sceams at you it wont do that, the barrier is lifted and you can continue to do what you wanted too. Download it here.

 

GOOGLE worldwide: Languages and countries

Useful page showing all the Google Search Engines of all countries. Check it out here.

 

Google Trends

Google trends enables us to see where people want certain things the most. You can enter a keyword and find out where people search for that most often! Check Google Trends

 

Google PageRank Checker

I personally enjoy this Google pagerank checker a lot: Google PageRank Checker

 

New tutorials

I added a new basic Flash tutorial showing how to create a simple motion tween. You can find it here: Basic Flash Motion Tween

 

2 new Flash tutorials

I added 2 new Flash tutorials to the tutorial selection. On deals with the if else conditional statement and the other deals with setting up a bit of color with actionscript. You can checkout the tutorials here: Actionscript and colors and If Else Conditionals.

 

Oh My!

Its mad times overhere, im helping promote and have created a few sites which has taken quite some time from me. site1: http://www.flashgamefreak.com, site2 http://www.freewinks.net, site3 http://www.smilieworx.com and more are to come this and next week. This has taken its toll on my time to write tutorials. I will make up for that as soon as possible. Thanks for your understanding.

 

Basic Flash Tutorials

I added 3 new basic Flash tutorials to the tutorial list. Dealing with layers, What are functions and one about The Registration Point.

 

A bit of a delay

I have had a very crazy time around here the last month, because of this some tutorials have not been written yet. This is a list of tutorial i still need to write and you can expect them in the very near future.

 

Advertise on FWR

If you want to advertise here then please contact me here. We can make a nice deal and you can get stats on them how often you would like.

 

2 new tutorials

2 new web design related tutorials were added ... more Flash tutorials are on their way but its a bit mad around here these last days ;)

 

View All News



In Focus

Do you shop at Home Depot?
Moving any time soon? Home Depot has the supplies that you need for any move or any other occasion.
Save money on your Auto Insurance!
You could be paying too much for your auto insurance, find out here if you qualify for HUGE savings!
Get free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Make cash with Penny Stocks!
Make cash with Penny Stocks!
Play World War 2 Commando Free!
Play World War 2 Commando online for free!
Play Swat 2 Now
Play the swat 2 game and shoot those bad guys!
Play Bubble Boomers
Pop the bubbles of the like colors for this very fun and addictive game for FREE!!!
Play Aztec Quest online for free!
Play Aztec Quest online for free!
Play Airstrike 1941 online for free!
Play Airstrike 1941 online for free!
Customize your Facebook with a theme
Customize the colors and music on your facebook profile page today!
Play Frogger Now!
Play frogger with 1 click download of gamevance!
Get a Free Auto Insurance Quote
Free quotes from top insurance carriers!
Want Home Depot September Deals?
Get some great September deals at Home Depot today!
Do you shop at Home Depot?
Get a ton of Home Depot coupons and gift card offers! Also if your looking to improve you home this is the place to be!
Save up to 70% on auto insurance!
Do you qualify for 60-70% off your auto insurance rates? Find out right now! Request quotes 24 hours a day with our secure online form. It's super fast and easy. See what your rate is absolutely free!
Play Pac Man right here!
Get all the hottest new games and all the old school games in one place!
Play the old school Baseball Stars
Get all the hottest new games and all the old school games in one place!
Play Streetfighter Now!!!
Get all the hottest new games and all the old school games in one place!
Print Coupons from Home!
Print Grocery Coupons from your house! Instant Savings!
Share your Opinion, win $50,000?
Share your Opinion, win $50,000? Couldn't get any easier! It's free and fun!
Need New Contact Lenses for FREE?
Treat your eyes to all day comfort with a free trial and save up to $100!
Stop identity theft in its tracks!
Protect My ID makes it easy for you!
Test Hunting Gear and Keep It!
We're looking for a few good product testers for our Hunting Gear!
Save Money on Airline Fares Here!
Save up to 50% off on major airline brands here!
Get a FREE Apple iPad Now!!!
You know you want your FREE Apple iPad right here!
Do you like to go Fishing?
We're looking for a few good product testers to test our Fishing gear. Keep it afterwards!

Cool Stuff