Hi there,
Your main problem is that you were going about this the wrong way. I'll show you what I mean by explaining my corrections.
Here is my version of your "runescreen.lua" file:
Code: Select all
runes = {}
runeImages = {}
runeImages["shadow"] = love.graphics.newImage("images/runes/shadow.png")
function runeCreate(x, y, id)
runes[#runes + 1] = {x=x, y=y, id=id}
end
function runeDraw()
for i,v in ipairs(runes) do
love.graphics.draw(runeImages[v.id], v.x, v.y)
end
end
function activaterune(key)
if key == "q" then
gamestate = "rune"
end
end
function deactivaterune(key)
if key == "q" then
gamestate = "play"
end
end
I also made a minor adjustment to your "love.load" callback in "main.lua":
Code: Select all
function love.load()
gamestate = "play"
--loading up teh runes
runeCreate(100, 100, "shadow")
end
The problem with your original code was two-fold. First, you were using "love.graphics.newImage" once every frame for each rune in your "runes" table, creating an endless amount of Image objects. Second of all, you were never actually drawing the images.
You see, "love.graphics.newImage" does not draw an image, instead it defines an Image object. It is not meant to be called over and over again. The function is usually used like this:
Code: Select all
image = love.graphics.newImage( filename )
--image is the variable which will contain the Image object, this is so that you can reference the Image object later
--filename most be a string and should be the filepath to the image file
[there are a few other ways that you can use the function (see:
https://love2d.org/wiki/love.graphics.newImage ), but they're not important right now.]
No matter how many times you draw an Image object, you only have to define it ONCE. So, you may have 20 "shadow runes" being drawn, but you only have to define the shadow rune image object once.
Now we need to draw the image object. We can do this a few different ways, but for now we will use "love.graphics.draw()". This function draws "drawable objects" onto the screen. A "drawable object" is usually an image object, but it can also be other things (see here if you're curious:
https://love2d.org/wiki/Drawable ).
Here is the function and its various arguments:
Code: Select all
love.graphics.draw( drawable object, x position, y position, rotation/orientation in radians, x-axis scale factor, y-axis scale factor, x-axis origin offset, y-axis origin offset, x-axis shearing factor, y-axis shearing factor )
So, let's say we define the shadow rune image like so:
Code: Select all
shadowRuneImage = love.graphics.newImage("images/runes/shadow.png")
That creates a new image object using the shadow rune image file, and then it "stores" this image object in the shadowRuneImage variable.
Now, to draw the shadowRuneImage at (100,100), we might use this code:
Code: Select all
function love.draw()
love.graphics.draw(shadowRuneImage, 100, 100)
end
Get it?
With all of that in mind, my correct code above should make a lot of sense. Let me explain a couple of things about it though...
First of all, I setup a table called "runeImages" to store all of your rune image objects. Each entry into this table should have a key referring to its type. For example:
Code: Select all
runeImages["shadow"] = love.graphics.newImage("images/runes/shadow.png")
If you wanted to add a fire rune image object to the table, you might write:
Code: Select all
runeImages["fire"] = love.graphics.newImage("images/runes/firerune.png")
This allows you to refer to these image objects later by referencing that entry in the table. So if you want to draw the shadow rune image at (100,100), you might write:
Code: Select all
function love.draw()
love.graphics.draw(runeImages["shadow"], 100, 100)
end
These leads to my version of your runeDraw() function:
Code: Select all
function runeDraw()
for i,v in ipairs(runes) do
love.graphics.draw(runeImages[v.id], v.x, v.y)
end
end
This now references the runeImages table to draw the appropriate rune image. This should be pretty self-explanatory, but you may notice another change I made - I got rid of the "img" key/column from your runes table, as well as removed any references to it in your functions. Why? Because, with the runeImages table in place, "id" now serves both its original purpose and the purpose of "img."
For example, take a look at my adjustment to your love.load callback (in main.lua) again:
Code: Select all
function love.load()
gamestate = "play"
--loading up teh runes
runeCreate(100, 100, "shadow")
end
Instead of writing:
Code: Select all
runeCreate(100, 100, ""images/runes/shadow.png", "shadow")
You just write:
Because the image object is already created and stored in the runeImages table, and the id that you write when calling your runeCreate function (in this case, "shadow") references that entry in the runeImages table (since the string you put as the id is the same).
So, if you had added a fire rune image object to the runeImages table like so:
Code: Select all
runeImages["fire"] = love.graphics.newImage("images/runes/firerune.png")
You could create a new fire rune like this:
Ok, and with that it should be working now.
Oh, I did make one other change in runescreen.lua: in the runeCreate() function, I changed...
Code: Select all
table.insert(runes, {x=x, y=y, id=id})
to:
Code: Select all
runes[#runes + 1] = {x=x, y=y, id=id}
This does almost the same thing (for your uses, it should have the exact same results), and is faster.
Ok, well hopefully that all made sense! Just in case you aren't able to get my changes above to work, I've attached my modified version of your project to this post.
Cheers!
-Ben
PS: If you want to add the "img" property back to the runes table (e.g. if there are cases where the id and the corresponding runeImages key are not the same), then feel free!