addeventlistener - addEventListner not working in Lua -
here's relevant functions in code, following error:
stack traceback:
[c]: in function 'error' ?: in function 'getorcreatetable' ?: in function 'addeventlistener' ?: in function 'addeventlistener' main.lua:26: in function 'createplayscreen' main.lua:79: in main chunk
my code:
-- set forward references local spawnenemy --create play screen local function createplayscreen() local background = display.newimage("spacebackground.jpg") background.x = centerx background.y = -100 background.alpha = 0 background:addeventlistener ( "tap", shipsmash ) local planet = display.newimage("earth.png") planet.x = centerx planet.y = display.contentheight+60 planet.alpha = .2 planet.xscale = 2 planet.yscale = 2 planet:addeventlistener ( "tap", shipsmash ) transition.to(background, {time = 2000, alpha = 1, y = centery, x = centerx}) local function showtitle() local gametitle = display.newimage("gametitle.png") gametitle.alpha = 0 gametitle:scale(4,4) transition.to(gametitle, {time=500, alpha = 1, xscale = 1, yscale = 1}) spawnenemy() end transition.to(planet, {time = 2000, xscale = .75, yscale = .75, alpha = 1, y = centery, oncomplete = showtitle}) end --game functions function spawnenemy() local enemy = display.newimage("asteroid.png") enemy.x = math.random(20, display.contentwidth-20) enemy.y = math.random(20, display.contentheight-20) enemy.alpha = 0 transition.to(enemy, {time = 200 , alpha =1}) enemy:addeventlistener ( "tap", shipsmash ) end local function shipsmash(event) local obj = event.target display.remove(obj) return true end createplayscreen() startgame()
what problem here ?
you referencing local function shipsmash
in addeventlistener
calls (enemy:addeventlistener ( "tap", shipsmash )
), function not defined @ point. need move definition of shipsmash
before functions expect use it.
if run static code analyzer on file (using lua-inspect, zerobrane studio, or tool this list), you'll see these 2 warnings among other things, should give idea function not referenced:
file.lua:6: first use of unknown global variable 'shipsmash' file.lua:41: unused local function 'shipsmash'
Comments
Post a Comment