Time for action – handling codes
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
Call
checkCurrentCellCode()
as the first task performed inside theif (!player.Dead)
statement in theUpdate()
method of the LevelManager class:checkCurrentCellCode();
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 where the player's WorldCenter
point is located. This allows the player to get close to deadly things without actually dying, but one pixel too far will trigger the square.