【问题标题】:Is it possible to put swf inside another swf to change the keyboard functions是否可以将 swf 放在另一个 swf 中以更改键盘功能
【发布时间】:2013-04-12 00:20:07
【问题描述】:

我有一个使用 adobe alchemy 制作的库运行某种模拟器的 swf 文件。这个 swf 运行游戏,我们可以使用键盘控制它们,我没有重新映射键盘上按钮的选项,所以我问是否可以将此 swf 放在另一个包含重新映射按钮的接口的 swf 中键盘?如果可能的话,这会影响模拟器的性能。 你能给我一个例子如何做这些事情吗?

【问题讨论】:

    标签: actionscript-3 flash apache-flex actionscript flash-builder


    【解决方案1】:

    我唯一能想到的就是这样的东西,它捕获一个标准的KeyboardEvent,然后调度一个新的KeyboardEvent,并重新映射一个keyCode 值。

    目前唯一的问题是每个媒体都会发送两个KeyboardEvents。第一个将是原始版本,后者将是重新映射的版本。

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
    
    function keyHandler(e:KeyboardEvent):void
    {
        if(!e.cancelable)
        {
            var map:Object = {
                65: 20,
                66: 13
            };
    
            // Set up you own event.
            // The new KeyboardEvent is cancelable, so we can track it as such.
            var kbd:KeyboardEvent = new KeyboardEvent(e.type, true, true);
            kbd.keyCode = e.keyCode;
    
            for(var i:String in map)
            {
                // Set the keyCode of the new KeyboardEvent to the mapped value
                // as defined above.
                if(e.keyCode === int(i)) kbd.keyCode = map[i];
            }
    
            stage.dispatchEvent(kbd);
        }
    
    
        // Notice that you will be notified twice of a KeyboardEvent; once for
        // the original and once for the new one with the remapped (if applicable)
        // keyCode value.
        trace(e.keyCode);
    }
    

    【讨论】:

    • 感谢您的回复,但您没有回答我的问题是否可能?
    • @Dora 好吧,是的,使用我回答中的代码很有可能。您无法重新映射已调度的默认键盘事件,但您可以执行我所做的并调度新的。如果游戏使用某种类型的库进行用户输入管理,可以利用新的 KeyboardEvent 模型来处理 keyCode 数据,那么您可以轻松做到这一点。不幸的是,我怀疑情况是否如此。
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    相关资源
    最近更新 更多