当我按下按钮时,如何在舞台上移动动画片段?

时间:2013-11-13 18:16:20

标签: actionscript-3 movieclip keyboard-events

我再一次,正在为一个班级做一个项目,并且在根据我按下按钮时如何让我的棒人(movieclip)向左或向右移动时遇到一些麻烦。目前我所拥有的东西让棍子男人玩“走路”#34;按下箭头键时动画,但我也希望他也动作!这就是我所拥有的。

我建议在按下按钮时使用计时器事件触发,并使用该功能取代他,但我在编码时遇到了一些麻烦。

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
import flash.events.TimerEvent;

stop();

var isReady:Boolean=new Boolean();

isReady = true;

var timerWalkLeft:Timer = new Timer(100);

function walkLeft(e:TimerEvent)
{
stickMan.x ++;
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyIsDown);

function keyIsDown(event:KeyboardEvent)
{

if (isReady==false)
{
    return;
}
else
{
    var key:uint = event.keyCode;
    switch (key)
    {
        case Keyboard.LEFT :
            stickMan.gotoAndStop("left");
            walkLeft();
            break;
        case Keyboard.RIGHT :
            stickMan.gotoAndStop("right");
            break;
        case Keyboard.UP :
            stickMan.gotoAndStop("jump")
            break;
        case Keyboard.DOWN :
            stickMan.gotoAndStop("crouch");
            break;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用一些动画库而不是处理计时器,它更容易,请看这里: http://www.greensock.com/tweenlite/

答案 1 :(得分:0)

这是我的方式:

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
import flash.events.TimerEvent;

stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyIsDown);
var isReady:Boolean = true;// learn shortcuts like that, I don't know your useless teacher but "= new Boolean();" is the same thing as "= false;" or even simpler ";" (false being the default value for boolean type)

var timerWalk:Timer = new Timer(100);
timerWalk.addEventListener(TimerEvent.TIMER, movementLoop);
timerWalk.start();

function movementLoop(e:TimerEvent = null)
{
    var mc:* = stickMan;
    if(dir == "left")
       mc.x--;
    else if(dir == "right")
       mc.x++;
}


function keyIsDown(event:KeyboardEvent)
{
    if(!isReady)// literally "if not isReady"
       return;

    var key:uint = event.keyCode;
    switch (key)
    {
        case Keyboard.LEFT :
            stickMan.gotoAndStop("left");
            dir = "left";
            break;
        case Keyboard.RIGHT :
            stickMan.gotoAndStop("right");
            dir = "right";
            break;
    }
}