View Full Version : [AS3] accelerated movement
xdeath
09-16-2009, 03:31 AM
i'm trying to make accelerated movement but i'm failing horribly. i have the increase of speed but not the decrease. i hope someone can hlp fix this:
var down = false; // down button control
var up = false; // down button control
var speed:Number = 0;
var maxSpeed:Number = 10;
var acc:Number = 0.5;
p1.addEventListener(Event.ENTER_FRAME,moving);
function moving(evt:Event){
if (down){
p1.y+=speed;
if (speed<=maxSpeed){
speed+=acc;
}
if (speed>maxSpeed){
speed=maxSpeed;
}
}
if (up){
p1.y-=speed;
if (speed<=maxSpeed){
speed+=acc;
}
if (speed>maxSpeed){
speed=maxSpeed;
}
}
trace(speed);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,pres sed);
function pressed(evt:KeyboardEvent){
if(evt.keyCode == Keyboard.UP){
// increasing y value make it mov down.
up=true;
}
if(evt.keyCode == Keyboard.DOWN){
// increasing y value make it mov down.
down=true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP,releas ed);
function released(evt:KeyboardEvent){
if(evt.keyCode == Keyboard.UP){
// increasing y value make it mov down.
up=false;
}
if(evt.keyCode == Keyboard.DOWN){
// increasing y value make it mov down.
down=false;
}
}
David Lindh
09-16-2009, 10:44 AM
Im quite new to as but i think this is how to do it.
Add the minSpeed var and let the speed get negative, then add it to the y pos.
if (down){
p1.y+=speed;
if (speed<=maxSpeed){
speed+=acc;
}
if (speed>maxSpeed){
speed=maxSpeed;
}
}
if (up){
p1.y+=speed;
if (speed>=minSpeed){
speed-=acc;
}
if (speed<minSpeed){
speed=minSpeed;
}
}
xdeath
09-16-2009, 11:34 AM
you clearly don't understand my code. down was a variable which controled whether the don button was being pressed or not. and i take it you can understand the rest.
I'm not really sure if that's the most efficient way of programming something like that?
I probably would have just used -maxSpeed instead of minSpeed, but I don't know if that's the type of motion you want. All I know is the motion you want involves the speed increasing and decreasing, but you haven't specified under what circumstances you want it to decrease.
So here's an exercise. Draw a table, and make a column for each item whose value could change and for each action you want to undertake.
So, if this should be determined entirely by the values of down and up, you start with this:
D=down
U=up
DU
FF
TF
FT
TT
Then you write to the right of that what you want to happen in each of those circumstances.
Maybe you want it to consider what direction it's currently moving. Like, pushing down while moving up might have a different behaviour than pushing down while moving down. So add that as a column, and extend the table down so you have all possible combinations.
xdeath
09-17-2009, 01:22 AM
when the key is released it slows down. when you press a key it increases speed. i have the increase just not the decrease. and it should decrease speed twice as fast as the iincrease. so thats just "acc*2". i know what i want and i've made alot of this myself but i can't figure out how to get the last little bit working.
And what happens if you're moving up and you push down? Do you decelerate at acc*2, or at acc*3, because it's the regular deceleration and the moving down acceleration on top of that?
xdeath
09-18-2009, 05:20 AM
well i just want a noraml platform game kind of style accelerated movement system. i've been trying for so bloody long. its just irritating now.
i would say if both buttons are pressed it probably wouldn't go anywere at all. but in all honesty i made this trying to make it like a normal platform game style increase and crease speed. and nobody would help me at flashkit, and there was no examples so what else could i do?
so in conclusion i simply started guessing hoping i managed to get it right.
With the code you pasted, the value of speed was the same if you were moving up at a speed of 5 or moving down at a speed of 5, and there's no variables that record what direction it moved last frame. With David Lindh's code, negative speeds move up and positive speeds move down, so there is a simple way to determine what direction it's moving.
So, let's see:
If you press both buttons or neither button, then decelerate.
If you press up, then if you are moving up accelerate if you are moving down decelerate
If you press down, then if you are moving down accelerate if you are moving up decelerate
Does that roughly describe the behaviour you want?
Or to take it further:
if ((up AND down) OR (!up AND !down)) {
decelerate
} else
if (up) {
if (speed>0) {
decelerate
} else {
accelerate
}
} else {
if (speed<0) {
decelerate
} else {
accelerate
}
}
xdeath
09-19-2009, 12:48 AM
ideally yes. but i would change this part of the code:
if ((up AND down) OR (!up AND !down)) {
decelerate
}
// would change it to this:
if (up AND down){
speed=0;
}
if(!up AND !down) {
decelerate // until speed hits zero
}
but with the no buttons being pressed isnt that the same thing as the else statement in your if statements on the buttons being pressed.
ideally yes. but i would change this part of the code:
if ((up AND down) OR (!up AND !down)) {
decelerate
}
// would change it to this:
if (up AND down){
speed=0;
}
if(!up AND !down) {
decelerate // until speed hits zero
}
If that's the behaviour you want, sure.
but with the no buttons being pressed isnt that the same thing as the else statement in your if statements on the buttons being pressed.
if ((up AND down) OR (!up AND !down)) {
decelerate
} else
if (up) {
if (speed>0) {
decelerate
} else {
accelerate
}
} else { //you mean this else?
// no, by this point we've eliminated three possibilities:
//(up AND down)
//(!up AND !down)
//(up)
//So the only thing left is (down)
if (speed<0) {
decelerate
} else {
accelerate
}
}
krayzeebean
09-24-2009, 07:46 AM
I recently worked on a game where the up/down keys scroll the background with acceleration/deceleration. This is the update function (called from an enter frame handler in the main class) and the addVelocity function. You'll also need a variable for the maximum speed and for friction, where 1 is no friction and 0 will stop immediately. I also use constants for DOWN and UP, they are just strings "down" and "up", the constants are so I get compile errors if I make a typo. The line if (mSpeed < 1) mSpeed = 0; prevents the background from scrolling up. If you want your object to be able to go up and down (rather than down being the same as hitting the brakes and coming to a stop) then you should remove that line.
public function update(pUp:Boolean, pDown:Boolean):void {
var newY:Number = y;
var vDown:Number = 0;
if (pUp) addVelocity(1.55, DOWN);
if (pDown) addVelocity(1.55, UP);
if (mSpeed < 1) mSpeed = 0;
vDown = Math.sin(Math.PI / 2) * mSpeed;
newY += vDown;
y = newY;
mSpeed *= mFriction;
}
public function addVelocity(pSpeed:Number, pDirection:String, pIgnoreMaxSpeed:Boolean = false):void {
if (pDirection == UP) pSpeed *= -1;
if (pIgnoreMaxSpeed) {
mSpeed += pSpeed;
return;
}
mSpeed = (mSpeed + pSpeed > mMaxSpeed) ? mMaxSpeed : mSpeed + pSpeed;
}
Darksydaz
09-24-2009, 02:59 PM
Wait... are you attempting to create a "fastfall" function?
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.