Jumping on a platformer[SOLVED]


(billy2000) #1

Hello guys. I’ve been looking on internet for awhile and i just cant get working in my head how does jumping on a platformer works. I understand how to make player jump,but what bothers me is how do i check if he hits a platform underneath him and not for example a wall, or a platform above him. Can you guys give me a code example you use or tell some tips about this? TY :smile:


(Bora Kasap) #2

so, you mean, walking, collisions and other stuff are done and ready for jumping right?


(billy2000) #3

hmm not rly ,1st i just want to understand how its done XD


(Bora Kasap) #4

First check this out:

then make a search in FP forum about “moveBy”, “entity type”, “collisions”, “grid collision” etc… you’ll find it out soon…

if you can’t, we’re here


(billy2000) #5

ty :smile: ill take a look at them


(Nate ) #6

This really can vary depending on how your game is set up. If you post some of your code I can attempt helping you.


(billy2000) #7

i didnt made any yet ,was informing and im trying to make one now >.<


(billy2000) #8

Making a platformer its harder then i expected O:


(Nate ) #9

Okay I tossed this together for you it isn’t done PERFECTLY but I think this is what you are looking for. This is how I would start any platformer project essentially. Please note that the movement code was from Zach Lewis! Shout out to Zach and his awesomeness!

Here is what I have: PlatformerExample.swf (88.4 KB)

Link to my source: https://www.dropbox.com/s/t7yju9qkf11qrw0/PlatformerExample.zip

Hope this helps!


Tutorial Requests?
(Stop dreaming, Start living.) #10
private function updateMovement():void 
{
	var movement:Point = new Point;
	if (Input.check(Key.LEFT)) movement.x--;
	if (Input.check(Key.RIGHT)) movement.x++;
	if (Input.check(Key.UP)) movement.y--;
	if (Input.check(Key.DOWN)) movement.y++;
	
	_velocity.x = PLAYER_SPEED * FP.elapsed * movement.x;
	_velocity.y = PLAYER_SPEED * FP.elapsed * movement.y;
}

private function updateCollision():void
{
	x += _velocity.x;
	if (collide("Level", x, y))
	{
		if (FP.sign(_velocity.x) > 0)
		{
			// Moving to the right
			_velocity.x  = 0;
			x = Math.floor((x + width) / Settings.tileSize) * Settings.tileSize - width;
		}
		else
		{
			// Moving to the left.
			_velocity.x = 0
			x = Math.floor(x / Settings.tileSize) * Settings.tileSize + Settings.tileSize;
		}
	}

	y += _velocity.y;
	if (collide("Level", x, y))
	{
		if (FP.sign(_velocity.y) > 0)
		{
			// Moving to the right
			_velocity.y  = 0;
			y = Math.floor((y + height) / Settings.tileSize) * Settings.tileSize - height;
		}
		else
		{
			// Moving to the left.
			_velocity.y = 0
			y = Math.floor(y / Settings.tileSize) * Settings.tileSize + Settings.tileSize;
		}
	}
}

The first function is just basic movement, the second one is what I use for my grid/tile based collision. I started learning AS3 and FlashPunk about 2 days ago and already making huge progress.

The collision works as follows:

If collide with a grid/tile that is solid on map ‘level’ (ogmo map, look into it) then the collide if statement gets activated.

Then it checks the direction you were moving in (two if statements for x axis and y axis) and then it will execute some code depending on the direction.

After that it will will make your player stop by setting the velocity to 0.

Then it will move the player on either axis to a new position depending on how many pixels it has move through the wall/block/tile/grid.

That is basically it, if you do not reposition the player it may be 1 or 2 pixels in the wall/block/tile/grid and I do not like that because I am a perfectionist haha.

Have fun.

This is my first post on this forum.


(Ultima2876) #11

And what a first post it is :slight_smile: Welcome!


(billy2000) #12

Wow i just woke up and saw this awesome comments.Ty guys ima get right on to code now.:smiley:


(billy2000) #13

Ty guys so much for help. I also looked on some more tuts on internet and this is what i got :smile:

public function movement():void
	{
		var pressed:Boolean=false;
        if (Input.check(Key.A)) {
            xSpeed-=power;
            pressed=true;
        }
        if (Input.check(Key.D)) {
            xSpeed+=power;
            pressed=true;
        }
        if (collide("solid",x,y+1)) {
            onTheGround=true;
            ySpeed=0;
            if (Input.check(Key.W)) {
                ySpeed-=jumpPower;
            }
        } else {
            ySpeed+=gravity;
        }
        if (Math.abs(xSpeed)<1&&! pressed) {
            xSpeed=0;
        }
        xSpeed*=hFriction;
        ySpeed*=vFriction;
        adjustXPosition();
        adjustYPosition();
		
		if (y > 500) {
			y = 0;
		}
	}
	private function adjustXPosition():void {
        for (var i:int=0; i<Math.abs(xSpeed); i++) {
            if (! collide("solid",x+FP.sign(xSpeed),y)) {
                x+=FP.sign(xSpeed);
            } else {
                xSpeed=0;
                break;
            }
        }
    }
    private function adjustYPosition():void {
        for (var i:int=0; i<Math.abs(ySpeed); i++) {
            if (! collide("solid",x,y+FP.sign(ySpeed))) {
                y+=FP.sign(ySpeed);
            } else {
                ySpeed=0;
                break;
            }
        }
    }

So far it works like a charm :smile:


(Nate ) #14

No problem billy! Not sure if you used my code or played the demo I made but it looks like you got it working! Glad to have helped!


(billy2000) #15

You guys are always here when i need help XD. This helps me learning a lot and i am very grateful ^^.


(billy2000) #16

Ah 1 more thing. I finally understood moveBy also …so it should be like this :smile:

public function movement():void
{
	var pressed:Boolean=false;
    if (Input.check(Key.A)) {
        xSpeed-=power;
        pressed=true;
    }
    if (Input.check(Key.D)) {
        xSpeed+=power;
        pressed=true;
    }
    if (collide("solid",x,y+1)) {
        onTheGround=true;
        ySpeed=0;
        if (Input.check(Key.W)) {
            ySpeed-=jumpPower;
        }
    } else {
        ySpeed+=gravity;
    }
    if (Math.abs(xSpeed)<1&&! pressed) {
        xSpeed=0;
    }
    xSpeed*=hFriction;
    ySpeed*=vFriction;
    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;
    }