Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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;
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now