Question

thanks for any help you can provide

I want a movieclip to move left or right with easing so I am using flash's tween .The code is below. Problem I am face is that when I am clicking left key it moves on once rather than to keep moving while i keep pressing the key and same with right key. Some help please? Thanks.

//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);              
                } 
        }
Was it helpful?

Solution

based on your answer serhatsezer I was able to fix the problem, thanks

The main problem I was doing was i was declaring the variables outside the function so it was not getting updated

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);
        }
    }

OTHER TIPS

I think you're trying to wrong way to do this. You have to control your pressed key at ENTER_FRAME listener. After that move them! But keep in mind you have to update variables in your function.

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;
    }
}

I hope this help. Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top