【问题标题】:Move animated movieClips using buttons instead of arrow keys使用按钮而不是箭头键移动动画影片剪辑
【发布时间】:2013-07-24 07:37:50
【问题描述】:

我正在尝试开发一款游戏,我希望我的角色在我单击按钮时运行,并且在我按住按钮时继续运行。我是 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();
}

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

/////////////////////////////////////// /////////////////////////////////////////p>

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;

  }

}

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

【问题讨论】:

    标签: android actionscript-3 flash animation


    【解决方案1】:

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

    值得一提的是,您链接的代码似乎是 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();
    }
    

    【讨论】:

    • 嗨。谢谢回复。我尝试了编码,但收到错误消息 1120:未定义属性 dx 的访问。这是什么意思?
    • 您在其中多次使用名为 dx 的变量。那是你的delta x,这就是他应该移动的速度。该错误意味着您尚未声明它。只需将“var dx:number”(减去语音标记)贴在顶部,靠近 leftDown 和 rightDown
    • 我把代码放上去,错误消失了。但是当我运行 SWF 文件时,我的角色会立即跑过舞台,而无需我点击按钮。
    • 确保使用 false 启动 mouseDown。你需要在你的按钮上做 addEventListeners,而不是在父剪辑上,这就是你现在正在做的事情
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多