Search code examples
actionscript-3flashmouseevent

change keyboard event to mouse event as3


I am a new new person who learn action script 3.

i have problem when i convert keyboard event to mouse event when i moving a walking character.

when use the keyboard event i have no problem. this is my code

    import flash.ui.Keyboard;
    var speed:Number=2;
    stage.addEventListener(KeyboardEvent.KEY_DOWN, stikman);
    function stikman(e:KeyboardEvent)
     {
      if (e.keyCode==Keyboard.LEFT)
       {
        stik.x-=speed;
        stik.scaleX=-1;
        stik.stik2.play();
       }
      else if (e.keyCode==Keyboard.RIGHT)
       {
        stik.x+=speed;
        stik.scaleX=1;
        stik.stik2.play();
       }
      }

and then i try to change keyboard event to mouse event when moving character with button it should press click, click and click. i want to hold the click when moving the character and when mouse up the character stop. but i still don't know how. this my code when i try to change to mouse event

     var speed:Number=2;
     mundur.addEventListener(MouseEvent.MOUSE_DOWN, stikman);
     function stikman(e:MouseEvent)
      {
       stik.x-=speed;
       stik.scaleX=-1;
       stik.stik2.play();
      }
     maju.addEventListener(MouseEvent.CLICK, stikman2);
     function stikman2(e:MouseEvent)
      {
        stik.x+=speed;
        stik.scaleX=1;
        stik.stik2.play();
      }

Solution

  • Because keyboard produces KeyboardEvent.KEY_DOWN event repeatedly as long as key is pressed, while MouseEvent.CLICK as well as MouseEvent.MOUSE_DOWN are dispatched only once per user action.

    With mouse you need to change the logic.

    // Subscribe both buttons.
    ButtonRight.addEventListener(MouseEvent.MOUSE_DOWN, onButton);
    ButtonLeft.addEventListener(MouseEvent.MOUSE_DOWN, onButton);
    
    var currentSpeed:Number = 0;
    var isPlaying:Boolean = false;
    
    function onButton(e:MouseEvent):void
    {
        // Set up the directions and start the animation.
        switch (e.currentTarget)
        {
            case ButtonLeft:
                currentSpeed = -speed;
                stik.stik2.play();
                stik.scaleX = -1;
                break;
    
            case ButtonRight:
                currentSpeed = speed;
                stik.stik2.play();
                stik.scaleX = 1;
                break;
        }
    
        isPlaying = true;
    
        // Call repeatedly to move character.
        addEventListener(Event.ENTER_FRAME, onFrame);
    
        // Hook the MOUSE_UP even even if it is outside the button or even stage.
        stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
    }
    
    function onFrame(e:Even):void
    {
        // Move character by the designated offset each frame.
        stik.x += currentSpeed;
    
        if (!isPlaying)
        {
            // Stop at last frame.
            // if (stik.stik2.currentFrame == stik.stik2.totalFrames)
    
            // Stop at frame 1.
            if (stik.stik2.currentFrame == 1)
            {
                // Stop the animation.
                stik.stik2.stop();
    
                // Stop moving.
                removeEventListener(Event.ENTER_FRAME, onFrame);
            }
        }
    }
    
    function onUp(e:MouseEvent):void
    {
        // Indicate to stop when the animation ends.
        isPlaying = false;
    
        // Unhook the MOUSE_UP event.
        stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
    }