Please bear with me here!
Code: Select all
TileMap = {}
TileMapMT = { __index = TileMap } --This metatable will be attached to every created class instance.
TileMap.Tiles = {}
for i = 1,2 do
TileMap.Tiles[i] = love.graphics.newImage("Tiles/Tile"..i..".png")
end
TileMap.Map = {}
function TileMap:Create()
local NewInstance =
{
MapWidth = 20,
MapHeight = 20,
MapX = 0,
MapY = 0,
MapOffsetX = 30,
MapOffsetY = 30,
MapDisplayWidth = 14,
MapDisplayHeight = 10,
TileWidth = 32,
TileHeight = 32
} --The new instance
setmetatable(NewInstance, TileMapMT)
return NewInstance
end
function TileMap:Draw()
for y=1, MapDisplayHeight do
for x=1, MapDisplayWidth do
love.graphics.draw(
Tiles[Map[y+MapY][x+MapX]],
(x*TileWidth)+MapOffsetX,
(y*TileHeight)+MapOffsetY)
end
end
end
Specifically, the error I'm getting is Tilemap.lua:31:'for' limit must be a number.
I thought about declaring my variables outside of the Create() function, but then how would I make them a part of a new instance?
Edit: I looked here, and it seems to suggest using self (similar to 'this' in C++). I tried doing it:
Code: Select all
function TileMap:Draw()
for y=1, self.MapDisplayHeight do
for x=1, self.MapDisplayWidth do
love.graphics.draw(
self.Tiles[self.Map[y+self.MapY][x+self.MapX]],
(x*self.TileWidth)+self.MapOffsetX,
(y*self.TileHeight)+self.MapOffsetY)
end
end
end