specifically with rendering images on top of another, currently my game seems buggy when there's a not of images on the screen because the render order changes so I was wondering if it is possible to force render an image on top of another image like a ZIndex or Render order?
Here's my .love file Here's a screenshot of it What I want is the coins that are created first to be under the coins that are created after and not appear in a random order like this
Also here's my Source Code
Code: Select all
local count = 0
local output = ""
local IMAGES = {}
local ImagesToDraw = {}
local function registerImages()
for i, filePath in ipairs(love.filesystem.getDirectoryItems("/Images")) do
IMAGES[i] = "/Images/" .. filePath
end
end
registerImages()
local SOUNDS = {}
local function registerSounds()
for i, filePath in ipairs(love.filesystem.getDirectoryItems("/Sounds")) do
SOUNDS[i] = "/Sounds/" .. filePath
end
end
registerSounds()
local function getRandom()
local index = love.math.random(1, #SOUNDS)
return love.audio.newSource(SOUNDS[index], "static")
end
local function increment()
count = count + 1
end
local function playCoinSFX()
local sound = getRandom()
sound:play()
end
local currentCoins = 0
local function drawImages()
for image, imageData in pairs(ImagesToDraw) do
imageData.y = imageData.y + 1
imageData.r = imageData.r + 0.02
if imageData.y < 600 then
love.graphics.draw(image, imageData.x, imageData.y, imageData.r, 0.5, 0.5, 256/2, 256/2)
else
image:release()
ImagesToDraw[image] = nil
currentCoins = currentCoins - 1
end
end
end
function love.draw()
love.graphics.print(count, 400, 300)
love.graphics.print(output, 400, 400)
drawImages()
end
local MAX_COINS = 100
local LAST_COIN = love.timer.getTime()
local function canCreateCoin()
return love.timer.getTime() - LAST_COIN > 0.1 and currentCoins < MAX_COINS
end
local function createCoin()
LAST_COIN = love.timer.getTime()
currentCoins = currentCoins + 1
local image = love.graphics.newImage(IMAGES[1])
ImagesToDraw[image] = { ["image"] = image, x = love.math.random(0, 600), y = -100, r = 0 }
output = LAST_COIN
end
local function playEffect()
if canCreateCoin() then
createCoin()
increment()
playCoinSFX()
end
end
local function registerEvents()
function love.keypressed()
playEffect()
end
function love.mousepressed()
playEffect()
end
function love.mousemoved()
playEffect()
end
end
registerEvents()