A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Making a button stay in down state

  1. #1
    Senior Member
    Join Date
    Oct 2002
    Posts
    126

    Making a button stay in down state

    Hi!

    I am trying to make a button stay in down state after it has been clicked, so it looks like it is active.

    How can I do that?

    thx...

  2. #2
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Maybe something like this ( although you can do this in at least 100 different ways):

    Code:
    my_button.stop();
    
    my_button.addEventListener(MouseEvent.MOUSE_DOWN, onBtnPress);
    my_button.addEventListener(MouseEvent.MOUSE_OVER, onBtnOver);
    my_button.addEventListener(MouseEvent.MOUSE_OUT, onBtnOut);
    
    function onBtnPress(event:MouseEvent):void
    {
    	my_button.gotoAndStop(3);
    }
    function onBtnOver(event:MouseEvent):void
    {
    	my_button.gotoAndStop(2);
    }
    function onBtnOut(event:MouseEvent):void
    {
    	if (my_button.currentFrame != 3)
    	{
    		my_button.gotoAndStop(1);
    	}
    }
    my_button is a movie clip on the stage that has 3 frames, the first frame is the "up" state, second frame is the "over" state, and the third frame is the "down" state ( the last two frames are identical ).



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #3
    Senior Member
    Join Date
    Oct 2002
    Posts
    126
    Thx for your reply.

    Thing is I have a button already on the stage with different looks for the up and down states. I also linked a class to this button and in this class I made a listener for the up and down states. However when I write e.g. gotoAndStop(3) inside the class I get the error message :

    1180: Call to a possibly undefined method gotoAndStop.

    I think I get this error because the class is not extending movieclip but the simplebutton class.

    How can I use gotoAndStop inside the class ?

    thx..

  4. #4
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Hmm, I just made a fast test to see if it works, and it should. I created a movie clip called "box" on the stage, I stopped it's first frame and added a keyframe on frame 5. onPress i made it gotoAndStop frame 5 ( since the first frame has a stop(); on it, the movie won't play ).

    Code:
    package
    {
    	import flash.display.*;
    	import flash.events.*;
    	
    	public class GoToAndStop extends Sprite
    	{
    		public function GoToAndStop()
    		{
    			init();
    		}
    		protected function init():void
    		{
    			box.addEventListener(MouseEvent.CLICK, onClick);
    		}
    		protected function onClick(event:MouseEvent):void
    		{
    			box.gotoAndStop(5);
    		}
    	}
    }
    Works just fine, and I don't think that your class has to extend MovieClip nor Sprite, because you have the movie clip on your stage, created manually, it will know that it's a movie clip...

    Dunno, work fine...

    PS: make sure that you've imported all the classes you'll be using in your own class.
    Last edited by fx.barrett; 12-27-2007 at 09:11 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  5. #5
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    doorman, you won't be able to do it with a SimpleButton. You must create a custom class that extends flash.display.MovieClip in order to add a toggle functionality to it.

  6. #6
    Senior Member
    Join Date
    Oct 2002
    Posts
    126
    Thx for your quick reply PlenaryCreation but I think you may be misunderstanding me.

    I am not using a movieclip.

    I have a button on the stage. Inside the button I have made different look for the up stage and down state frames. I have a class (extending simplebutton) linked to the button in the library. Inside this class I have written a mouseUp listener, so when I release I want to remain in the down state of the button. Hence, I am trying to use gotoAndStop(3) where frame 3 is the down state of the button.

    Or maybe you are understanding me correctly?

    Thx..

  7. #7
    Senior Member
    Join Date
    Oct 2002
    Posts
    126
    I see!! MyFriendIsATaco

    ...but I am afraid that if I use movieclip the cursor of the mouse won´t change its shape to the default button "hand cursor". I think it is important because I am not using rollovers to make the button more clearly separate from the surrounding.

    Or does the mouse cursor also change using mouse state listeners on movieclips?

    ....thx

  8. #8
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    yeah, it'll change depending on what you are doing with the movieclip. If not, you can just call: clip.buttonMode = true; to force the hand cursor.

  9. #9
    Senior Member
    Join Date
    Oct 2002
    Posts
    126
    cool never heard of that functionality... thx

  10. #10
    Junior Member
    Join Date
    Jun 2008
    Posts
    5
    hey guys.

    this has helped a lot. this is the only place that i have found that actually gives advice that works.

    one more question:
    how do you get the button to stay in the "Mouse Over" state after it has been clicked? The code above works great but it only stays in the "Mouse Over" state until you roll over it with your mouse again. I guess what i want is to disable the MOUSE_OVER after it has been clicked. Any suggestions?

    Thank you so much.

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