Creating a Flash platform game with Flixel and Flex – Squishing the player

Google+ Pinterest LinkedIn Tumblr +

PLAY THE DEMO

DOWNLOAD THE SOURCE CODE

TUTORIAL INDEX

There were two limitations with the moving blocks in the last tutorial. The first was that the blocks would slide underneath the player. This can be easily fixed with one line of code in the MovingBlock collision function.

override public function collide(Core:FlxCore):void
{   
 // ..
 
 if (contactFromTop)
 {
  if (Core.hitCeiling(this))
  {
   Core.y = this.y - Core.height;
   Core.last.y = Core.y;
   Core.x += this.x - this.last.x;
  } 
 }
}

Here we move the player horizontally by the same amount as the block when the player has collided with the block from above (i.e. it is standing on it).

The second limitation was that the player would be transported through the blocks in situations where it should have been squished. To fix that up we need to perform collision detection on the static and moving blocks individually.

First we define a new FlxArray in the GameState class to hold the moving blocks.

protected var movingBlocks:FlxArray = new FlxArray();

All the instances of the MovingBlock class are then placed into the new array

this.movingBlocks.add(
 this.add
 (
  new MovingBlock(
   32, 
   64, 
   0, 
   0,
   LEVEL_DIMENSIONS>>1, 
   LEVEL_DIMENSIONS - BLOCK_DIMENSIONS * 3, 
   BLOCK_DIMENSIONS * 2, 
   BLOCK_DIMENSIONS * 2, 
   TechTilesImage
  )
 )
);

In the update function we do collisions on the moving blocks, and then the static ones. Note that the order of the calls to the FlxG collideArray function are important: the moving blocks have to be processed before the static blocks. We then check to see if the player is embedded in any of the blocks. If it is it has been squashed, and we call the playerSquash function.

public override function update():void
{
 super.update();
 
 FlxG.collideArray(movingBlocks, player);
    FlxG.collideArray(levelBlocks, player);
 FlxG.collideArrays(playerBullets, levelBlocks);
 FlxG.collideArrays(enemies, levelBlocks);   
 FlxG.overlapArrays(playerBullets,enemies,bulletHitEnemy);
 
 FlxG.overlapArray(movingBlocks, player, playerSquash);
    FlxG.overlapArray(levelBlocks, player, playerSquash);    
}

In the playerSquash function we simply kill the player. For an actual game you would probably remove a players life and restart the state in this function.

private function playerSquash(Collide1:FlxCore, Collide2:FlxCore):void
{
 player.kill();
}

Share.

About Author

Leave A Reply