2017-10-13 102 views
-1

我是谁学习动作脚本3.改变键盘事件,鼠标事件AS3

我有问题,当我的键盘事件转换为鼠标事件,当我移动行走人物新的新人。

使用键盘事件时我没有问题。这是我的代码

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(); 
     } 
     } 

,然后我尝试用按钮,它应该按点击,点击,点击移动角色时,键盘事件更改鼠标事件。我想要在移动角色时以及在将角色停止时进行点击。但我仍然不知道如何。当我尝试这是我的代码更改为鼠标事件

 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(); 
     } 

回答

2

因为键盘重复只要按下键产生KeyboardEvent.KEY_DOWN事件,而MouseEvent.CLICK以及的MouseEvent.MOUSE_DOWN是每个用户操作仅调度一次。

用鼠标你需要改变逻辑。

// 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); 
} 
+0

感谢您的回答,先生,其权利。但是当鼠标向上时,我希望角色仍然走路,然后停在第一个位置。你能再帮我一次吗? –

+0

@BagasPR然后不要退订Event.ENTER_FRAME,也不要停止动画。 – Organis

+0

我在先生之前尝试过,但我仍然无法做到这一点。我不明白你的意思,因为我对此仍然陌生:D。你能解释一下吗?当鼠标放下角色走路,并且当鼠标移动角色仍然走路直到帧结束,然后返回并停止到第一帧(第一个位置)。在此之前感谢 –