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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

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

Post 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.
Attachments
walktest.love
(8.58 KiB) Downloaded 143 times
Last edited by miniaturedog on Tue Apr 30, 2019 2:34 pm, edited 1 time in total.
User avatar
CrimsonGuy
Prole
Posts: 48
Joined: Thu Apr 04, 2019 3:32 pm

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

Post 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
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

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

Post 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).
User avatar
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

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

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests