PushButton Tutorial Series – Tracking & Jumping

Google+ Pinterest LinkedIn Tumblr +

PLAY THE DEMO

DOWNLOAD THE SOURCE CODE

RETURN TO THE TUTORIAL INDEX

Commonly in games you want the players character (or car, space ship etc) to always be visible in the centre of the screen. Pushbutton makes this very simple, with the Scene2DComponent (created in our scene entity) having a property called trackObject. By setting trackObject to a component that extends the IDrawable2D interface, like the SpriteRenderComponent component our player entity uses to draw a sprite to the screen, the entity that contains that component will automatically be viewed in the centre of the screen as it moves around.

Implementing this takes only a few lines of code in the EntityFactory createPlayer function.

EntityFactory.as

static public function createPlayer(name:String, scene:String):IEntity
{
 // ...
 
 var sceneRenderer:Scene2DComponent = 
  NameManager.instance.lookupComponentByName(
    scene, SCENE_RENDERER_COMPONENT) as Scene2DComponent;    
  
 sceneRenderer.trackObject = Render;
 
 // ...
}

In order to make the player more interesting to watch we will give it the ability to jump. To this point the collision detection system has taken care of adjusting the velocity of the player in response to a collision. When the player hits a platform floor the vertical velocity is set to 0 to stop it falling further, and when it hits a wall the horizontal velocity is set to 0 to stop the player running.

In practice adding the ability to jump is quite simple: when the player is standing on the ground and the jump key is pressed we simply set the vertical velocity to a predetermined jump speed. But knowing when the player is standing on the ground requires being informed of the collisions in the system that previously occurred in the background.

The collision system, much like the animation system, uses ActionScript events to notify components that a collision has occurred. Since the KeyboardController will ultimately be responsible for deciding when the player will jump, it will be the KeyboardController that will listen for collision events. The onAdd and onRemove functions, which are part of the EntityComponent class that all PushButton components extend, are where we add and remove a component from the event system.

KeyboardController.as

protected override function onAdd():void
{
 super.onAdd();
 
 owner.eventDispatcher.addEventListener(CollisionEvent.COLLISION_EVENT, OnCollision);
 owner.eventDispatcher.addEventListener(CollisionEvent.COLLISION_STOPPED_EVENT, OnCollisionEnd);
}

protected override function onRemove():void
{
 super.onRemove();
 
 owner.eventDispatcher.removeEventListener(CollisionEvent.COLLISION_EVENT, OnCollision);
 owner.eventDispatcher.removeEventListener(CollisionEvent.COLLISION_STOPPED_EVENT, OnCollisionEnd);
}

The OnCollision function is triggered when the owner of the KeyboardController component (the player entity in our case) has been involved in a collision.

private function OnCollision(event:CollisionEvent):void
{

We make sure that the collision is with a “Platform”. In future the player could also collide with enemies, bullets, powerups etc, but none of these collisions are relevant to the KeyboardController.

 if (ObjectTypeManager.instance.doesTypeOverlap(event.collidee.collisionType, "Platform"))
 {

Then we check to see if the collision normal, which you can think of as a line pointing out perpendicular to the platform, has a y component of at least 0.7. This indicates that the platform is not too steep to stand on.

  if (event.normal.y > 0.7)

If it is not too steep, we increment the onGround variable to indicate that the player is standing on a platform.

   onGround++;
 }
}

The OnCollisionEnd function does the reverse, decrementing the onGround variable when the player is no longer colliding with a platform.

private function OnCollisionEnd(event:CollisionEvent):void
{
 if (ObjectTypeManager.instance.doesTypeOverlap(event.collidee.collisionType, "Platform"))
 {
  if (event.normal.y > 0.7)
   onGround--;
 }
}

The onGround variable now indicates whether the player is standing on the ground. In the onTick function we check to see if the jump key is pressed and onGround is greater than zero. If so the players vertical velocity modified to make it jump.

public override function onTick(tickRate:Number):void
{
 // ...
 
 if (InputManager.isKeyDown(InputKey.UP) && onGround > 0)
 {
  velocity.y = -JumpSpeed;
 } 
 
 // ...
}
Share.

About Author

Leave A Reply