角色动作与按键相关联,随着角色速度的提高而提高动画速度

时间:2013-07-31 09:33:25

标签: actionscript-3 animation actionscript keypress

我遇到了一个棘手的问题(至少对我而言)我无法弄明白。我有一个简笔画,我想制作一个简单的运行动画,取决于他是向左还是向右移动,还要将动画速度与他移动的x方向的速度联系起来。

下面我提供了我的角色如何移动的代码(我的游戏中的所有不必要代码都被移除)。 xspeed就是我想要的动画速度。我可以简单地将它与xspeed的绝对值联系起来,因为它可能是负数。理想情况下,我想要有2个动画,1个用于向左移动,1个用于向右移动。我想在我的stickman1的同一时间线上制作两个动画,然后做这样的事情。

if (xspeed > 0){stickman1.gotoAndPlay(2)}
if (xspeed < 0){stickman1.gotoAndPlay(5)}

假设我向右移动的动画是3帧长,从第2帧开始,以4结束,我的动画向左移动也是3帧长,从第5帧开始,到第7帧结束,然后是第4帧和第7帧只是输入了一些代表gotoAndPlay(correct frame to repeat)的代码。尽管如此,我知道在时间轴上进行任何编码总是不好的做法,所以如果可能的话我想远离那个。然后它变得更糟。我不知道如何加速动画=(。这就是我所在的地方,非常感谢有关这些问题的帮助,我的角色运动的完整代码如下!谢谢!

public var gameTimer:Timer;
public var stickman1:Stickman1;
public var leftBool:Boolean = false;
public var rightBool:Boolean = false;
public var accel:Number = 0.5;
public var maxspeed:Number = 8;
public var xspeed:Number = 0;

public function gameScreen():void
{
    this.addEventListener(Event.ENTER_FRAME, addSomeListeners, false, 0, true);
    stickman1 = new Stickman1();
    stickman1.x = 250;
    stickman1.y = 300;
    addChild(stickman1);
    gameTimer.addEventListener(TimerEvent.TIMER, onTick, false, 0, true);
    gameTimer = new Timer(25);
    gameTimer.start();
}

public function addSomeListeners(event:Event):void
{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp, false, 0, true);
}

public function onTick(timerEvent.TimerEvent):void
{
    if(rightBool==true && xspeed<maxspeed){xspeed+=2}
    if(leftBool==true && xspeed>-maxspeed){xspeed-=2}
    if(xspeed>0){xspeed-=accel}
    if(xspeed<0){xspeed+=accel}

    stickman1.x+=xspeed;
    stickman1.y+=yspeed;
}

public function onKeyDown(keyboardEvent.KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT){leftBool = true};
    if (event.keyCode == Keyboard.RIGHT){rightBool = true};
}

public function onKeyUp(keyboardEvent.KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT){leftBool = false};
    if (event.keyCode == Keyboard.RIGHT){rightBool = false};
}

1 个答案:

答案 0 :(得分:0)

你需要管理你自己的动画系统,但它并不像听起来那么难:)

这样的事情应该有效(假设第0帧是站立的,帧1-n正在运行):

public var dir:int = 0; // direction you're going; -1 = left, 1 = right, 0 = not moving
public var speed:Number = 0.0; // the speed of your char
public var stickman:Stickman; // your stickman obj
public var prevTime:Number = 0.0; // used to calculate delta time in the update
public var currAnimTime:Number = 0.0; // our current animation time
public var currAnimFrame:int = 0; // our current animation frame

public function gameScreen():void
{
    // create our stickman
    this.stickman = new Stickman();
    this.stickman.gotoAndStop( 0 ); // stand frame
    …

    // add our listeners
    this.addEventListener( Event.ENTER_FRAME, this._onEnterFrame );
    this.addEventListener( KeyboardEvent.KEY_DOWN, this._onKeyDown );
    this.addEventListener( KeyboardEvent.KEY_UP, this._onKeyUp );

    this.prevTime = getTimer();
}

private function _onEnterFrame( e:Event ):void
{
    // calculate delta time
    var currTime:int = getTimer();
    var dt:Number   = ( currTime - this.prevTime ) * 0.001; // dt is in seconds
    this.prevTime   = currTime;

    // check if we're slowing down
    if( this.dir == 0 )
    {
        this.speed *= 0.8;
        if( this.speed <= 0.01 ) // or whatever value you want
            this.speed = 0.0;
    }
    else
    {
        // we're running
        this.speed += 20 * dt * this.dir; // or whatever speed increase you want
        if( this.speed > 20 )
            this.speed = 20; // max speed;
    }

    // if our speed is 0, just play the stand anim and return
    if( this.speed == 0 )
    {
        this.gotoAndStop( 0 );
        return;
    }

    // get our anim time - the seconds for a frame.
    // basically, if speed is 20, then our anim time will be
    // 1.0 (1 second) / 20.0 -> 0.05, or 0.05 seconds
    // per frame. We use seconds because dt and currAnimTime are
    // in seconds. NOTE: this is a linear transform of speed -> fps
    var animTime = 1.0 / this.speed;

    // update our current anim time
    this.currAnimTime += dt;
    while( this.currAnimTime > animTime )
    {
        // increase our current anim frame
        this.currAnimFrame++;
        if( this.currAnimFrame > this.totalFrames )
            this.currAnimFrame = 1; // frame 0 is our stand anim
    }

    // go to our new frame
    if( this.currAnimFrame != this.currFrame )
        this.gotoAndStop( this.currAnimFrame );

    // flip our scale based on if we're right or left
    this.scaleX = ( this.dir < 0 ) ? -1 : 1;
}

private function _onKeyDown( e:KeyboardEvent ):void
{
    // move left or right
    if( e.keyCode == Keyboard.LEFT )
        this.dir = -1;
    else if( e.keyCode == Keyboard.RIGHT )
        this.dir = 1;
}

private function _onKeyUp( e:KeyboardEvent ):void
{
    // stop
    if( e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT )
        this.dir = 0;
}

基本上,您只需更改显示每个帧的时间,并手动处理更改到右侧帧。这样,你的动画就可以加快速度并减慢你的速度。