This is how I do those:
public class Hero extends Entity
{
public var gravity:Number = 800;
public var jumpForce:Number = -1600;
public var velocity:Point = new Point();
public var onFloor:Boolean = false;
// other stuff
// other stuff
// other stuff
override public function update():void
{
super.update();
// other stuff
/* HOW TO CHECK IF ON FLOOR */
onFloor = collide("floor", x, y + 1);
/*
This function checks for collisions.
It places the hero at coordinates (x, y+1), which is one pixel below where it currently is.
This way, if the hero is on top of a floor for example, and you move it 1 pixel down, it will collide with the floor.
If the hero is on the air, and you move it 1 pixel down, it will not collide with any floor.
This function returns the entity which was collided with or null if not found.
This function DOES NOT move the hero no matter if you put x+1000 and y+200.
After the collision check, the hero is moved back to its original position.
*/
/* WHEN THE JUMP BUTTON IS PRESSED */
if (onFloor) velocity.y = jumpForce;
/*
Check first if the hero is on a floor.
If so, jump.
*/
/* ADDING GRAVITY */
velocity.y += gravity * FP.elapsed;
/* MOVING THE HERO */
moveBy(velocity.x * FP.elapsed, velocity.y * FP.elapsed, ["floor", "wall"]);
/*
This function moves the hero by the specified amount in both x and y axes.
You can also pass in an array of the types of entities you want it to collide with.
This way, it will move the hero but stop when it collides with an entity of type "floor" or "wall" as in this example.
All entities have a variable called type. You can set it inside an entity's constructor:
type = "floor";
When the hero collides with the said entity, the moveCollide functions gets called. This will be discussed below.
*/
}
override public functions moveCollideY(entity:Entity):Boolean
{
velocity.y = 0;
/*
When the above moveBy() function detects a collision with "floor" or "wall" along the Y axis, this function gets called.
The entity passed is the entity which it collided with.
You want to set the velocity.y to 0 because when you are on the floor, you stop but your velocity would still increase due to gravity.
If your hero walks towards an edge and falls, his velocity would be so big he would just zap down so fast.
So set this to 0.
*/
return true;
/*
This statement just tells the engine if we should collide with the object or not.
If true, the normal collision thing you imagined would happen.
If false, the hero will pass through the floor but you will still get notified if the hero overlaps the floor.
So set this to true.
*/
}
override public functions moveCollideX(entity:Entity):Boolean
{
velocity.x = 0;
/*
Same as above.
*/
return true;
/*
Same as above.
*/
}
}