用键盘快速转换补间

时间:2014-01-15 13:37:42

标签: actionscript-3 flash flash-cs5

感谢您提供的任何帮助

我想要一个动画片段向左或向右移动,因此我正在使用flash的补间。代码如下。我面临的问题是,当我点击左键时,它会继续移动一次,而不是继续移动,而我一直按下键,右键同样。有些帮助吗?感谢。

//variable declarations
var Currpos:Number = boat_mc.x ;
var xleft:Number = boat_mc.x - 40;
var xright:Number = boat_mc.x + 40;

        // move boat
        stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardClick);

        function onKeyboardClick (e:KeyboardEvent):void{
            if (e.keyCode == Keyboard.LEFT){
                var tweenleft:Tween = new Tween(boat_mc, "x", Regular.easeOut, Currpos, xleft, 2, true);
                } 

                if (e.keyCode == Keyboard.RIGHT){
                var tweenright:Tween = new Tween(boat_mc, "x", Regular.easeOut, Currpos, xright, 2, true);              
                } 
        }

2 个答案:

答案 0 :(得分:0)

我认为你正试图以错误的方式做到这一点。你必须在ENTER_FRAME监听器上控制你按下的键。之后移动它们!但请记住,您必须更新函数中的变量。

import fl.transitions.Tween;
import fl.transitions.easing.Regular;
import flash.events.Event;

var Currpos:Number = boat_mc.x;
var xleft:Number = boat_mc.x - 40;
var xright:Number = boat_mc.x + 40;
var isRightPressed:Boolean = false;
var isLeftPressed:Boolean = false;


// move boat
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardClick);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyboardUp);

function onKeyboardClick(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.LEFT)
    {
        isLeftPressed = true;
    }

    if (e.keyCode == Keyboard.RIGHT)
    {
        isRightPressed = true;
    }
}

function onKeyboardUp(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.LEFT)
    {
        isLeftPressed = false;
    }

    if (e.keyCode == Keyboard.RIGHT)
    {
        isRightPressed = false;
    }
}

stage.addEventListener(Event.ENTER_FRAME,loop);
var xSpeed:Number = 0.8;
function loop(event:Event):void
{
    if (isRightPressed)
    {
        boat_mc.x +=  xSpeed;
    }

    if (isLeftPressed)
    {
        boat_mc.x -=  xSpeed;
    }
}

我希望这有帮助。干杯!

答案 1 :(得分:0)

基于你的答案serhatsezer我能够解决问题,谢谢

我正在做的主要问题是我在函数外面声明了变量,所以它没有得到更新

import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextField;
    import flash.ui.Mouse;
    import flash.display.DisplayObjectContainer;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import mochi.as3.*;


    //variable declaration
    var isRightPressed:Boolean = false;
    var isLeftPressed:Boolean = false;
    var tweenleft:Tween;
    var tweenright:Tween;


    // move boat
    stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardClick);
    stage.addEventListener(KeyboardEvent.KEY_UP,onKeyboardUp);

    function onKeyboardClick(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.LEFT)
        {
            isLeftPressed = true;
        }

        if (e.keyCode == Keyboard.RIGHT)
        {
            isRightPressed = true;
        }
    }

    function onKeyboardUp(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.LEFT)
        {
            isLeftPressed = false;
        }

        if (e.keyCode == Keyboard.RIGHT)
        {
            isRightPressed = false;
        }
    }

    stage.addEventListener(Event.ENTER_FRAME,loop);

    function loop(event:Event):void
    {
    //variable declrations

    var Currpos:Number = boat_mc.x;
    var xleft:Number = boat_mc.x - 40;
    var xright:Number = boat_mc.x + 40;

        if (isRightPressed)
        {
            tweenright = new Tween(boat_mc, "x", Regular.easeOut, Currpos, xright, 2, true);                
            trace(boat_mc.x);
        }

        if (isLeftPressed)
        {
            tweenleft = new Tween(boat_mc, "x", Regular.easeOut, Currpos, xleft, 2, true);
            trace(boat_mc.x);
        }
    }