VG:Platformer Engine

From MyProjectFun
Jump to: navigation, search

This is an area for discussing the platformer engine. We'll begin with the base engine and how it is used.

I've attached v2.2 of the Platforming Engine - which is the base Level 2 engine with some platforming capabilities. To use the platforming, attach a state machine to your player that uses the InAir state as its initial state.

Anything you want to land on must be named "Platform" and be on a different display list than the player. That's because the player needs to collide with platforms.

That's all it takes to get platforms working! Extensions to this are coming soon.

Q: I want to collide with sprites named "Platform" and sprites named "Block" also. How do I do this?

If you open up PlatformStates.cs, at the top of the Update function for the OnPlatform state, there's a small bit about finding out which platform we landed on. We want to extend that to include sprites with other names (let's say "Block" for example):

// If we're not on a platform, fall
if (!(Helpers.LandsOn(This.Sprite, This.Sprite.CollisionWithSprite("Platform" )) || Helpers.LandsOn(This.Sprite, This.Sprite.CollisionWithSprite("Block" ))))
{
  this.m_StateMachine.CurrentState = new InAir(this.m_StateMachine);
}
else
{
  // Find out which Platform we've landed on
  Sprite Platform = null;
  if (Helpers.LandsOn(This.Sprite, This.Sprite.CollisionWithSprite("Platform" )))
    Platform = This.Sprite.CollisionWithSprite("Platform" );
 
  // If we have no platform, do we have a block?
  if (Platform == null && Helpers.LandsOn(This.Sprite, This.Sprite.CollisionWithSprite("Block" )))
    Platform = This.Sprite.CollisionWithSprite("Block" );

...

That takes care of the OnPlatform state. Now for the InAir! Again, at the top of the Update function, we have a bit to change:

// If we're on a platform or block, stop falling
if (Helpers.LandsOn(This.Sprite, This.Sprite.CollisionWithSprite("Platform" )) || Helpers.LandsOn(This.Sprite,
This.Sprite.CollisionWithSprite("Platform" )))
{
  this.m_StateMachine.CurrentState = new OnPlatform(this.m_StateMachine);
}

That should do it!

Q: I want my player to do flips in his jumping animation, but when I made the animation, he fell through the platform. My jump animation is not as tall as my stand animation. What gives?

If your jump animation is not as tall as your idle animation, then the character will land, change animation to the taller idle, see that his feet are now below the platform, and then fall some more. What we need to do to fix this is modify our OnEnter function of the OnPlatform state. Just replace it with the following version:

public override void OnEnter(WorldObject Owner)
{
  // Get our jump animation's height
  float height = (Owner as Sprite)GetHeight();
 
  // Change to an Idle animation
  (Owner as Sprite).Animation = 0;
 
  // Subtract our idle animation's height from the jump's height
  height -= (Owner as Sprite).GetHeight();
 
  // Move the player so his feet touch the ground
  (Owner as Sprite).Position.Y += height;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Engineering
Game Design
Multimedia Production
VG Programming
Downloads
Toolbox