A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: creating my own right click menu

  1. #1
    Member
    Join Date
    Apr 2004
    Posts
    49

    creating my own right click menu

    hi.

    i am creating a software simulation and need to create my own right click so match the software im simulating. Ive got as far as to remove the normal menu items and add my own which then displays a movie clip. the code im using for that is...

    var mymenu = new ContextMenu();
    mymenu.hideBuiltInItems();
    this.menu = mymenu;
    _root.menu = mymenu;
    mymenu.customItems.push(new ContextMenuItem("Format", myformat));
    function myformat(){
    _root.format._visible=true;
    }

    I would like to have more than one menu item added (i need 4 more) but cant work out how to have more than one.

    even better, i would like to attach different menus to different buttons and mc's.

    how do i go about this?

    thanks

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    easy enough to add more items (name them differently).
    You can change the menu by making a handler for it....

    Code:
    _root.format._visible=false;
    var mymenu = new ContextMenu();
    mymenu.hideBuiltInItems();
    
    this.menu = mymenu;
    _root.menu = mymenu;
    // new menu items //
    mymenu.customItems[0] = new ContextMenuItem("SHOW", showFn);
    mymenu.customItems[1] = new ContextMenuItem("HIDE", hideFn);
    mymenu.customItems[2] = new ContextMenuItem("TOGGLE", toggleFn);
    
    trace(mymenu.customItems.length);
    
    // menu functions //
    function showFn(){
    	_root.format._visible=true;
    }
    function hideFn(){
    	_root.format._visible=false;
    }
    function toggleFn(){
    	_root.format._visible=!_root.format._visible;
    }
    
    // menu handler //
    function constructMenu ( _args_ ){
    	// go through all menu items
    	for(i = 0; i < mymenu.customItems.length; i++){
    		// disable menu item (default action)
    		mymenu.customItems[i].visible = false;
    		// loop through posted args
    		for (j=0; j<arguments.length; j++) {
    			if (i == arguments[j]) {
    				// enable if arg matches arr position
    				mymenu.customItems[i].visible = true;
    				break;
    			}
    		}
    	
    	}
    }
    
    // example code on 4 buttons //
    button1.onRollOver = function(){
    	constructMenu(0);		
    }
    
    button2.onRollOver = function(){
    	constructMenu(1);		
    }
    
    button3.onRollOver = function(){
    	constructMenu(0,1,2);		
    }
    
    button4.onRollOver = function(){
    	constructMenu();		
    }
    hope that helps.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  3. #3
    Member
    Join Date
    Apr 2004
    Posts
    49
    thanks for the reply

    im a bit baffled to some of the code. ill try to be a bit more specific now i know how it will work.

    i have 3 buttons on the stage. With instance names:

    mycomputer
    floppy
    folder

    When the user right clicks i want it to display a different menu for whatever button they right clicked on.

    so for example the "floppy" will have options:

    format
    rename
    edit

    you could might be what im after but im just not sure exactly how to use it so any pointers would be great Thanks again for the help.

  4. #4
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    since you cannot detect a right mouse click in actionscript (and therefore change the menu settings on right click), I get round this by changing the menu settings when the mouse rolls over the button. Then when the user right clicks, the menu is already set up to have the correct items.

    The menu items are all stored in an array at the begining and so I set up a function that reads a list of numbers of allowable menu items.

    the function constructMenu() is set up so that it will allow any number of parameters to be passed to it. So it doesn't matter if there are 0 or 500 menu items you want to enable for a given button.

    constructMenu(0); will construct the menu so that mymenu.customItems[0] will be enabled (e.g. "SHOW"), all other menu items are disabled by default.

    If i want the first two custom menu items to appear, I will call the function with different parameters, e.g.constructMenu(0,1); (only "SHOW" and "HIDE" are enabled).

    constructMenu(); will disable all menu items.

    constructMenu(0,1,2,3,4,5,6,7,8); will enable all the menu items specified in my example below.


    so in your case...

    Code:
    _root.format._visible=false;
    var mymenu = new ContextMenu();
    mymenu.hideBuiltInItems();
    
    this.menu = mymenu;
    _root.menu = mymenu;
    // new menu items //
    mymenu.customItems[0] = new ContextMenuItem("SHOW", showFn);
    mymenu.customItems[1] = new ContextMenuItem("HIDE", hideFn);
    mymenu.customItems[2] = new ContextMenuItem("TOGGLE", toggleFn);
    mymenu.customItems[3] = new ContextMenuItem("Format", format); //format is a function written below
    mymenu.customItems[4] = new ContextMenuItem("Rename", rename); //rename is a function written below
    mymenu.customItems[5] = new ContextMenuItem("Edit", edit); //edit is a function written below
    mymenu.customItems[6] = new ContextMenuItem("Item", itemFn); // you will need to add the other functions
    mymenu.customItems[7] = new ContextMenuItem("Other Item", otherFn);
    mymenu.customItems[8] = new ContextMenuItem("Whatever", whateverFn);
    
    
    // menu functions // here you would specify all functions accessed by the menu
    // I have only written the 3 functions for the floppy button, you will need to add the rest...
    function format(){
    	// do edit}
    function rename(){
    	// do rename
    }
    function edit(){
    	// do edit
    }
    
    // menu handler //
    function constructMenu ( _args_ ){
    	// go through all menu items
    	for(i = 0; i < mymenu.customItems.length; i++){
    		// disable menu item (default action)
    		mymenu.customItems[i].visible = false;
    		// loop through posted args
    		for (j=0; j<arguments.length; j++) {
    			if (i == arguments[j]) {
    				// enable if arg matches arr position
    				mymenu.customItems[i].visible = true;
    				break;
    			}
    		}
    	
    	}
    }
    
    // example code on the 3 buttons //
    mycomputer.onRollOver = function(){
    	constructMenu(0,1,2);		
    }
    
    floppy.onRollOver = function(){
    	constructMenu(3,4,5);		
    }
    
    folder.onRollOver = function(){
    	constructMenu(6,7,8);		
    }
    
    // this will disable all menu items, you might want to do this for 
    // every button so that if not over a button then all menu items are disbled.
    floppy.onRollOut = function(){
    	constructMenu();		
    }
    I hope that helps a little, let me know if you are still unsure on anything.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  5. #5
    since you cannot detect a right mouse click in actionscript (and therefore change the menu settings on right click)
    It is possible with Flash 7.

    Here's a great tutorial on how to do this:

    Right Click Tutorial


  6. #6
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    cool, thanks for the tip off
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  7. #7
    Member
    Join Date
    Apr 2004
    Posts
    49
    thanks for all the help guys

    think ive got it now

    simon

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