Please help with my code

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
OscarDaFries
Prole
Posts: 2
Joined: Fri Feb 02, 2024 9:48 am

Please help with my code

Post by OscarDaFries »

the ERROR:
Error

Resources/anim8.lua:36: frameWidth should be a number, was "nil"


Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'error'
Resources/anim8.lua:36: in function 'assertPositiveInteger'
Resources/anim8.lua:101: in function 'newGrid'
main.lua:12: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[C]: in function 'xpcall'

My oringinal code:
function love.load()
anim8 = require 'Resources/anim8'
love.graphics.setDefaultFilter("nearest", "nearest")

player = {}
player.x = 400
player.y = 350
player.speed = 7.5
gravity = 0
isJumping = false
jumpHeight = 100
player.grid = anim8.newGrid( )

player.sprite = love.graphics.newImage('Lua ig assets/Sprites/Swordsman.png')
player.spriteSheet = love.graphics.newImage('Lua ig assets/Sprites/Swordsman run spritesheet.png')
player.grid = anim8.newGrid( 48, 48, player.spriteSheet:getWidth(), player.spriteSheet:getHeight() )

player.animations = {}
player.animations.left = anim8.newAnimation( player.grid('1-8', 2), 0.2 )
player.animations.right = anim8.newAnimation(player.grid('1-8', 1), 0.2 )

player.anim = player.animations.left
end

function love.update(dt)
local isMoving = false

if love.keyboard.isDown("d") then
player.x = player.x + player.speed
player.anim = player.animations.right
isMoving = true
end

if love.keyboard.isDown("a") then
player.x = player.x - player.speed
player.anim = player.animations.left
isMoving = true
end

if love.keyboard.isDown("space") and not isJumping then
gravity = -10
isJumping = true
end

if isJumping then
player.y = player.y + gravity
gravity = gravity + 0.5

if player.y > 350 then
player.y = 350
gravity = 0
isJumping = false
end
end


if isMoving == false then
player.anim:gotoFrame(9)
end

player.anim:update(dt)
end

function love.draw()
player.anim:draw(player.spriteSheet, player.x, player.y, nil, 10)
end
vilonis
Prole
Posts: 12
Joined: Sun Jan 14, 2024 10:52 pm

Re: Please help with my code

Post by vilonis »

I don’t k is what anim8 is, but it expects its first parameter to be a positive integer and you aren’t passing anything.
OscarDaFries
Prole
Posts: 2
Joined: Fri Feb 02, 2024 9:48 am

Re: Please help with my code

Post by OscarDaFries »

I follow Chalacade's turtorial for this code, if possible please point out the wrong part and rewrite it. Thank you
User avatar
Nikki
Citizen
Posts: 87
Joined: Wed Jan 25, 2017 5:42 pm

Re: Please help with my code

Post by Nikki »

Code: Select all

Error

Resources/anim8.lua:36: frameWidth should be a number, was "nil"
so, as it say, one of anim8's functions is being called with nil instead of a number.

to figure out which function you can look further in the error message :

Code: Select all

Resources/anim8.lua:101: in function 'newGrid'
main.lua:12: in function 'load'
so over at your line 12, in load() the function newGrid is called it says,
lo and behold:

Code: Select all

jumpHeight = 100
player.grid = anim8.newGrid( )
a few lines further you actually call the function the proper way, so maybe copy pasta or something, but the error has happened already then..

basically the error message and stack trace is there for you, the developer that run into an error, you can learn a lot from it.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests