【问题标题】:AS3 changing KeyboardEvents to MouseEventsAS3 将键盘事件更改为鼠标事件
【发布时间】:2015-02-08 14:08:05
【问题描述】:

我正在使用 Adob​​e Flash CS6 学习 AS3,我正在编写一个使用 KeyboardEvents 创建带有动画的移动对象的教程,它运行良好,但现在我想更改它,以便我可以使用屏幕 (MouseEvents) 来移动角色,但我不知道该怎么做。

var movingRight;
var movingLeft;

function onKeyPress(e:KeyboardEvent):void
{
    if(e.keyCode == Keyboard.RIGHT){
        movingRight=1;
    }
    else if(e.keyCode == Keyboard.LEFT){
        movingLeft=1;
    }
}

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    要使用按钮移动character,您必须使用两个MouseEvents:按下按钮时的MouseEvent.MOUSE_DOWN事件和释放按钮时的MouseEvent.MOUSE_UP,所以你可以这样做:

    LeftBTN.addEventListener(MouseEvent.MOUSE_DOWN, LeftBTN_onPress);
    LeftBTN.addEventListener(MouseEvent.MOUSE_UP, LeftBTN_onRelease);
    function LeftBTN_onPress(e:MouseEvent):void
    {
        movingLeft = 1;
    }
    function LeftBTN_onRelease(e:MouseEvent):void
    {
        character.gotoAndStop(1);
        movingLeft = 0;
    }
    
    RightBTN.addEventListener(MouseEvent.MOUSE_DOWN, RightBTN_onPress);
    RightBTN.addEventListener(MouseEvent.MOUSE_UP, RightBTN_onRelease);
    function RightBTN_onPress(e:MouseEvent):void
    {
        movingRight = 1;
    }
    function RightBTN_onRelease(e:MouseEvent):void
    {
        character.gotoAndStop(4);
        movingRight = 0;
    }
    

    您也可以像这样为所有事件侦听器使用一个函数:

    stage.addEventListener(KeyboardEvent.KEY_DOWN, move_character);
    stage.addEventListener(KeyboardEvent.KEY_UP, move_character);
    LeftBTN.addEventListener(MouseEvent.MOUSE_DOWN, move_character);
    LeftBTN.addEventListener(MouseEvent.MOUSE_UP, move_character);
    RightBTN.addEventListener(MouseEvent.MOUSE_DOWN, move_character);
    RightBTN.addEventListener(MouseEvent.MOUSE_UP, move_character);
    
    function move_character(e:*){
    
        var event_type:String = e.type;                     // get event type : to identify which kind of event is fired
        var current_target:String = e.currentTarget.name;   // get target name : to identify the target of the fired event
    
        switch (event_type){
    
            case MouseEvent.MOUSE_DOWN :
    
                if(current_target == 'LeftBTN') movingLeft = 1;
                else movingRight = 1;
    
                break;
    
            case MouseEvent.MOUSE_UP:
    
                if(current_target == 'LeftBTN'){
                    character.gotoAndStop(1);
                    movingLeft = 0;
                } else {
                    character.gotoAndStop(4);
                    movingRight = 0;
                }           
                break;      
    
            case KeyboardEvent.KEY_DOWN:
    
                if (e.keyCode == Keyboard.RIGHT){           
                    movingRight = 1;            
                } else if (e.keyCode == Keyboard.LEFT) {            
                    movingLeft = 1;         
                }           
                break;
    
            case KeyboardEvent.KEY_UP:
    
                if (e.keyCode == Keyboard.LEFT) {           
                    character.gotoAndStop(1);
                    movingLeft = 0;         
                } else if (e.keyCode == Keyboard.RIGHT) {           
                    character.gotoAndStop(4);
                    movingRight = 0;            
                }
                break;
        }
    
    }
    

    或者,使用e 参数作为Event

    function move_character(e:Event){
    
        var event_type:String = e.type;                     // get event type : to identify which kind of event is fired
        var current_target:String = e.currentTarget.name;   // get target name : to identify the target of the fired event
        var key_code:Number;
    
        switch (event_type){
    
            case MouseEvent.MOUSE_DOWN :
    
                if(current_target == 'LeftBTN') movingLeft = 1;
                else movingRight = 1;
    
                break;
    
            case MouseEvent.MOUSE_UP:
    
                if(current_target == 'LeftBTN'){
                    character.gotoAndStop(1);
                    movingLeft = 0;
                } else {
                    character.gotoAndStop(4);
                    movingRight = 0;
                }           
                break;      
    
            case KeyboardEvent.KEY_DOWN:
    
                key_code = KeyboardEvent(e).keyCode;
    
                if (key_code == Keyboard.RIGHT){     
                    movingRight = 1;            
                } else if (key_code == Keyboard.LEFT) {            
                    movingLeft = 1;         
                }           
                break;
    
            case KeyboardEvent.KEY_UP:
    
                key_code = KeyboardEvent(e).keyCode;
    
                if (key_code == Keyboard.LEFT) {           
                    character.gotoAndStop(1);
                    movingLeft = 0;         
                } else if (key_code == Keyboard.RIGHT) {           
                    character.gotoAndStop(4);
                    movingRight = 0;            
                }
                break;
        }
    
    }
    

    希望能有所帮助。

    【讨论】:

    • 投了反对票。该方法作为无类型参数,这是不好的做法。既然所有事件都从 Event 继承,为什么不将参数输入为 Event?
    • @BotMaster 我同意你的观点,但我尽量做到简单,因为 OP 在 AS3 的世界中是新的。我将编辑我的答案以添加它。
    【解决方案2】:

    在舞台上放两个按钮,命名为 btnRight 和 btnLeft,然后使用这段代码

    btnLeft.addEventListener(MouseEvent.CLICK, onButtonClick, false, 0, true);
    btnRight.addEventListener(MouseEvent.CLICK, onButtonClick, false, 0, true);
    
    function onButtonClick(evt:MouseEvent) {
        if (evt.target == btnLeft) movingLeft = 1;
        else movingRight = 1;
    }
    

    我还建议只使用一个名为movingDirection 的变量,它可以是1、0 或-1 :)

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 2011-10-25
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多