zoukankan      html  css  js  c++  java
  • SOLVED: Right Click in AS3

    A day or two ago polyGeek has revived an old and challenging idea that one could make use of custom right-click functionality in Flash (AS3 + Javascript).

    Why would anyone want to do this? Well, there are several very important reasons:

    1) Games – the power of AS3 has brought Flash to the world of digital entertainment. At last it is possible to focus on the idea of your game rather than on how to improve the laggy experience. One thing that is still missing – right click functionality. We had this forever in desktop games, now it is time to let your casual RTS, RPG and FPS creations conquer the web.

    2) User Experience – 2 buttons are better than 1. Every experimentalist's dream is to be able to have more input options, not just one button. I can bet someone would soon create a stunning interface using this new functionality and we would see that on no less than FWA.

    3) RIA – Rich Internet Applications. My clients are often asking if it is possible to remove embeded Flash Player menus from their applications and replace them with their company’s branding stuff.

    ***

    AND THE ANSWER IS – YES! It is now possible to use custom right-click functionality in Flash and even Flex.

    After long hours of searching through Microsoft’s documentation I came up with a universal solution that works nicely at least on 3 major browsers – Firefox 2, IE 7 and Safari. (IE6 has not been tested but I can bet it works OK).

    ***

    Here you can see the * DEMO of right click * functionality (click the grey area to draw transparent dots)

    Javascript source code looks like this:

    JAVASCRIPT:
    1. /**
    2. *
    3. * Copyright 2007
    4. *
    5. * Paulius Uza
    6. * http://www.uza.lt
    7. *
    8. * Dan Florio
    9. * http://www.polygeek.com
    10. *
    11. * Project website:
    12. * http://code.google.com/p/custom-context-menu/
    13. *
    14. * --
    15. * RightClick for Flash Player.
    16. * Version 0.6.2
    17. *
    18. */
    19.  
    20. var RightClick = {
    21.     /**
    22.      *  Constructor
    23.      */
    24.     init: function () {
    25.         this.FlashObjectID = "customRightClick";
    26.         this.FlashContainerID = "flashcontent";
    27.         this.Cache = this.FlashObjectID;
    28.         if(window.addEventListener){
    29.              window.addEventListener("mousedown", this.onGeckoMouse(), true);
    30.         } else {
    31.             document.getElementById(this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); }
    32.             document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}
    33.             document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;
    34.         }
    35.     },
    36.     /**
    37.      * GECKO / WEBKIT event overkill
    38.      * @param {Object} eventObject
    39.      */
    40.     killEvents: function(eventObject) {
    41.         if(eventObject) {
    42.             if (eventObject.stopPropagation) eventObject.stopPropagation();
    43.             if (eventObject.preventDefault) eventObject.preventDefault();
    44.             if (eventObject.preventCapture) eventObject.preventCapture();
    45.          if (eventObject.preventBubble) eventObject.preventBubble();
    46.         }
    47.     },
    48.     /**
    49.      * GECKO / WEBKIT call right click
    50.      * @param {Object} ev
    51.      */
    52.     onGeckoMouse: function(ev) {
    53.         return function(ev) {
    54.         if (ev.button != 0) {
    55.             RightClick.killEvents(ev);
    56.             if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
    57.                 RightClick.call();
    58.             }
    59.             RightClick.Cache = ev.target.id;
    60.         }
    61.       }
    62.     },
    63.     /**
    64.      * IE call right click
    65.      * @param {Object} ev
    66.      */
    67.     onIEMouse: function() {
    68.         if (event.button> 1) {
    69.             if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
    70.                 RightClick.call();
    71.             }
    72.             document.getElementById(RightClick.FlashContainerID).setCapture();
    73.             if(window.event.srcElement.id)
    74.             RightClick.Cache = window.event.srcElement.id;
    75.         }
    76.     },
    77.     /**
    78.      * Main call to Flash External Interface
    79.      */
    80.     call: function() {
    81.         document.getElementById(this.FlashObjectID).rightClick();
    82.     }
    83. }

    On the Flash side is as simple as this code (AS3):

    Actionscript:
    1. package {
    2.    
    3.     import flash.display.*;
    4.     import flash.external.ExternalInterface;
    5.  
    6.     public class RightClick extends Sprite
    7.     {
    8.        
    9.         public function RightClick()
    10.         {
    11.             stage.scaleMode = StageScaleMode.NO_SCALE;
    12.             stage.align = StageAlign.TOP_LEFT;
    13.            
    14.             var methodName:String = "rightClick";
    15.             var method:Function = onRightClick;
    16.             ExternalInterface.addCallback(methodName, method);
    17.         }
    18.        
    19.         private function onRightClick():void {
    20.  
    21.             var mx:int = stage.mouseX;
    22.             var my:int = stage.mouseY;
    23.  
    24.             if(my> 0 && my <stage.stageHeight && mx> 0 && mx <stage.stageWidth) {
    25.                 // YOUR CODE HERE
    26.             }
    27.         }
    28.     }
    29. }

    This demo has been tested and confirmed working on:

    WINDOWS VISTA

    • Internet Explorer 7.0.6001 (16549)
    • Firefox 2.0.0.6 (with mouse gestures disabled)
    • Maxthon 2 (with mouse gestures disabled)
    • Safari 3.0.3 (522.15.5)

    Windows XP SP2

    • Internet Explorer 6
    • Internet Explorer 7
    • Maxthon 2 (with mouse gestures disabled)
    • FireFox 2 (with mouse gestures disabled)
    • Safari 3
    • Netscape 8

    Mac OSX 10.4.10 (Intel)

    • Firefox 2
    • Safari 3.0.3

    Thank you all for testing!

    Opera will not work, the browser forces the context menu to appear and blocks mouse events by default.

    If you manage to get the demo working, please post the OS and Browser build number in the comments. Please also leave a comment if you experience any problems with the demo.

    Google Code Project

    DEMO

    DIGG THIS

    .

  • 相关阅读:
    asp.net 浏览器下载文件的四种方式
    微信网页认证asp.net core返回MP_verify_CuUky7CS6gvKrMSe.txt
    qrcode length overflow (1632>1056)qrcode.js使用过程中二维码长度溢出解决办法
    layui的文档位置
    在64位Win2008 Server 系统上部署SSIS包遇到的问题及解决办法
    SQL SERVER 2008 中收缩数据库日志
    Reporting Service 按需隐藏导出类型
    SQL语句统计数据库中数据 有心人
    NC27 集合的所有子集(一)
    java虚拟机
  • 原文地址:https://www.cnblogs.com/chinatefl/p/1229054.html
Copyright © 2011-2022 走看看