Jump to content

Jumping a sprite in a Box2D Platformer


Recommended Posts

So I am just working on a demo for a game that I still be starting soon. I just found out about Box2D today and I was wondering if anyone knows how I would jump a sprite.


Currently, I can press the up key and I can infinitely soar into the sky. I have gravity working fine. I have seen people use a "jump timer". I understand the concept, but do not understand how to code this up. Also, I have failed at all attempts to check in my "man" is touching the ground. What is the best way to do this?


Here is my update code:

		function update() {		    man.body.setZeroVelocity();		    man.body.fixedRotation = true;		    if (cursors.left.isDown)		    {		    	man.scale.x = -.2;		        man.body.moveLeft(400);		    	man.animations.play('run', 10, true);		    }		    else if (cursors.right.isDown)		    {		    	man.scale.x = .2;		        man.body.moveRight(400);		    	man.animations.play('run', 10, true);		    }		    if (cursors.up.isDown)		    {		    	        		man.body.velocity.y = -750;		    }		    else if (cursors.down.isDown)		    {		        man.body.moveDown(400);		    }		    if (!cursors.left.isDown && !cursors.right.isDown) {		    	man.animations.stop();		    	man.frameName = "shooting1.png";		    };		}

And Here is what it looks like:




Link to comment
Share on other sites

This is my collision listener so far, you can play with it. It tells you what 2 bodies collided. In my code, it ignores any collision with a body that doesn't have a userData set. userData is set in the bodyDef, box2d doesn't care what it is: object, number, string, etc.  I'm currently assigning it a string ex "player" or "death" <- object that instantly kills player. I don't assign one to solid ground, because there's currently no need for me to do anything when my player touches solid ground 'cause box2d takes care of what I need.


var listener = new b2d.b2ContactListener;//console.log(world);    listener.BeginContact = function(contact) {	var a = contact.GetFixtureA().GetBody().GetUserData()    	var b = contact.GetFixtureB().GetBody().GetUserData();    	//if we have assigned userData to both bodies, it's important enough to check into    	if (a &&  {    		console.log("Collision between: " + a + " & " + ;    	}	             }    listener.EndContact = function(contact) {        //console.log(contact.GetFixtureA().GetBody().GetUserData());    }    listener.PostSolve = function(contact, impulse) {            }    listener.PreSolve = function(contact, oldManifold) {    }

Also, there's a b2Body.isAwake() function that will return false if the b2Body is "resting", but I haven't gotten into that yet.  Much like yourself, I have just started playing with box2d.


Sorry if this reply seems too vague for your answer, I'm currently on my lunch break :)


edit: If you want to see if the player touched anything at all, put this in listener.BeginContact.

if (a == 'player' || b == 'player') {    //do what ever you need there}
Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...