View Full Version : [AS3] Stuck with particular time-based hero's movement
Hi all!
I can't think/google out solution for next task:
1. player presses arrow
2. character walks constant number of pixels for constant amount of time
3. character stops (or if arrow is held, takes next step)
For example, character steps 10 pixels per 1 second.
Common time-based movement uses abstract speed:
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
if (down) {
character.y+=characterSpeed*timePassed*0.001;
}
mb Tween or Timer can help...
Thanks for any hints.
TomMalufe
09-29-2009, 07:03 PM
I would use a timer and a boolean toggle probably. The keyboard event KEY_DOWN sets the arrowToggle:Boolean to true and then the timer event handler can do step 2 like the code you have. when KEY_UP you set arrowToggle back to false and the walking stops.
I would use a timer and a boolean toggle probably. The keyboard event KEY_DOWN sets the arrowToggle:Boolean to true and then the timer event handler can do step 2 like the code you have. when KEY_UP you set arrowToggle back to false and the walking stops.
I already did that, your arrowToggle = my down.
But problem exactly with:
character.y+=characterSpeed*timePassed*0.001;
because it controls speed's factor based only on time, not real velocity (px/sec).
newblack
09-29-2009, 09:07 PM
px/sec is speed. whatever characterSpeed is defined as is how many pixels per second this method will move your character. which seems to be what you're trying to do.
you're not describing your problem well enough. is it a logic issue? what isn't working?
newblack
I can't find the way to move character for n pixels, no more, no less.
Per one second.
Game is top-down tile-based and character must pass 1/2 of tile for one step.
It's also real-time, so i need time-based movement.
Excuse me for bad English.
newblack
09-29-2009, 10:51 PM
you make position a function of time. X(t) = X(0) + vt. X is position, which is a function of t and v is your velocity vector. you clamp t to the range 0 <= t <= 1.
so in your update function, t = ( now - start ) / duration; if t > 1.0, t = 1.0. which means v is just the vector defined as X(1) - X(0). because you want duration to be 1.0 second, and you want your character to move 10 pixels, pressing to the right would make your v = (10,0), down v = (0,10) etc.
this is what any tweening engine will do for you, where you're tweening between X(0) and X(1).
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.