How to use same image more than once
Posted: Sat Jul 06, 2019 10:37 pm
I have the following problem:
I created a table for blueprints of objects I want to use in my game (sapceships, etc). Every blueprint has an image. Once I need a new object I create it by loading the data of one of the blueprints into the table for objects.
When I assign the same blueprint (thus the same image) to more than one object only 1 of those objects is rendered. If I assign a different blueprint to it it renders normally.
Where it says "test" is where I created object[2] and object[3]. object[2].img = blueprint[2].img and object[3].img = blueprint[2].img is where I call the same image: img = love.graphics.newImage("images/Human-Spacestation.png").
how can I use the same image more than once without having this problem?
Here is my code:
I created a table for blueprints of objects I want to use in my game (sapceships, etc). Every blueprint has an image. Once I need a new object I create it by loading the data of one of the blueprints into the table for objects.
When I assign the same blueprint (thus the same image) to more than one object only 1 of those objects is rendered. If I assign a different blueprint to it it renders normally.
Where it says "test" is where I created object[2] and object[3]. object[2].img = blueprint[2].img and object[3].img = blueprint[2].img is where I call the same image: img = love.graphics.newImage("images/Human-Spacestation.png").
how can I use the same image more than once without having this problem?
Here is my code:
Code: Select all
-- Blueprints
blueprint = {
[1] = {name = "Spaceship",
img = love.graphics.newImage("images/Human-Battlecruiser.png"),
},
[2] = {name = "Spacestation",
img = love.graphics.newImage("images/Human-Spacestation.png"),
},
}
-- Objects
object = {
nextID = 1,
selection = {n = 1,
crosshairImg = love.graphics.newImage("images/crosshair026.png"),
print = true,
},
}
object[1] = blueprint[1]
object[1].img = blueprint[1].img
object[1].id = object.nextID
object[1].activePlayerChar = true
object[1].isOnMap = true
object[1].posX = map.centerPosX
object[1].posY = map.centerPosY
object.nextID = object.nextID + 1
--test
object[2] = blueprint[2]
object[2].img = blueprint[2].img
object[2].id = object.nextID
object[2].activePlayerChar = false
object[2].isOnMap = true
object[2].posX = map.gridStartPosX + 2*map.boxWidthAndHeight - map.centerBoxPosX
object[2].posY = map.gridStartPosY + 2*map.boxWidthAndHeight - map.centerBoxPosY
object.nextID = object.nextID + 1
object[3] = blueprint[2]
object[3].img = blueprint[2].img
object[3].id = object.nextID
object[3].activePlayerChar = false
object[3].isOnMap = true
object[3].posX = map.gridStartPosX + 1*map.boxWidthAndHeight - map.centerBoxPosX
object[3].posY = map.gridStartPosY + 1*map.boxWidthAndHeight - map.centerBoxPosY
object.nextID = object.nextID + 1
--end of test
function printObjectOnMap(isOnMap, img, posX, posY)
if isOnMap == true then
imgWidth = img:getWidth()
love.graphics.draw(img, posX, posY, 0, 1, 1, imgWidth/2, imgWidth/2)
end
end
for i = 1, #object do
printObjectOnMap(object[i].isOnMap, object[i].img, object[i].posX, object[i].posY)
end