Haxe Forum > Right Click flash menu

  • Hello,
    I'm from stencyl which is now using Haxe as its language.

    I was wondering since the stencyl community hasn't really answered this; how would I go by editing the right click menu (Context Menu) using the haxe code. I've tried a few ways but I haven't really dipped much into haxe or AS3.

    I want it to look like this.

    Sponsored By: Sponsor Name

    Game By: Max Glockling

    I want both to open to links.

    Any answers would be lovely :D

    -Max

  • Not experimented with it for awhile, normally you can pretty much just follow an as3 tutorial...

    http://www.albinoblacksheep.com/tutorial/contextmenu

    You will need to import ContextMenu

    http://haxe.org/api/flash/ui/contextmenu
    http://haxe.org/api/flash/ui/contextmenuitem
    http://haxe.org/api/flash/ui/contextmenubuiltinitems

    if you get stuck I can make you a haxe flash example, but probably best if you give it a go yourself first.

  • Thanks :D will take a look into this.

  • This is what I think I was supposed to get but I got about 2 errors.

    var builtInItems : ContextMenuBuiltInItems = new ContextMenu();
    builtInItems : ContextMenuBuiltInItems();
    
    var my_sponsor : new ContextMenuItem("SPONSOR NAME");
    var my_name : new ContextMenuItem("By: Max Glockling");
    var my_copyright : new ContextMenuItem("Copyright - 2013");
    
    my_copyright.enabled = false;
    my_copyright.separatorBefore = true;
    
    my_menu.customItems.push(my_sponsor, my_name, my_copyright);
    
    contextMenu = my_menu;

    I'm sure you'll find something.
    Again, thank you

  • var builtInItems : ContextMenuBuiltInItems = new ContextMenu();
    builtInItems : ContextMenuBuiltInItems();
    It's quite obvious that can never be correct syntax... But please, post the errors next time if you get any, because it's hard to guess what they are.

    A working example:

    build.hxml:

    -main Main
    -swf index.swf
    -swf-header 1024:768:30:FFFFFF
    -D network-sandbox

    Main.hx:
    package;
    import flash.display.Sprite;
    import flash.events.ContextMenuEvent;
    import flash.Lib;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    
    class Main extends Sprite
    {
        public function new()
        {
            super();
            
            Lib.current.contextMenu = createContextMenu("Google");
            
            var s = new Sprite();
            s.buttonMode = true;
            s.graphics.beginFill(0xFF0000);
            s.graphics.drawRect(0,0,100,100);
            addChild(s);
            s.contextMenu = createContextMenu("Yahoo");
        }
        function onContextMenuItemSelect(event:ContextMenuEvent):Void
        {
            var caption:String = event.currentTarget.caption;
            flash.Lib.getURL(new flash.net.URLRequest("http://" + caption.toLowerCase() + ".com"), "_blank");
        }
        
        function createContextMenu(caption:String):ContextMenu
        {
            var item = new ContextMenuItem(caption);
            item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onContextMenuItemSelect);
            
            var cm = new ContextMenu();
            cm.hideBuiltInItems();
            cm.customItems.push(item);
            
            return cm;
        }
    
        public static function main()
        {
            Lib.current.addChild(new Main());
        }
    }

    The example shows a different contextmenu when clicking on the 'stage' than when clicking on the red rectangle.

    Also, the Adobe flash/actionscript3 documentation contains examples for just about every class, property or method:
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/ContextMenu.html#includeExamplesSummary

    Jan

  • Ah darn, Jan was answering this... I just made a test, maybe not useful but here is a snipit just incase.

            var menu = new ContextMenu();
            menu.hideBuiltInItems();
            var copyright = new ContextMenuItem("click for copyright date", true );
            copyright.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT,   
                                        function( e: ContextMenuEvent )
                                            {
                                                copyright.caption = "Copyright - 2013";
                                                copyright.enabled = true;
                                            } 
                                        , false, 0, true );
            copyright.separatorBefore = true;
            menu.customItems = [    new ContextMenuItem("SPONSOR NAME", false )
                                ,   new ContextMenuItem("By: Max Glockling", false )
                                ,   copyright
                                ];
            
            flash.Lib.current.contextMenu = menu;
  • Thanks guys. I have like 0 coding experience (Why I'm using Stencyl)

    I will try this out

  • Just tried Justins version since Stencyl adds all the import and everything

    This is the errors I get.

    Context Menu (59) - Missing ;
    Context Menu (44) - Class not found
    Context Menu (46) - Class not found
    Context Menu (47) - Unknown identifier
    Context Menu (55) - Class not found

  • Well I actually dumped it in my sine wave test rather than setup from scratch so here the example with a sinewave, like I said we can't really offer specific help with stencyl maybe you will have to get the stencyl guys to send both me and Jan full licenses to encourage us to start using it, I am sure it would make it easier for us to answer questions, but even if they did I can't promise we would have time to explore it. I am not sure stencyl runs on my leopard anyway but I think I installed it on my pc the evaluation version but to be honest it took me too long to find where I code haxe so I decided not to explore much further.

    -swf mathsinbyexample.swf
    -swf-version 9
    -main MathSinbyexample
    -swf-header 800:600:30:cccccc
    
    // author: JLM at justinfront
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import haxe.Timer;
    import flash.Lib;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.events.ContextMenuEvent;
    class MathSinbyexample
    {    
        public static inline var RED: Int = 0xFF0000;
        private var _stage: MovieClip;
        private var _view_mc: Sprite;
        private var _wide: Int;
        private var _step: Int;
        private var _amplitude: Int;
        private var _timeStep: Int;
        private var _timer: Timer;
        private var _x: Int;
        private var _y: Float;
        private var _dAngle: Int;
        private var _radians: Float;
        private var _angleChange: Float;
        
        public function new(
                            wide_: Int,
                            step_: Int,
                            amplitude_: Int,
                            timeStep_: Int,
                            dAngle_: Int
                            )
        {   
            _wide = wide_;
            _step = step_;
            _amplitude = amplitude_;
            _timeStep = timeStep_;
            _dAngle = dAngle_;
            _stage = Lib.current;
            _radians = 0;
            _angleChange = _dAngle * Math.PI/360;
            init();
        }
        
        private function init():Void
        {
            
            var menu = new ContextMenu();
            menu.hideBuiltInItems();
            var copyright = new ContextMenuItem("click for copyright date", true );
            copyright.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT,   
                                        function( e: ContextMenuEvent )
                                            {
                                                copyright.caption = "Copyright - 2013";
                                                copyright.enabled = true;
                                            } 
                                        , false, 0, true );
            copyright.separatorBefore = true;
            menu.customItems = [    new ContextMenuItem("SPONSOR NAME", false )
                                ,   new ContextMenuItem("By: Max Glockling", false )
                                ,   copyright
                                ];
            
            flash.Lib.current.contextMenu = menu;
            
            createView();
            _view_mc.graphics.lineStyle( 0, RED, 1);
            _timer = new Timer( _timeStep );
            _timer.run = drawSine;
        }
        
        private function createView():Void
        {
            _view_mc = new Sprite();
            _view_mc.x = 10;
            _view_mc.y = 250;
            _stage.addChild( _view_mc );
            
        }
    
        public function drawSine():Void
        {
            if( (_x+=_step) > _wide )
            {
                _timer.stop();
            }
            _radians += _angleChange;
            _y = _amplitude * Math.sin( _radians );
            _view_mc.graphics.lineTo( _x, _y );
        }
        
        public static function main(): Void 
        {   
            var wide:Int = 760;
            var step:Int = 3;
            var amplitude: Int = 200;
            var timeStep: Int = 30;
            var dAngle: Int = 5;
            new MathSinbyexample( wide, step, amplitude, timeStep, dAngle ); 
        }
    
    }
< Prev | Page 1 / 1 | Next >