开发者

In CS5 Actionscript 3.0 the function Tween( ) is their away not to have a endpoint for the tween parameter

I have tried numerous different methods to make this run the way I want. I'm using arrow keys to movie a MovieCLip on the x-axis using only Right arrow and Left arrow while the MovieClip is running an animation during the motion I found out that the function Tween() would work the best. Here is my code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

function keyPressed(evt:KeyboardEvent):void
{
    if(evt.keyCode == Keyboard.RIGHT && var_move == false)
    {
        var_move = true;
        hero.gotoAndPlay(3);
        hero.scaleX = 1;
        var tween1:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x += 70, 15, false);
        tween1.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
    }

    if(evt.keyCode == Keyboard.LEFT && var_move == false)
    {
        var_move = true;
        hero.gotoAndPlay(3);
        hero.scaleX = -1;
        var tween2:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x -= 70, 15, false);
        tween2.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);

    }
}

function onMotionFinished($evt:TweenEvent):void
{
    hero.gotoAndPlay("Stand");
    var_move = false;

}

The code in question is:

var tween1:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x += 70, 15, false);

I was wondering do you need a endpoint for the Tween(). This is the only part this messing me up. I don't want the object to stop at set points. So if you just tab the either arrow button the object will continue to the point set for it. I'm trying to stop the on a dime, where ever I want.I did try the following code.

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);

function keyPressed(evt:KeyboardEvent):void
{
    if(evt.keyCode == Keyboard.RIGHT)
    {
        var_move = true;
        hero.gotoAndPlay("Walk");
        hero.scaleX = 1;
        hero.x += 5;
    }

    if(evt.keyCode == Keyboard.LEFT)
    {
        var_move = true;
        hero.gotoAndPlay("Walk");
        hero.scaleX = -1;
        hero.x -= 5;
    }
}

function releaseKey(evt:KeyboardEvent):void
{
    if(evt.keyCode == Keyboard.RIGHT || evt.keyCode == Keyboard.LEFT)
 开发者_StackOverflow中文版   {
        hero.gotoAndStop("Stand");
    }
}

This code works when it comes to stopping the animation on a dime, but there is a bad delay then the arrow key is first pressed. Almost like a easeIn, but it isn't. It's like a response delay with in ActionScript and the key press. Can anybody help me with either one of these issues? Thanks.


I don't really understand the question but I recommended looking into http://www.greensock.com/tweenmax/ for your animation needs.


You could try using the ENTER_FRAME event for your motion, and killing the listener when the key is released. Something like this (untested):

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);

var movement:int = 0;

function keyPressed(evt:KeyboardEvent):void
{
    if(evt.keyCode == Keyboard.RIGHT) {
        hero.gotoAndPlay("Walk");
        hero.scaleX = 1;
        movement = 5;
        stage.addEventListener(Event.ENTER_FRAME,doMovement);
    } else if(evt.keyCode == Keyboard.LEFT) {
        hero.gotoAndPlay("Walk");
        hero.scaleX = -1;
        movement = -5;
        stage.addEventListener(Event.ENTER_FRAME,doMovement);
    }
}

function releaseKey(evt:KeyboardEvent):void
{
    if(evt.keyCode == Keyboard.RIGHT || evt.keyCode == Keyboard.LEFT)
    {
        hero.gotoAndStop("Stand");
        stage.removeEventListener(Event.ENTER_FRAME,doMovement);
    }
}

function doMovement(evt:Event):void
{
    hero.x += movement;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