Skip to content
Casey Baxter edited this page Oct 30, 2012 · 5 revisions

A custom layer is a layer that is user defined. It can be placed inside of a map's layerOrder table just like TileLayers and ObjectLayers. The name is merely an abstraction. Custom layers have no specific class structure.

To create a custom layer you should use Map.newCustomLayer(). You can give it an already made layer or it will return a new empty one. Additionally you may convert an ObjectLayer to a custom layer by using ObjectLayer.toCustomLayer().

When any layer is inserted into a map's layerOrder all callbacks triggered with map.callback() are then forwarded to it. The most common one being draw. Any callback can be triggered by the user.

Example:

-- Load the map
local ATL = require("AdvTiledLoader")
ATL.Loader.path = "maps/"
local map = ATL.Loader.load("desert.tmx")

-- Forward love callbacks
function love.update(dt) map:callback("update", dt) end
function love.draw() map:callback("draw") end

-- Create a rectangle
local rect = {x = 0, y = 100, size = 50, speed = 300, direction = 1}

-- Create a custom layer
local customLayer = map:newCustomLayer("MyCustomLayer")

-- Have the custom layer draw the rectangle
function customLayer:draw()
   love.graphics.rectangle("fill", rect.x, rect.y, rect.size, rect.size)
end

-- On update, move the rectangle and have it bounce off the edge of the screen.
function customLayer:update(dt)
  rect.x = rect.x + rect.speed * dt * rect.direction
  if rect.x < 0 then rect.direction = 1   if rect.x + rect.size > love.graphics.getWidth() then rect.direction = -1 end
end

Data

CustomLayer.class (default: "CustomLayer")
The class name

CustomLayer.map (default: parent map)
The map that the layer is contained in.

CustomLayer.name (default: nil)
The name of the layer.


###See Also Map