• home
  • forum
  • my
  • kt
  • download
  • Custom Context(Right Click) Menu AS3

    Author: 2009-07-15 01:20:18 From:

    Your finished product should look like this SWF
    Right click anywhere to see context menu.

    First you have to set up your main class.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package{
        import flash.display.MovieClip
        import flash.events.ContextMenuEvent
        import flash.net.navigateToURL
        import flash.net.URLRequest
        import flash.ui.ContextMenu
        import flash.ui.ContextMenuItem
       
        public class main extends MovieClip{
            var c:ContextMenu = new ContextMenu();
            var item1:ContextMenuItem = new ContextMenuItem("©Drkgodz");
            var item2:ContextMenuItem = new ContextMenuItem("Go to my blog");
           
            public function main():void{
               
            }
        }
    }

    You should have to have the following imports:

    1
    2
    3
    4
    5
    6
    import flash.display.MovieClip
    import flash.events.ContextMenuEvent
    import flash.net.navigateToURL
    import flash.net.URLRequest
    import flash.ui.ContextMenu
    import flash.ui.ContextMenuItem

    This code:

    1
    2
    3
    4
    5
    var c:ContextMenu = new ContextMenu();
    var item1:ContextMenuItem = new ContextMenuItem("©Drkgodz");
    var item2:ContextMenuItem = new ContextMenuItem("Go to my blog");
    item1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onItemSel);
    item2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onItemSel);

    does a few things.
    The first line creates your custom Context Menu.
    Then the next 2 lines create your items(can go up to however many you want).
    Then after that, those 2 lines add the event listeners.
    Now that you have your basic outline done, add this code in the initializer function(main()).

    1
    2
    3
    c.hideBuiltInItems();
    c.customItems = [item1, item2];
    this.contextMenu = c;

    The first line hides the normal items in the right click menu(Zoom In, Prev, Next, Quality).
    The second line sets the items included to item 1 and item 2.
    This line is very important.
    If you didn’t have this line, your items won’t show up. If you want to add more items, you have to add it to this array.
    Then the last line sets the context menu of root to your custom context menu(c).
    Now you need to add the event listeners. I do it my own unique way. :)
    If you look at other tutorials, each item has it’s own event handler. If you look at my code, they both have one function – onItemSel:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    private function onItemSel(e:ContextMenuEvent):void{
    switch(e.target)
    {
        case item1:
            trace('item 1 selected');
            break;
        case item2:
            trace('item 2 selected');
            navigateToURL(new URLRequest('http://drkgodz.hobo-studios.org/blog/'));
            break;
        }
    }

    Okay, I used a switch case statment. What my code does is, get what item got clicked on(e.target). Then it switches it, and you do case item1: //what happens when you click on it here, case item2:, and so on.
    This keeps your code from cluttering and allows you to easily add more items.

    Now your whole class should look like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    package{
        import flash.display.MovieClip
        import flash.events.ContextMenuEvent
        import flash.net.navigateToURL
        import flash.net.URLRequest
        import flash.ui.ContextMenu
        import flash.ui.ContextMenuItem
       
        public class main extends MovieClip{
            var c:ContextMenu = new ContextMenu();
            var item1:ContextMenuItem = new ContextMenuItem("©Drkgodz");
            var item2:ContextMenuItem = new ContextMenuItem("Go to my blog");
           
            public function main():void{
                c.hideBuiltInItems();
                c.customItems = [item1, item2];
                this.contextMenu = c;
                item1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onItemSel);
                item2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onItemSel);
            }
            private function onItemSel(e:ContextMenuEvent):void{
                switch(e.target)
                {
                    case item1:
                        trace('item 1 selected');
                        break;
                    case item2:
                    trace('item 2 selected');
                        navigateToURL(new URLRequest('http://drkgodz.hobo-studios.org/blog/'));
                        break;
                }
            }
        }
    }

    Source Download

    discuss this topic to forum

    relation tutorial

    No information

    Category

      3D (36)
      Math Physics (18)
      3rd Party (10)
      Navigation (70)
      Actionscripting (251)
      Optimization (17)
      Animation (185)
      Projector (11)
      Audio (54)
      Special Effects (173)
      Backend (26)
      Text Effects (93)
      Drawing (34)
      Tips and Techniques (58)
      Dynamic Content (38)
      Tricks (8)
      Games (114)
      Utilities (24)
      Getting Started (99)
      Video (59)
      Interactivity (48)
      Web Design (37)

    New

    Hot