0

我正在尝试开发一个游戏,我希望我的角色在我点击一个按钮时运行,如果我按住按钮继续运行。我是 ActionScript 3 的新手,所以我有点迷路了。

我找到了满足我要求的代码;但它使用箭头键,如下所示:

function moveRunKei() {
    if (Key.isDown(Key.RIGHT)) {
        dx = 15; //speed
        runKei._xscale = 50;
    } else if (Key.isDown(Key.LEFT)) {
        dx = -15;
        runKei._xscale = -50;
    } else {
        dx = 0;
    }

    runKei._x += dx;

    if (runKei._x < 100) runKei._x = 100; //30
    if (runKei._x > 550) runKei._x = 550;

    if (dx != 0 && runKei._currentframe == 1) {
        runKei.gotoAndPlay("run");
    } else if (dx == 0 && runKei._currentframe != 1) {
        runKei.gotoAndStop("stand");
    }
}

this.onEnterFrame = function(){
    moveRunKei();
}

我需要能够使用按钮来做到这一点。

///////////////////////////////////////// //////////////////////////////

import flash.events.Event;

var mouseDown:Boolean;
var speed:Number=4;
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);


function onMouseDown(event:MouseEvent):void
{
  mouseDown = true;
}

function onMouseUp(event:MouseEvent):void
{
  mouseDown = false;
}

function onEnterFrame(event:Event):void
{
  if (mouseDown)
  {
        runKei.gotoAndPlay("Run");
    runKei.x += speed;

  }

}

当我按住按钮时,这段代码能够让我的角色连续移动,但它在移动时没有动画(角色冻结,直到我释放按钮) - 我不知道如何解释它。

4

1 回答 1

0

您需要在每个按钮上添加鼠标按下和鼠标向上的事件侦听器以进行移动。然后有布尔值来跟踪按钮是否关闭。

值得一提的是,您链接的代码似乎是 actionscript2,所以我已将其更改为与 as3 一起使用

var leftDown:Boolean = false;
var rightDown:Boolean = true;

leftButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
rightButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
leftButton.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
rightButton.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
leftButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp)
rightButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp)

function onMouseDown(e:MouseEvent):void
{
    //since you can't click on two things at once, this is fine.
    rightDown = (e.target == rightButton);
    leftDown = (e.target == rightButton);
}

function onMouseDown(e:MouseEvent):void
{
    //since you can't click on two things at once, this is fine.
    rightDown = (e.target == rightButton);
    leftDown = (e.target == leftButton);
}

function moveRunKei()
{
    if (rightDown) {
        dx = 15; //speed
        runKei.scaleX = -0.5;
    } else if (leftDown) {
        dx = -15;
        runKei.scaleX = -0.5;
    } else {
        dx = 0;
    }

    runKei.x += dx;

    if (runKei.x < 100) runKei.x = 100; //30
    if (runKei.x > 550) runKei.x = 550;

    if (dx != 0 && runKei.currentFrame == 1)
    {
        runKei.gotoAndPlay("run");
    }
    else if (dx == 0 && runKei.currentFrame != 1)
    {
        runKei.gotoAndStop("stand");
    }
}

    this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void
{
    moveRunKei();
}
于 2013-07-24T09:18:56.020 回答