Games [XUI2D] Single Screen Tile Map Example / Hello World With Background

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2018-09-27_17.09.08.png


This example shows how to use X2TileMap to create a single screen game with tiles based background. This means that the background is never scrolled.

To avoid drawing issues we want that the size of each tile will be a whole number and that each tile will be square shaped.

This is done with:
B4X:
Dim TileSize As Int = Min(X2.MainBC.mWidth / TileMap.TilesPerRow, X2.MainBC.mHeight / TileMap.TilesPerColumn)
TileMap.SetSingleTileDimensionsInBCPixels(TileSize, TileSize)
And:
B4X:
Private Sub SetWorldCenter
   'The map size will not be identical to the screen size. This happens because the tile size in (bc) pixels needs to be a whole number.
   'So we need to update the world center and move the map to the center.
   X2.UpdateWorldCenter(TileMap.MapAABB.Center)
End Sub

Drawing a tiles map is done with:
B4X:
Public Sub Tick (GS As X2GameStep)
   TileMap.DrawScreen(Array("Tile Layer 1"), GS.DrawingTasks)
End Sub

Public Sub DrawingComplete
   TileMap.DrawingComplete
End Sub

The example is included in the examples pack.
 
Top