0
votes

I want to spawn some random objects and i managed to do it from the top of the screen. Then i'd like to make them fall like in an endless runner game. But the code doesn't work and it gives me an error. Here is it:

local function spawn()
local object1 = display.newImage(group[i1],29,1)   <---this refers to a position in a group of objects
object1:scale(1.23,1.30)
end
timer.performWithDelay(2000,spawn,-1)

local function fall()
object1.y = object1.y + 10    <---it says that this is a nil value
timer.performWithDelay(100,fall,-1)
1
object1 is a local variable in the spawn function. It doesn't exist in the fall function. - Etan Reisner
So what should i do? Just declare the variable without local? - blackbird192
If you can only ever have one of those objects that would work but probably isn't the right solution. You need to figure out how to manage the objects you create and how to have them available where you need them (either because you can look them up somewhere global or because they are passed around as arguments/etc.). - Etan Reisner

1 Answers

0
votes

The problem is that you are calling one function variable from another function. Object 1 is a local variable within the spawn function, so it is more or less nonexistent when called from another function.