Jumping progressively on a platformer[SOLVED]


(billy2000) #1

The player is already jumping, but i want it to jump progressively like this: Each time he jumps he start to go up fast, but when i release the “w” key he should just begin falling. I just cant get it working properly, to start jumping fast ,then to decrease the jumping speed,but also to begin falling when the key was released. My code for jumping looks like this:

private var jumpPower:Number=8;
private var hFriction:Number=0.85;
private var gravity:Number = 0.3;

//jump
        if (collide("solid",x,y+1)) {
            onTheGround=true;
            ySpeed = 0;
            if (Input.check(Key.W)) {
                ySpeed-=jumpPower;
            }
        } else {
			
            ySpeed += gravity;
			
        }
ySpeed *= vFriction;

Any tips/ideas/bits of code to help? ty >.<


(Bora Kasap) #2

i think you just need to seperate jump function from fall function like this

      if (Input.check(Key.W)) {
                    ySpeed-=jumpPower;
                   onTheGround=false;
}
    
     if (collide("solid",x,y+1)) {
                onTheGround=true;
                ySpeed = 0;
                
            } else {
                ySpeed += gravity;
    
            }
    ySpeed *= vFriction;

(billy2000) #3

i tried to do that :smile: But when i use this code:

      if (Input.check(Key.W)) {
                ySpeed-=jumpPower;
               onTheGround=false;
}

 if (collide("solid",x,y+1)) {
            onTheGround=true;
            ySpeed = 0;

        } else {
            ySpeed += gravity;

        }
ySpeed *= vFriction;

it dosent jump at all,i think cuz its always on the ground , and when i use this:

 if (collide("solid",x,y+1)) {
            onTheGround=true;
            ySpeed = 0;

        } else {
            ySpeed += gravity;

        }
 if (Input.check(Key.W)) {
                ySpeed-=jumpPower;
               onTheGround=false;

}

ySpeed *= vFriction;

it dosent stop jumping at all. He just keeps going up xD.


(Bora Kasap) #4

have you tried changing “y+1” to only “y” ?


(Bora Kasap) #5

also, where are your player’s movement codes?

ySpeed += gravity;

thats not enough?

you probably need something like that

ySpeed += gravity;
y += ySpeed;

but you’re going to take much problems for further feautures like falling down without jumping from a cliff with this stuff


(billy2000) #6

pretty much this is the code i use :

public function movement():void
	{
		var pressed:Boolean = false;
		
		//for running
		if (Input.pressed(Key.A)) {
			if(pressedLT==0)
			pressedLT = 15;
			
			if (pressedLT != 15 && pressedLT != 0)
			runing = true;
			
		}
		if (Input.pressed(Key.D)) {
			if(pressedRT==0)
			pressedRT = 15;
			
			if (pressedRT != 15 && pressedRT != 0)
			runing = true;
		}
		
		//move l r
        if (Input.check(Key.A)) {
			if(runing)
            xSpeed = -SpeedR;
			else
			xSpeed = -SpeedW;
			
            pressed=true;
        }
		else
        if (Input.check(Key.D)) {
			if(runing)
            xSpeed = SpeedR;
			else
			xSpeed = SpeedW;
			
            pressed=true;
        }
		
		//jump
		
		if (collide("solid",x,y+1)) {
        onTheGround=true;
        ySpeed = 0;
        if (Input.check(Key.W)) {
            ySpeed-=jumpPower;
        }
        } else {
        ySpeed += gravity;
        }

        
        
		
		//running timer and stop running here
		if (pressedLT > 0)
		pressedLT--;
		if (pressedRT > 0)
		pressedRT--;
		if (!Input.check(Key.A) && !Input.check(Key.D) && xSpeed==0) {
			runing = false;
		}
		
        ySpeed *= vFriction;
		//if its running add firction
		if(runing){
		    if (Math.abs(xSpeed)<1&&! pressed) {
                xSpeed=0;
            }
           xSpeed *= hFriction;
		}
		else {
			if (! pressed) {
               xSpeed=0;
            }
		}
		
		moveBy(xSpeed,ySpeed,"solid");

		
		if (y > 500) {
			y = 0;
		}
	}
	override public function moveCollideX(e:Entity):Boolean {
        xSpeed = 0;
		return true;
    }
    override public function moveCollideY(e:Entity):Boolean {
        ySpeed = 0;
		return true;
    }

and i case its needed the variables:

private var jumpPower:Number=8;
    private var hFriction:Number=0.85;
    private var vFriction:Number=0.99;
    private var ySpeed:Number=0;
    private var onTheGround:Boolean=false;
    private var gravity:Number = 0.3;

the 1st code its just that it instantly makes onTheGround variable false after is made true. Or at last thats what i think.


(billy2000) #7

