Skip to main content

Map

A 2D grid of sprites. Can be used to create tile-based games.

Usually represents the level or map of a game. Drawn using the map editor.

Creation

MapObj

Construct a new instance of an empty map.

map = MapObj(width, height, spriteSheet)
ParameterTypeDefaultNote
widthnumber⚠️ requiredin cells.
heightnumber⚠️ requiredin cells.
spriteSheetSpriteSheet⚠️ required
ReturnTypeNote
mapMap

TileMap

A globally available instance for the standard tiles map of any game.

It contains the sprites drawn in the map editor.

TileMap:draw(); --draws the map on the screen.
caution

Do not destroy this object or render it unusable. As it's used by he map function.

Doing so would render it also unusable.

Methods

map:map

Transform the tile map by applying a function to every cell.

map:map(cellFunction, x, y, width, height)
ParameterTypeDefaultNote
cellFunctionCellFunction⚠️ required
xnumber0in cells.
ynumber0in cells.
widthnumbermap widthin cells.
heightnumbermap widthin cells.

CellFunction

function cellFunction(cellX, cellY, spriteId)
--Logic to modify the spriteId.
return spriteId;
end
ParameterTypeNote
cellXnumber
cellYnumber
spriteIdnumber
ReturnTypeNote
spriteIdnumber, nilnil to leave unmodified.

map:cell

Get the id of a sprite for a cell

spriteId = map:cell(cellX, cellY)
ParameterTypeDefaultNote
cellXnumber⚠️ required
cellYnumber⚠️ required
ReturnTypeNote
spriteIdnumber

Set the id of a sprite for a cell

map = map:cell(cellX, cellY, spriteId)
ParameterTypeDefaultNote
cellXnumber⚠️ required
cellYnumber⚠️ required
spriteIdnumber⚠️ required
ReturnTypeNote
mapMapthe object itself. allows to do chain calls.

map:cut

Create a new map out of a region from this map.

resultMap = map:cut(x, y, width, height) 
ParameterTypeDefaultNote
xnumber0in cells.
ynumber0in cells.
widthnumbermap widthin cells.
heightnumbermap heightin cells.
ReturnTypeNote
resultMapMap

map:size

Get the dimensions of the map.

width, height = map:size()
ReturnTypeNote
widthnumberin cells.
heightnumberin cells.

map:width

Get the width of the map.

width = map:width()
ReturnTypeNote
widthnumberin cells.

map:height

Get the height of the map.

height = map:height()
ReturnTypeNote
heightnumberin cells.

map:draw

Draw the map or a section of the map on the visible screen.

map = map:draw(destinationX, destinationY, sourceX, sourceY, sourceWidth, sourceHeight, scaleX, scaleY, spriteSheet)
ParameterTypeDefaultNote
destinationXnumber0in pixels.
destinationYnumber0in pixels.
sourceXnumber0in cells.
sourceYnumber0in cells.
sourceWidthnumbermap widthin cells.
sourceHeightnumbermap heightin cells.
spriteSheetSpriteSheetSpriteMap
ReturnTypeNote
mapMapthe object itself. allows to do chain calls.

map:export

Export the tiles map data into a .lk12 raw string.

data = map:export()
ReturnTypeNote
datastringa .lk12 raw string.

map:import

Import the tiles map data into a .lk12 raw string.

map = map:import(data)
ParameterTypeDefaultNote
datastring⚠️ requireda .lk12 raw string.
ReturnTypeNote
mapMapthe object itself. allows to do chain calls.

map:spritebatch

Enable the use of a spritebatch for the rendering of the map. Which brings a huge performance boost.

map:spritebatch(usage)
ParameterTypeDefaultNote
usagestring'static'enum: 'dynamic', 'static', 'stream'.