There’s a few things that might be going on here:
[code]local myImage
function showImage()
myImage = display.newImage(“heyo.png”)
end
showImage()
myImage.x = 10
[/code]
This is fine, it’s garden variety pre-declaring your variables. But there are two popular ways in which users get this wrong:
[code]-- [1]
local myImage
function showImage()
local myImage = display.newImage(“heyo.png”)
end[/code]
Here, you’ve redeclared. The myImage at the top is not the same as the myImage inside the function, so your first myImage is still == nil. You need to drop “local” from inside the function.
[code]-- [2]
local myImage
function showImage()
myImage = display.newImage(“heyo.png”)
end
local pic = showImage()
pic.x = 10[/code]
Here, the problem is that you are not returning anything from the function. If you don’t type “return” anywhere, showImage() will return “false”, which obviously can’t set x/y. “pic” is effectively empty. The proper way to do it is something like this:
function showImage()
local image = display.newImage("heyo.png")
return image
end [import]uid: 41884 topic_id: 26274 reply_id: 106555[/import]