private void checkLevelTransition() { Vector2 centerCell = TileMap.GetCellByPixel(WorldCenter); if (TileMap.CellCodeValue(centerCell).StartsWith("T_")) { string[] code = TileMap.CellCodeValue(centerCell). Split('_'), if (code.Length != 4) return; LevelManager.LoadLevel(int.Parse(code[1])); WorldLocation = new Vector2( int.Parse(code[2]) * TileMap.TileWidth, int.Parse(code[3]) * TileMap.TileHeight); LevelManager.RespawnLocation = WorldLocation; velocity = Vector2.Zero; } }
Update()
method of the Player class to add a check for pressing the Up key or pressing Up on the gamepad to check the current square for an available transition. Place this after the check for pressing the Space bar or A button to jump:if (keyState.IsKeyDown(Keys.Up) || gamePad.ThumbSticks.Left.Y > 0.3f) { checkLevelTransition(); }
The checkLevelTransition()
method examines the code value in the cell containing the center of the player's sprite, looking for an entry starting with T_
that indicates a transition square. If found, the text is split into pieces separated by an underscore using the Split()
method. If the result is not four pieces (T_, the map number, an X
coordinate, and a Y
Coordinate), the method simply returns and does nothing.
If the code is in the proper format, the LevelManager is asked to load the desired map. After the map has been loaded (which may contain START
codes that position the player) the player is moved to the position indicated by the transition code value.
18.217.108.11