Cannot display images from a table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Echo13243
Prole
Posts: 2
Joined: Fri Mar 10, 2017 1:07 am

Cannot display images from a table

Post 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 ^^
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Cannot display images from a table

Post 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.
User avatar
Nikki
Citizen
Posts: 84
Joined: Wed Jan 25, 2017 5:42 pm

Re: Cannot display images from a table

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests