Page 1 of 1

Cannot display images from a table

Posted: Fri Mar 10, 2017 1:01 pm
by Echo13243
I'm trying to make a 15 tile puzzle game as a way to practice what I've learned about Love2D so far... it's not working out well

Code: Select all

require("load")

function love.load()

tile = {}
loc = {}

for i = 1, 15 do --This is where I got desperate, you can ignore it
  tile[i] = {img = nil}
end
for i = 1, 15 do
  tile[i].img = love.graphics.newImage("assets/tile"..i..".png")
end
for i = 1, 16 do
  loc[i] = {x = nil, y = nil}
end

LoadLoc()

background = {
  x = 0,
  y = 0,
  img = love.graphics.newImage("assets/background.png")
}

end

function love.update(dt)
--love.mouse.getX() love.mouse.getY() love.mouse.isDown(1)

end

function love.draw()
  love.graphics.draw(tile1.img, loc1.x, loc1.x, 0, 1, 1, 0, 0)

  love.graphics.draw(background.img, background.x, background.y, 0, 1, 1, 0, 0)

end
tile1 will not display, saying it's attempting to index global value 'tile1' (a nil value). I don't see why the value would be nil.
Sorry if this is a really easy fix, I've been trying for half an hour to no avail and any help will be very appreciated ^^

Re: Cannot display images from a table

Posted: Fri Mar 10, 2017 1:09 pm
by MasterLee
You initialize your image coords to nil, then it is never changed.
You use loc1.x and tile1.x instead of loc[1].img and tile[1].x.
Also you use the x coordinate also for the tile y location.

Re: Cannot display images from a table

Posted: Fri Mar 10, 2017 1:13 pm
by Nikki
in this code I see here there is no variable that is named tile1
thats why its nil.

there is a variable named tile, and its used as an array so the first one is reachable with tile[1]

same goes with the loc1 that ought to be loc[1]


so this line

Code: Select all

love.graphics.draw(tile1.img, loc1.x, loc1.x, 0, 1, 1, 0, 0)
should become something like

Code: Select all

love.graphics.draw(tile[1].img, loc[1].x, loc[1].x, 0, 1, 1, 0, 0)