Ah didnt put it @_@ in my code i gave. Yeah i use moveby at the end of the function


(Bora Kasap) #8

oh man, i think you should check for some tutorials, that’s so hard to go on like that…


(billy2000) #9

i tried to make the player fall down after i released the key like this:

        if (Input.released(Key.W)) {
			ySpeed = 1;
		}

But the fall its just too sudden on tapping jump key. I tried that after i saw what zachwlewis said on here:

Am i implementing it wrong? >.<


(billy2000) #10

oh…i got it it was 0 not 1 -.-" so the code looks like this now:

if (Input.released(Key.W)) {
		ySpeed = 0;
	}

but i still got a little problem,after i release w it does a little slow mo,im trying to solve this now.


(Stop dreaming, Start living.) #11

Also make sure to use FP.elapsed with your speed functions.

Example

ySpeed += gravity * FP.elapsed;

Where gravity would be 100 for instance. If it is 100 it means it would move 100 pixel per second. This is important because if you do not use it your speeds can change depening on how long a single frame lasts and it can look choppy and not smooth.


(billy2000) #12

Ah ty …well usually on this situation i use FP.stage.frameRate=60 and it just fix my frame rate on 60 fps As for jumping almost got it very smoothly ,still have 1 more detail i have to take care, but this tomorrow cuz here almost 1 am >.<. And ima give the code on here tomorrow too xD


(billy2000) #13

Ok everything its flawless:) I made it so it can jump progressively , but also to stay a bit in the air after it finished jumping and begins to fall. The code its a bit messy right now cuz didn’t had time to clear it up but this is how it looks:

public function movement():void
	{
		var pressed:Boolean = false;
		
		
		//jump
		
		if (airTime > 0 && !onTheGround && !onceAir) { 
			airTime--;
			if(airTime==0){
			    onceAir = true;
				closetoZero = false;
			}
		}
		
		if (collide("solid", x, y + 1)) {
            onTheGround=true;
            ySpeed = 0;
            if (Input.check(Key.W)) {
				onceAir = false;
                ySpeed -= jumpPower;
            }
        } else {
			
			 if (Input.check(Key.W)) {
				pressedCounter++;
            }
			
			onTheGround = false;
			
			if(airTime==0)
            ySpeed += gravity;
        }
		
		if (ySpeed > -2 && ySpeed<0 && !onTheGround && !onceAir && airTime==0) {
			airTime = 6;
			ySpeed = -0.01;
			//closetoZero = true;
		}
		
	    if (Input.released(Key.W) && ySpeed < 0 && ySpeed > -3) {
			ySpeed = -0.05;//-1
			airTime = 5;
		}
		if (Input.released(Key.W) &&  ySpeed < -3) {
			ySpeed = -1;//-1
			airTime = 5;
		}
		
		
		
		if(airTime==0)
		ySpeed *= vFriction;
		
		if (ySpeed == 0){
		    onTheGround = true;
			pressedCounter = 0;
			
		}
		
		//for running
		if (Input.pressed(Key.A)) {
			if(pressedLT==0 && onTheGround)
			pressedLT = 15;
			
			if (pressedLT != 15 && pressedLT != 0)
			runing = true;
			
		}
		if (Input.pressed(Key.D)) {
			if(pressedRT==0 && onTheGround)
			pressedRT = 15;
			
			if (pressedRT != 15 && pressedRT != 0)
			runing = true;
		}
		
		//move l r
        if (Input.check(Key.A)) {
			if(runing)
            xSpeed = -SpeedR;
			else
			xSpeed = -SpeedW;
			
            pressed=true;
        }
		else
        if (Input.check(Key.D)) {
			if(runing)
            xSpeed = SpeedR;
			else
			xSpeed = SpeedW;
			
            pressed=true;
        }
		
		
        
		
		//running timer and stop running here
		if (pressedLT > 0)
		pressedLT--;
		if (pressedRT > 0)
		pressedRT--;
		if (!Input.check(Key.A) && !Input.check(Key.D) && xSpeed==0) {
			runing = false;
		}
		
		//if its running add firction
		if(runing){
		    if (Math.abs(xSpeed)<1&&! pressed) {
                xSpeed=0;
            }
           xSpeed *= hFriction;
		}
		else {
			if (! pressed) {
               xSpeed=0;
            }
		}
		
		moveBy(xSpeed, ySpeed, "solid");

		
		if (y > 500) {
			y = 0;
		}
	}
	override public function moveCollideX(e:Entity):Boolean {
        xSpeed = 0;
		return true;
    }
    override public function moveCollideY(e:Entity):Boolean {
        ySpeed = 0;
		return true;
    }

Ty guys! :smile:


(Ultima2876) #14

This topic was automatically closed after 3 days. New replies are no longer allowed.