Single Frame Navigation Example

This sample's timeline:
timeline of example

  1. /******* Gordons single frame menu system example ************/
  2.  
  3. // the basic idea is to control the visibilty of pages using a menu of buttons, all on one frame rather
  4. // than have each page on a separate frame, and using gotoAndStop to move to the frame we want
  5. // this appraoch greatly simplifies the quit page logic too.
  6.  
  7.  
  8. // b1_MC,  b2_MC & b3_MC are the movieClips we use as buttons -  that constitute the menu
  9. // p1_MC, p2_MC & p3_MC are the respective "pages" that "open" (are made visible)
  10.  
  11. // add a property to each of the movieClips we are using as buttons
  12. // this new property, "openBox" points to the page each "button" will open
  13. // this way, we can use one event function to handle the opening of all pages
  14. // note you can add new properties to movieClips simply by stating them like this:
  15. b1_MC.openBox = p1_MC;
  16. b2_MC.openBox = p2_MC;
  17. b3_MC.openBox = p3_MC; // this is our quit screen
  18. /// adding properties in this way only works on movieClips - will not work on any other symbol type.
  19.  
  20. // make the all pages invisible at start up - if you want to have a startup page visible, set one of these to true, and currentPage = to the instance name of that movieClip
  21. p1_MC.visible = false;
  22. p2_MC.visible = false;
  23. p3_MC.visible = false;
  24. byebye_MC.visible = false;
  25.  
  26. //keep track of which "page" is open, so if a page is already open, we close it when we open another
  27. // in this example, no page is open when we start so set this to null
  28. var currentPage = null;
  29.  
  30. // add the event linstener to our movieClip buttons, and make the mouse appear as a hand
  31. // when it rolls over the button ( buttonMode property )
  32. b1_MC.addEventListener(MouseEvent.CLICK, showBoxes ); // showBoxes is the function that is called when the event is detected
  33. b1_MC.buttonMode = true;
  34.  
  35. b2_MC.addEventListener(MouseEvent.CLICK, showBoxes );
  36. b2_MC.buttonMode = true;
  37.  
  38. b3_MC.addEventListener(MouseEvent.CLICK, showBoxes ); // this is the quit button
  39. b3_MC.buttonMode = true;
  40.  
  41. // set up the close button for each page, if you provide one
  42. // note that the close buttons are inside of their respective pages
  43. // hence we must name the instance of the parent movieClip first - e.g.: parent_instance_name.child_instance_name
  44. p1_MC.close_btn.addEventListener(MouseEvent.CLICK, closeBox );
  45. p2_MC.close_btn.addEventListener(MouseEvent.CLICK, closeBox );
  46.  
  47. //p3_MC - the quit button - has no close button, but rather yes quit and no don't quit buttons
  48. // we handle these as special cases
  49. p3_MC.yes_btn.addEventListener(MouseEvent.CLICK, showByeBye ); // show the quit screen
  50. p3_MC.no_btn.addEventListener(MouseEvent.CLICK, returnToCurrent ); // user decideds not to quit, so we will show the current page, if there was one
  51.  
  52. byebye_MC.reload_btn.addEventListener(MouseEvent.CLICK, startOver ); // show the quit screen
  53.  
  54. // if a movieClip button is clicked on
  55. // first check to see if a box is already open, if true close it
  56. // then open the appropraite box, and set our currentPage pointer
  57. // if the quit button was clicked (b3_MC) then handle that a little differently
  58. function showBoxes( evt ){
  59.     if( currentPage != null ){ // make sure there is a current page showing before trying to close it...
  60.         currentPage.visible = false; // hide the currentPage
  61.     }
  62.     if( evt.target.name == "b3_MC" ){ // has the quit button been clicked?
  63.         showMenu( false ); // hide all the menu items, don't change currentPage value
  64.     }else{ // if not the quit button, handle normally - set the currentPage pointer to the button's current page
  65.         currentPage = evt.target.openBox
  66.     }
  67.     evt.target.openBox.visible = true; // show this button's page
  68. }
  69.  
  70. // event that handles the close box in each of the pages
  71. function closeBox( evt ){
  72.     // since the close button is inside the movieClip, and we want to hide the movieClip, the target needs to tell it's parent to close.
  73.     evt.target.parent.visible = false;
  74.     currentPage = null;
  75. }
  76.  
  77. //hide or show the menu buttons when we click on quit
  78. function showMenu( showOrHide ){ // showOrHide is a boolean passed to this function
  79.     b1_MC.visible = showOrHide;
  80.     b2_MC.visible = showOrHide;
  81.     b3_MC.visible = showOrHide;
  82. }
  83.  
  84. function returnToCurrent( evt ){ // user decided not to quit
  85.     p3_MC.visible = false; // hide the quit button
  86.     if( currentPage != null ){ // since we allow the possiblity that no page is showing, we need to check ( see the closeBox function )
  87.         currentPage.visible = true; // show the page that was visible when the user clicked quit
  88.     }
  89.     showMenu( true );
  90. }
  91.  
  92. function showByeBye( evt ){ // this is the end screen, but we provide a reload button in this example
  93.     p3_MC.visible = false;
  94.     byebye_MC.visible = true;
  95. }
  96.  
  97. function startOver( evt ){ // the user has quit, but we give them a chance to reset and run again
  98.     // reset all back to startup state:
  99.     byebye_MC.visible = false;
  100.     showMenu( true );
  101.     currentPage = null;
  102. }
  103.  

Gordon Graber 2009