why am I getting this error?
Posted: Tue Dec 15, 2020 5:19 pm
I know I am missing something completely fundamental and obvious, but Im getting why this doesnt work and I have gone thru quite a few examples and videos and I am only getting more confused.
I am getting this error
in this little script:
I am assuming somehow hero.x is nil- but I dont know why its nil. Wanting to check hero.x position in relation mouse.x position.
So how to create the hero reference in such a way I can see and check coordinates etc?
I am getting this error
Code: Select all
Error
main.lua:33: attempt to compare nil with number
Traceback
main.lua:33: in function 'update'
[C]: in function 'xpcall'
in this little script:
Code: Select all
--create our tables, image here is just a name
myImage={}
mouse={}
--loads our myImages
function love.load()
myImage.image = love.graphics.newImage("hand.png")
myImage.background= love.graphics.newImage("800x600.jpg")
-- here I define hero as a image
hero= love.graphics.newImage("hero.png")
movex=0
movey=0
stepx=0
stepy=0
end
function love.mousereleased(x, y, button)
if button == 1 then
movex=x
movey=y
end
end
--get our mouse position
function love.update()
love.mouse.setVisible( false )
mouse.x, mouse.y = love.mouse.getPosition()
-- here is where the bug occurs
if hero.x < mouse.x then stepx=stepx+4
end
end
function love.draw()
--draw background
love.graphics.draw(myImage.background,0,0)
--draw image to mouse position
love.graphics.draw(myImage.image,mouse.x-40,mouse.y-10)
--draw hero and move towards
love.graphics.draw(hero,stepx,stepy)
end
I am assuming somehow hero.x is nil- but I dont know why its nil. Wanting to check hero.x position in relation mouse.x position.
So how to create the hero reference in such a way I can see and check coordinates etc?