Reputation: 1
I'm creating a new game with Coronasdk, I need to make trees appear from the right of the screen and make them scroll to the left. That's the code
local Trees = {} --I generate trees that appears randomly
local function createTree(event)
table.insert(Trees, tree)
return tree
end
local function treeScroll (self, event)
self.x = self.x - 3
end
MIN = 3000
MAX = 7000
local function gameLoop()
local tree = createTree()
tree.enterFrame = treeScroll
Runtime:addEventListener("enterFrame", tree)
for i, thisTree in ipairs (Trees) do
if thisTree.x < -100 then
Runtime:removeEventListener("enterFrame", thisTree)
display.remove (thisTree)
table.remove (Trees, i)
end
end
timer.cancel(timer1)
timer1=timer.performWithDelay(math.random(MIN, MAX), gameLoop, 0)
end
timer1 = timer.performWithDelay (math.random(MIN, MAX), gameLoop, 0)
It tells me that addEventListener:Listener cannot be nil:nil, and it refers to this part
local function gameLoop()
local tree = createTree()
tree.enterFrame = treeScroll
Runtime:addEventListener("enterFrame", tree)
for i, thisTree in ipairs (Trees) do
if thisTree.x < -100 then
Runtime:removeEventListener("enterFrame", thisTree)
display.remove (thisTree)
table.remove (Trees, i)
end
end
timer.cancel(timer1)
timer1=timer.performWithDelay(math.random(MIN, MAX), gameLoop, 0)
end
I don't know what to do
Upvotes: 0
Views: 58