A Flash Developer Resource Site

Page 1 of 3 123 LastLast
Results 1 to 20 of 47

Thread: movie clip buttons

  1. #1
    Member
    Join Date
    Oct 2006
    Posts
    79

    movie clip buttons

    hi

    im having a problem trying to find out how to make a button stay in its rollover state after
    being clicked and then when another button is clicked switch it to that one ,and so on.
    please help, ive looked everywhere

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    This is not precisely what you are looking for, but may give you some idea.

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

  3. #3
    Member
    Join Date
    Oct 2006
    Posts
    79
    thanks for the help

    if any one finds out any other ideas please post

  4. #4
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    I think i have an idea what you want to do. I will look into it when i get home this afternoon (probably in another hour or so) and see if i can sort something out for you.

  5. #5
    Member
    Join Date
    Oct 2006
    Posts
    79
    thanks for the trouble

  6. #6
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    ok here you go.

    This is actually not the color function you want but the concept is the same. You would just edit the button states to be colors instead of positions. The area you want to concentrate on is the stick function which is what makes the button stay on the down state until another button is pressed.

    If you need more explanation on the code let me know.
    Attached Files Attached Files

  7. #7
    Member
    Join Date
    Oct 2006
    Posts
    79
    thats exactly what i wanted
    thank you so much for your help
    could you go through the code for me

    cheers v1knight

  8. #8
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    No problem, here you go.

    Inside of the buttons (grayButton_mc) you have the labels and all the states of the button (over, down and out) the hit area is the same graphic with it's alpha set to 0 and it is also used as the mask for the button. In your case you may most likely not need the mask since you are doing color transitions and not graphic animation. Everything else should be self explanatory

    I tried as much as possible to comment everything in the code for you but i will go more in depth. the first section of code i put the stop action for obvious reasons and then i defined a variable that holds the value of what the current page is. The page that is currently loaded on the screen. I do not assign a value to that as yet because that is done when you press the buttons. If you notice when you load up the movie nothing appears on the screen except the menu and no buttons are selected. You would need to change the code and add code to fix that for the initial page that you want loaded.

    Code:
    stop();
    
    // create variable to track current page for button states
    var currentPage;
    The 2nd set of code creates a movieclip called holder_mc on the root timeline that will be holding your content and then moves that empty clip to the center of the stage. you could of course do it differently since there are many methods of loading content. Just use your method.

    Code:
    //create holder for content
    this.createEmptyMovieClip("holder_mc", this.getNextHighestDepth());
    holder_mc._x = Stage.width/2;
    holder_mc._y = Stage.height/2;

  9. #9
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    Next i assigned the button states. Instead of writing out all the code under each button I created functions that assigned the actions. I am only posting the code for 1 button but it is the same for all the other 3. In the onRollover, onRollout and onRelease of each button, the button passes itself to the rollover(), rollout() and stick() functions respectively where it goes and does the actions in that function The only exception is the loadContent() call in the onRelease that passes the name of the movieclip that it wants to load in the content area.

    Code:
    //***  Assign Button States  ***
    home_mc.onRollOver = function() {
    	rollover(this);
    }
    
    home_mc.onRollOut = function() {
    	rollout(this);
    }
    
    home_mc.onRelease = function() {
    	loadContent("home_mc");
    	stick(this);
    }

  10. #10
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    all of those buttons point to the functions below. I will go through each one separately. the rollover(movieName) and rollout(movieName) functions receive the name of the button that was rolled over and out and tests to see if it is actually the current page that is loaded. If it isn't then it plays the over state of that button otherwise it does nothing since it will be in the down state. And you don't want the rollover and rollout action occurring if it is in the down state.

    Code:
    function rollover(movieName){
    	if (currentPage != movieName){
    		movieName.gotoAndPlay("over");
    	}
    }
    
    function rollout(movieName){
    	if (currentPage != movieName){
    		movieName.gotoAndPlay("out");
    	}
    }

  11. #11
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    On release of the button the stick(movieName) function is called and receives the name of the button to stick in the down position. If it is not then it plays the rollout or out state of the button that is currently in the down state then sets the current page to the page that you want to load then goes and stops at the down state of that new button that was pressed.


    Code:
    function stick(movieName){
    	if (currentPage != movieName){
    		currentPage.gotoAndPlay("out");
    	}
    	currentPage = movieName;
    	currentPage.gotoAndStop("down");
    }
    at the same time it also loads the content for that new page into the movieclip you created at the beginning of the code. It passes the name of the content movie you want to load(attach) from the library or file system (the method would vary based on the method you choose to load your content). it then put that content into the holder movieclip with an instance name of the movie being the same name as the movie being loaded and puts it at level 1. If you are just attaching the content from the library then remember that you need to export each of the content movieclips for actionscript.

    Code:
    function loadContent(movieName){
    	holder_mc.attachMovie(movieName, movieName ,1);
    }

    and there you have it. I hope that was good enough for you. Let me know if you have any more questions about it.

  12. #12
    Member
    Join Date
    Oct 2006
    Posts
    79
    thanks for the explanation
    ill have a go at editing it now

  13. #13
    Member
    Join Date
    Oct 2006
    Posts
    79
    ive copied the code i needed and at the moment ive only done 1 button (about)
    it all works except from once ive clicked it, when i rollover it again it goes back to
    the first frame of the button.
    could you please tell me what ive done wrong.
    Attached Files Attached Files

  14. #14
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    before i look at the file i can already guess it is either in the if statement or setting of the variable or a wrong stop code on the button but i will let you know for sure after i look at the file

  15. #15
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    Actually it was your frame names. remove the _ from in your names and code _out _down _over make then out, over and down. Flash has a tendency to use the _ for buttons a little more than you may want sometimes. the problem is that you are actually sending too much information to flash when you use the _over so flas looks at that as a button function and treat it that way regardless of what other code you have assigned to the function. Flash "knows" that once it encounters an _over that it is to automatically jumps to that frame in a button. So even if you have other code there, flash will ignore it. Removing the _ will fix that problem.

  16. #16
    Member
    Join Date
    Oct 2006
    Posts
    79
    thank you once again
    im learning a lot from you

  17. #17
    Member
    Join Date
    Oct 2006
    Posts
    79
    the way im used to linking the buttons to the pages is to make frames on the root timeline with labels saying the name of the pages and then putting in a (gotoAndPlay("E.G")
    but i dont know how to make the code that we've been doing to do that
    how do you do it
    thanks

  18. #18
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    you could do it one of 2 ways. Either change the loadContent function to what is below and make sure that the release statement passes the name of the label that it needs to go to.

    Code:
    home_mc.onRelease = function() {
    	loadContent("home");
    	stick(this);
    }
    
    function loadContent(movieName){
    	gotoAndPlay(movieName);
    }
    or delete the loadContent function altogether and change the onRelease functions of all the buttons to be

    Code:
    home_mc.onRelease = function() {
    	gotoAntPlay("frameLabel");
    	stick(this);
    }
    Last edited by VI Knight; 10-19-2006 at 04:40 PM.

  19. #19
    Member
    Join Date
    Oct 2006
    Posts
    79
    thanks
    all the links are working now
    at the start of the timeline it plays the first button but how do i get the button to stop on its down state automatically just as if it has been clicked
    thankyou for all your help

  20. #20
    Island Boy
    Join Date
    Apr 2006
    Posts
    655
    add this at the top of your code

    stick(about_mc);

    substutute the about_mc for whatever button it is that you want the stick

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