Platformer collision in FlashPunk


(Kevin Graham) #1

This is a tutorial from the old flashpunk forums, all credit goes to user dabrorius from that site. I by no means take any credit.


This is a good tutorial for platformer collison, which can be found here:

link


Alternative to Tilemap
(fedyfausto) #2

i use moveBy function :slight_smile: set the type of solids, if you wanna collide with other entities, use the Collide(xoffset, yoffset, type of entity)


(Jacob Albano) #3

MoveBy is the best thing ever.


(rye761) #4

Could you please expand on this with a code sample?


(DelishusCake) #5
public class Player extends Entity 
{
	public function Player(x:Number, y:Number) 
	{
		anim = new Spritemap(R.GFX_MAN, 16, 16);
		anim.add("idle", [0]);
		anim.add("walk", [1, 2, 3, 2, 1, 4, 5, 6, 5, 4], 12);
		anim.add("jump", [7, 8, 9], 10, false);
		anim.add("fall", [10, 11], 10, false);
		
		super(x, y, anim);
		setHitbox(6, 16, -5);
		
		phys = new Physics();
		phys.maxVelocity = new Point(100, 200);
		phys.drag.x = phys.maxVelocity.x * 4;
		phys.acceleration.y = phys.maxVelocity.y * 4;
	}
	override public function update():void 
	{
		phys.acceleration.x = 0;
		
		if (Input.check(Key.LEFT))
		{
			phys.acceleration.x = -phys.drag.x;
			anim.flipped = true;
		}
		else if (Input.check(Key.RIGHT))
		{
			phys.acceleration.x = phys.drag.x;
			anim.flipped = false;
		}
		if (jump >= 0 && Input.check(Key.SPACE))
		{
			jump += FP.elapsed;
			if (jump > 0.18) jump = -1;
		}else jump = -1;
		if (jump > 0) phys.velocity.y = -(phys.maxVelocity.y * 8) * jump;
		
		phys.update();
		
		if (collide("solid", x, y + 3) == null)
		{
			if (phys.velocity.y > 0) anim.play("fall");
			else anim.play("jump");
		}
		else if (phys.velocity.x != 0) anim.play("walk");
		else anim.play("idle");
		
		moveBy(phys.x, phys.y, "solid");
	}
	override public function moveCollideX(e:Entity):Boolean 
	{
		if (!collide("solid", x + FP.sign(phys.velocity.x), y - 1))
		{
			y --;
			return false;
		}
		phys.velocity.x = 0;
		return true;
	}
	override public function moveCollideY(e:Entity):Boolean
	{
		if (e.type == "solid")
		{
			if (collide("solid", x, y + 1)) jump = 0;
		}
		phys.velocity.y = 0;
		return true;
	}
	private var jump:Number = 0;
	private var anim:Spritemap;
	private var phys:Physics;
}

This is how I do it. It even allows for sloped tiles (In the form of pixelmasks), and variable height jumping (think mario)! Physics is just a tween with a FlashPunk-ified version of the flixel physics.