[SOLVED] Have some troubles with Animation again
Posted: Tue Apr 30, 2024 10:58 am
Hey there! I'm trying to implement an animation function, but it does'nt work well. Here is the code:
I'm getting an error, that lua can't index the animation function. I think, the problem is in the getAnimationFrame function. I'm not sure, what to write there, because index and lastIndex are wrong or not indexable..???
Code: Select all
local player
local walkingDownAnimation
local walkingLeftAnimation
local walkingRightAnimation
local walkingUpAnimation
local tilemap
local num_tiles
local tiles
function love.load()
FramesWalkingDown = {
love.graphics.newImage("/Character_Sprites/going_down_01.png"),
love.graphics.newImage("/Character_Sprites/going_down_02.png"),
love.graphics.newImage("/Character_Sprites/going_down_03.png"),
love.graphics.newImage("/Character_Sprites/going_down_04.png"),
love.graphics.newImage("/Character_Sprites/going_down_05.png"),
love.graphics.newImage("/Character_Sprites/going_down_06.png")
}
FramesWalkingUp = {
love.graphics.newImage("/Character_Sprites/going_up01.png"),
love.graphics.newImage("/Character_Sprites/going_up02.png"),
love.graphics.newImage("/Character_Sprites/going_up03.png"),
love.graphics.newImage("/Character_Sprites/going_up04.png"),
love.graphics.newImage("/Character_Sprites/going_up05.png"),
love.graphics.newImage("/Character_Sprites/going_up06.png")
}
FramesWalkingRight = {
love.graphics.newImage("/Character_Sprites/going_right01.png"),
love.graphics.newImage("/Character_Sprites/going_right02.png"),
love.graphics.newImage("/Character_Sprites/going_right03.png"),
love.graphics.newImage("/Character_Sprites/going_right04.png"),
love.graphics.newImage("/Character_Sprites/going_right05.png"),
love.graphics.newImage("/Character_Sprites/going_right06.png")
}
FramesWalkingLeft = {
love.graphics.newImage("/Character_Sprites/going_left01.png"),
love.graphics.newImage("/Character_Sprites/going_left02.png"),
love.graphics.newImage("/Character_Sprites/going_left03.png"),
love.graphics.newImage("/Character_Sprites/going_left04.png"),
love.graphics.newImage("/Character_Sprites/going_left05.png"),
love.graphics.newImage("/Character_Sprites/going_left06.png")
}
walkingDownAnimation = createAnimation(0.1, FramesWalkingDown)
walkingUpAnimation = createAnimation(0.1, FramesWalkingUp)
walkingRightAnimation = createAnimation(0.1, FramesWalkingRight)
walkingLeftAnimation = createAnimation(0.1, FramesWalkingLeft)
tilemap = {
{16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
{16, 16, 16, 16, 16, 16, 11, 12, 13, 12, 13, 12, 13, 12, 13, 15},
{16, 16, 16, 16, 16, 16, 21, 22, 23, 22, 23, 22, 23, 22, 23, 25},
{16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
{16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
{16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
{16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
{11, 12, 13, 12, 13, 12, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
{21, 22, 23, 22, 23, 22, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
{31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
{31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
{31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
{51, 52, 53, 52, 53, 15, 32, 33, 32, 33, 32, 33, 11, 52, 53, 55},
{16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
{16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
{16, 16, 16, 16, 16, 51, 52, 53, 54, 52, 53, 54, 55, 16, 16, 16}
}
num_tiles = 60
tiles = {}
for i = 1, #tilemap do
local row = tilemap[i]
for j = 1, #row do
local tile = row[j]
if tiles[tile] == nil then
tiles[tile] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", tile) .. ".png")
end
end
end
player = {}
width = 16
height = 16
player.x = 70
player.y = 70
player.image = love.graphics.newImage("/Character_Sprites/standing_down.png" )
end
function resetAnimation(animation)
animation.timer = 0.0
animation.index = 1
end
function advanceAnimation(animation, dt)
animation.timer = animation.timer + dt
if animation.timer > animation.duration then
animation.timer = 0
animation.index = animation.index + 1
if animation.index > animation.lastIndex then
animation.index = 1
end
end
end
function createAnimation(frameDuration, frames)
local animation = {timer = 0.0, index = 1, duration = frameDuration, frames = frames, lastIndex = #frames}
return animation
end
local dt = love.timer.getDelta()
local Down = {}
function love.joystickpressed(joystick, button)
Down[button] = true
end
function love.joystickreleased(joystick, button)
Down[button] = nil
end
function getAnimationFrame(animation)
return animation.frames[animation.lastIndex]
end
function love.update(dt)
if Down[6] then
player.x = player.x + 4
player.currentAnimation = walkingRightAnimation
end
if Down[4] then
player.y = player.y + 4
player.currentAnimation = walkingDownAnimation
end
if Down[2] then
player.x = player.x - 4
player.currentAnimation = walkingLeftAnimation
end
if Down[1] then
player.y = player.y - 4
player.currentAnimation = walkingUpAnimation
end
end
function love.draw()
for i = 1, #tilemap do
local row = tilemap[i]
for j = 1, #row do
local tile = row[j]
if tiles[tile] then
love.graphics.draw(tiles[tile], (j - 1) * width, (i - 1) * height)
end
end
end
love.graphics.draw(getAnimationFrame(player.currentAnimation), player.x, player.y)
end