The basic problem is that a variable cannot “leave” a function unless you tell it to. It exists only within the function.
[lua]function flappybird()
local cooldude = 34
end
print(cooldude) – nil[/lua]
In that example, cooldude is local, so it exists only within the function. If you want it to exist outside, you only have two options:
[lua]
local cooldude
function flappybird()
cooldude = 34
end
print(cooldude) – 34
[/lua]
This is the “pre-declare” method. You declare the variable first (even if it’s empty) and then update it from the function. Since the variable was made outside of the function it can be seen outside, as well.
[lua]
function flappybird()
local cooldude = 34
return cooldude
end
print(flappybird()) – 34
local tempVar = flappyBird()
print(tempVar) – 34
[/lua]
“return” is the other method. When you type return, the function ends immediately and gives you back the value you choose.
In your code, you create jetSprite within a function, but it can’t escape the function. It’s not visible anywhere else. So to everything else, it’s “nil”.
Since you’re looking for it in a touchScreen command (where passing in a variable might be difficult, I think pre-declare is the easiest option for you. You can even declare the variable at the beginning of your file; lots of professional code sometimes starts off with big pre-declares.