Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"attempt to index a nil value"-error in Lua

Tags:

lua

function newImage(Image, posx, posy)
    pic = Bitmap.new(Texture.new(Image))
    stage:addChild(pic)
    pic:setPosition(posx,posy)
end

local birdie = newImage("bird.png", 100, 100)
birdie:setAnchorPoint(0.5,0.5)
birdie:setRotation(45)

If I call newImage() like above, the image gets loaded but when I try to use birdie:setAnchorpoint() it gives the error "attempt to index birdie, a nil value".
How can I fix this?

like image 478
S Khurana Avatar asked Apr 13 '14 21:04

S Khurana


1 Answers

You're not returning anything from your function call. Also, use local variables inside functions.

function newImage(Image, posx, posy)
    local pic = Bitmap.new(Texture.new(Image)) 
    stage: addChild(pic)
    pic:setPosition(posx,posy)
    return pic
end
like image 72
hjpotter92 Avatar answered Sep 28 '22 00:09

hjpotter92