Time for action – handling codes

  1. Add the checkCurrentCellCode() helper method to the LevelManager class:
    #region Helper Methods
    private static void checkCurrentCellCode()
    {
        string code = TileMap.CellCodeValue(
            TileMap.GetCellByPixel(player.WorldCenter));
    
        if (code == "DEAD")
        {
            player.Kill();
        }
    }
    #endregion
  2. Call checkCurrentCellCode() as the first task performed inside the if (!player.Dead) statement in the Update() method of the LevelManager class:
    checkCurrentCellCode();
  3. Launch the game and run to the right until you reach an area where there are floor blocks missing, forming a pit. Jump into the pit, and your character will die when he reaches the lowest level of the pit.

What just happened?

We simply read the code value from the map for the cell ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.