Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Move object for a certain time interval

New Here ,
Jan 10, 2014 Jan 10, 2014

Hello i am working on a tile based game, and currently i am trying to fix the movement. What i want to do is make my character move smoothly for a fixed amount of time. I might not be explaining my self very clearly but i wanna do something like this:

function keyPress(event:KeyboardEvent)

{

     if (event.keyCode == Keyboard.RIGHT)

     {

          Move character 25 pixels to the right smoothly.

     }

}

Hope i made understandable, any help is much appreciated

TOPICS
ActionScript
725
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 11, 2014 Jan 11, 2014

Here's one way to get around a key being held down, but it won't help if someone starts speed poking the key since you have a timed movement.  To get around that you'd have to have a listener for the Tween that provides a similar restriction/control...

var rightDown:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyRelease);

function keyPress(event:KeyboardEvent)
{
     if (event.keyCode == Keyboard.RIGHT)
     {
          if(!righ

...
Translate
LEGEND ,
Jan 10, 2014 Jan 10, 2014

There are a number of ways to do this. 

One would be to use the built-in Tween class (or a third party version of it like TweenLite) to move the object for the distance desired per some defined timeframe. 

Another way would be to start running a Timer and for each count of it you move the object some distance and continue until you reach the 25 pixels you intend. 

Another similar way would be to use ENTER_FRAME event processing to makes the steps until reaching the 25 pixel distance.

Another way would be to have timeline tween that is triggered by the key press and moves the object to its destination.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2014 Jan 11, 2014

Thanks for the help, i looked up the Tween class and i was able to create a tween which moved my character 25 pixels in the duration of 1 second. However i still cannot figure out an effecient way to create smooth tilebased movement. The problem is that when i hold the key down the tween is "spammed". How would i go about it if i were to make movement like in the nintendo pokemon games?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 11, 2014 Jan 11, 2014
LATEST

Here's one way to get around a key being held down, but it won't help if someone starts speed poking the key since you have a timed movement.  To get around that you'd have to have a listener for the Tween that provides a similar restriction/control...

var rightDown:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyRelease);

function keyPress(event:KeyboardEvent)
{
     if (event.keyCode == Keyboard.RIGHT)
     {
          if(!rightDown){ // only allow the tween to occur when the key is first pressed
               rightDown = true;
               // tween here

          }
     }
}

function keyRelease(event:KeyboardEvent)
{

     // reset the control when the key is released
     if (event.keyCode == Keyboard.RIGHT)
     {
          rightDown = false;
     }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines