dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Button Class

  1. #1
    come 'ere a minute
    Join Date
    May 2004
    Location
    BC, Canada
    Posts
    50

    Button Class

    Hi everyone,

    I have a Button class I made, which is Buttons.as and I link that to each movie clip in my menu.fla file. It works how it is setup right now, but I need to make it so when I click one of the buttons it stays selected (downState) and then when I click another button it will stay selected and the other will reset to the normalState. I bet this is probably fairly simple with using an 'if' statement, I'm just not sure how to go about doing it.

    Here's my script:
    Code:
    class Buttons extends MovieClip {
    	// connects handlers with the functions
    	function Buttons() {
    		onRollOver = overState;
    		onRollOut = normalState;
    		onPress = downState;
    		onRelease = releaseState;
    		onDragOut = dOutState;
    		onDragOver = dOverState;
    	}
    	// function for normalState
    	function normalState() {
    		// normal state (frame 1 of mc)
    		gotoAndStop(1);
    	}
    	// function for overState
    	function overState() {
    		// over state (frame 2 of mc)
    		gotoAndStop(2);
    	}
    	// function for downState
    	function downState() {
    		// over state (frame 3 of mc)
    		gotoAndStop(3);
    	}
    	// function for onRelease
    	function releaseState() {
    		// mouse up (frame 2 of mc)
    		gotoAndStop(2);
    	}
    	// function for Draging off the mc
    	function dOutState() {
    		// drag out of mc (frame 1 of mc)
    		gotoAndStop(1);
    	}
    	// function for Dragging over the mc
    	function dOverState() {
    		// drag over of mc (frame 2 of mc)
    		gotoAndStop(2);
    	}
    }
    I am greatful for any help.

    Thank you,
    PrimalSniper
    http://www.streamsearch.ca - The music you want, when you want it
    http://www.machonemedia.ca - Web development and other media related services
    http://www.myshortfilms.com - A community for short film makers to comment and rate films

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    This tutorial is for MX but you may be able to take some information for what you try to accomplish:

    http://www.flashscript.biz/memoclips...but_color.html
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    The problem seems like no reference.

    Change the constructor of your class to following code and should work:

    Code:
       var obj:MovieClip;
       function Buttons(target:MovieClip) {
           obj = target;
           obj.onRollOver = overState;
           obj.onRollOut = normalState;
           obj.onPress = downState;
           obj.onRelease = releaseState;
           obj.onDragOut = dOutState;
           obj.onDragOver = dOverState;
       }
    Challenging you: I explore my strengths;
    Being challenged: I discover my weaknesses;
    Defeating you: I GIVE you my knowledge;
    Being defeated: I GAIN your knowledge;

  4. #4
    come 'ere a minute
    Join Date
    May 2004
    Location
    BC, Canada
    Posts
    50
    Hmm, thats doesn't work, I didn't think it would either. My script is a class, an external actionscript file (Buttons.as). I know I need a variable and if statements im just not sure how to do it, my script works perfectly right now, I just need it so remember which one was pressed and stay selected untill a new button is pressed then the new button stayed pressed and the other buttons are normal. This is kinda for school which I am totally stumped on, but im gonna keep trying.

    Let me know if you guys figure it out.

    Thanks.
    http://www.streamsearch.ca - The music you want, when you want it
    http://www.machonemedia.ca - Web development and other media related services
    http://www.myshortfilms.com - A community for short film makers to comment and rate films

  5. #5
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    I managed to come up with this. You can create as many buttons as you want and register with regButtons class so that they can be treated as a group of buttons.

    But, it's kind of cranky and I am quite sure that experts of AS2 will come up with better solutions.

    Also, you need to understand that by build this Button class, you need to use eventDispatcher to dispatch events captured by Buttons class so that you can perform action.

    I will also work on this again later to refine it for better solution. For example, improve the class to allow multiple group of buttons (currently all buttons registered will be in the same group), add event dispatcher to the button class to dispatch events to main movie.
    Attached Files Attached Files
    Last edited by websam; 05-30-2005 at 01:34 AM.
    Challenging you: I explore my strengths;
    Being challenged: I discover my weaknesses;
    Defeating you: I GIVE you my knowledge;
    Being defeated: I GAIN your knowledge;

  6. #6
    Senior Member
    Join Date
    Sep 2000
    Location
    Pittsburgh
    Posts
    252
    Hi,

    I think maybe the simplest way to do it, although i'm not certain it is the best way, would be to create a static variable which houses the currently selected clip and maybe combine some of those event handlers so you don't have as many...

    Code:
    class Buttons extends MovieClip {
    	
    	private static var _selected:MovieClip;
    	
    	function Buttons() {
    		onRollOver = onDragOver = overState;
    		onRollOut =  onDragOut = normalState;
    		onPress = downState;
    		onRelease = releaseState;
    	}
    
    	function normalState() {
    		if(_selected != this){
    			gotoAndStop(1);
    		}
    	}
    
    	function overState() {
    		if(_selected != this){
    			gotoAndStop(2);
    		}
    	}
    
    	function downState() {
    		if(_selected != this){
    			gotoAndStop(3);
    		}
    	}
    
    	function releaseState() {
    		_selected.gotoAndStop(1);
    		_selected = this;
    		gotoAndStop(2);
    	}
    }
    hope this helps some,
    Dunc
    Last edited by Dunc; 05-30-2005 at 01:07 AM.

  7. #7
    come 'ere a minute
    Join Date
    May 2004
    Location
    BC, Canada
    Posts
    50
    Hey Dunc,

    Thank you, that's exactly what I needed. That's what I had too! heh. Except I was setting the variable to true on some of the functions and then saying if(isPressed == true){blah blah}. Anyways, thanks.
    http://www.streamsearch.ca - The music you want, when you want it
    http://www.machonemedia.ca - Web development and other media related services
    http://www.myshortfilms.com - A community for short film makers to comment and rate films

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center