I have no idea why I am getting this error, I am a new dev.

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
moths
Prole
Posts: 1
Joined: Sun Mar 28, 2021 10:46 pm

I have no idea why I am getting this error, I am a new dev.

Post by moths »

I have a table set up like this
sprites = {}
sprites.background = love.graphics.newImage('sprites/background.png')
sprites.player = love.graphics.newImage('sprites/player.png')
In draw I have this
function love.draw()
love.graphics.draw(sprites.background, 0, 0)

world:draw()
love.graphics.draw(sprites.player, px, py, .1, .1, sprites.player.getWidth() / 2, sprites.player.getHeight() / 2)
end
But I'm getting this error
Error

main.lua:56: bad argument #1 to 'getWidth' (Texture expected, got no value)


Traceback

[C]: in function 'getWidth'
main.lua:56: in function 'draw'
[C]: in function 'xpcall'
can somebody please tell me what's going on and how to fix it
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: I have no idea why I am getting this error, I am a new dev.

Post by togFox »

what's line 56?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: I have no idea why I am getting this error, I am a new dev.

Post by MrFariator »

The error is because you used a dot (".") instead of a colon (":") when calling the getWidth and getHeight functions.

The corresponding line of code needs to be either of the two:

Code: Select all

-- with "."
love.graphics.draw(sprites.player, px, py, .1, .1, sprites.player.getWidth(sprites.player) / 2, sprites.player.getHeight(sprites.player) / 2)

-- with ":", the preferred method in a case like this, and is syntactically equivalent to the "." method
love.graphics.draw(sprites.player, px, py, .1, .1, sprites.player:getWidth() / 2, sprites.player:getHeight() / 2)
What the colon syntax does is implicitly send the invoked function the object it's attached to as the first function argument. As such, in this case it sends the löve object representing the texture. getWidth and getHeight both expect a texture as the first passed function argument, hence the error if you don't do either of the two above.
Post Reply

Who is online

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