Page 1 of 1

[anim8] attempt to call method draw (nil value) [SOLVED]

Posted: Sat Apr 27, 2019 9:07 pm
by miniaturedog
I've been trying for the past few hours or so to animate a walking sprite using anim8, but after looking through several tutorials and forum posts and tweaking my code, I got this error:

Code: Select all

Error
main.lua:34: attempt to call method 'draw' (a nil value)
Traceback
main.lua:34: in function 'draw'
[C]: in function 'xpcall'
Here are the relevant portions of my code:

Code: Select all

local anim8 = require "anim8/anim8"
local player

function love.load()
    local walk = love.graphics.newImage("turtle_walking.png")
    local g = anim8.newGrid(32, 32, walk:getWidth(), walk:getHeight())
    
    player = {
    -- player attributes
        animations = {
            right = anim8.newAnimation(g('1-2',1), 1)
        }
    }
    player.animation = player.animations.right
end

function love.update(dt)
    player.animation:update(dt)
    if next(love.touch.getTouches()) ~= nil then
        player.x = player.x + (player.speed * dt)
        player.animation = player.animations.right
    end
    
    if love.keyboard.isDown("d") then
        player.x = player.x + (player.speed * dt)
        player.animation = player.animations.right
    end
end

function love.draw()
    player.animations:draw(player.walk, player.x, player.y)
end
I've also attached a .love file. I'm relatively new to programming and LÖVE in general, so any help would be appreciated.

Re: [anim8] attempt to call method draw (nil value)

Posted: Sun Apr 28, 2019 1:46 am
by CrimsonGuy
Change

Code: Select all

local walk = love.graphics.newImage("turtle_walking.png")
to

Code: Select all

walk = love.graphics.newImage("turtle_walking.png")
If walk is local it cant be reached outside of love.load, only use local for variables that you wont need outside of their code block, still use local whenever you can is a good practice.

more info here

https://www.lua.org/pil/4.2.html

Re: [anim8] attempt to call method draw (nil value)

Posted: Sun Apr 28, 2019 9:18 am
by grump
The code snippet you posted is different from the actual code in the love file.

The actual fix is calling player.animation:draw (not animations).

Re: [anim8] attempt to call method draw (nil value)

Posted: Tue Apr 30, 2019 2:36 pm
by miniaturedog
grump wrote: Sun Apr 28, 2019 9:18 am The code snippet you posted is different from the actual code in the love file.

The actual fix is calling player.animation:draw (not animations).
You're right, thank you! I must've read over that a million times and never caught the error LOL

And yes, I realized the code was different after I posted :'D