error i am getting now
main.lua:20: attempt to index global 'player' (a nil value)
traceback
main.lua:20: in function 'update'
[C]: in function 'xpcall'
here is my new code :
main.lua
Code: Select all
equire("map.lua")
require("player.lua")
function love.load()
map = Map.load()
player = Player.load()
end
function love.keypressed(key)
if state.keypressed then state.keypressed(key) end
end
function love.keyreleased(key)
if state.keyreleased then state.keyreleased(key) end
end
function love.update(dt)
map:update(dt)
player:update(dt)
end
function love.draw()
map:draw()
player:draw()
end
Code: Select all
Map = {}
Map.__index = Map
function Map.load()
local temp = {}
setmetatable(temp, Map)
temp.TileW, temp.TileH = 32, 32
temp.Tileset = love.graphics.newImage('tiles.png')
temp.tilesetW, temp.tilesetH = temp.Tileset:getWidth(), temp.Tileset:getHeight()
Quads = {
love.graphics.newQuad(0, 0, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 1 = brown
love.graphics.newQuad(32, 0, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 2 = grass
love.graphics.newQuad(0, 32, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 3 = halfbridge
love.graphics.newQuad(32, 32, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH) -- 4 = black
}
TileTable = {
{ 2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,2 },
{ 1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1 },
{ 2,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2 }
}
return temp
end
function Map.update(dt)
end
function Map.draw()
for rowIndex=1, #TileTable do
local row = TileTable[rowIndex]
for columnIndex=1, #row do
local number = row[columnIndex]
local x = (columnIndex-1)*temp.TileW
local y = (rowIndex-1)*temp.TileH
love.graphics.drawq(temp.Tileset, Quads[number], x, y)
end
end
end
Code: Select all
Player = {}
Player.__index = Player
function Player.load()
--First we initialize all the state images that we need
images = {
up = love.graphics.newImage("tankup.png"),
down = love.graphics.newImage("tankdown.png"),
left = love.graphics.newImage("tankleft.png"),
idle = love.graphics.newImage("tankidle.png"),
right = love.graphics.newImage("tankright.png")
}
--And here's our player object, complete with a state variable and a variable that holds the current image.
tank = {
state = "idle",
image = images["tankidle.png"],
x = 500,
y = 300,
speed = 100,
hp = 100
}
end
function Player.update(dt)
--Now what we do is we check for a button to be pressed, and if it is we set the player state and move the player.
if love.keyboard.isDown("left") then
tank.state = "left"
tank.x = tank.x - tank.speed*dt
elseif love.keyboard.isDown("right") then
tank.state = "right"
tank.x = tank.x + tank.speed*dt
elseif love.keyboard.isDown("up") then
tank.state = "up"
tank.x = tank.x - tank.speed*dt
elseif love.keyboard.isDown("down") then
tank.state = "down"
tank.x = tank.x + tank.speed*dt
else
if love.keyboard.isDown(" ") then
tank.state ="idle"
end
end
--Now we update the player image with whatever image corresponds to the current state.
tank.image = images[Player.state]
end
function Player.draw()
--And finally we draw the player.
love.graphics.draw(tank.image, tank.x, tank.y)
end