addEventListner not working in Lua - lua

Here's the relevant functions in my code, I get the 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 up 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 is the problem here ?

You are referencing a local function shipSmash in your addEventListener calls (enemy:addEventListener ( "tap", shipSmash )), but the function is not defined at that point. You need to move the definition of shipSmash before the functions where you expect to use it.
If you run a static code analyzer on your file (using lua-inspect, ZeroBrane Studio, or another tool from this list), you'll see something like these two warnings among other things, which should give you an idea that this function is not properly referenced:
file.lua:6: first use of unknown global variable 'shipSmash'
file.lua:41: unused local function 'shipSmash'

Related

Receiving "Attempt to index global 'sprite' (a nil value)" in Lua

Im attempting to make a simple game app and keep running into this problem. I stopped programming in Lua for a few years so I don't exactly remember how to fix this. Anyway, my code is as follows:
EDIT: Here is the entire file. Still trying to figure out the formatting of Stack Overflow. Error occurs at line 76.
module(..., package.seeall)
-- Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
---------
local Rad = math.rad
local Sin = math.sin
local Cos = math.cos
local Pi = math.pi
local Atan2 = math.atan2
local radD = 180 / Pi
local DegR = Pi / 180
local touchPoint = display.newCircle(localGroup, -50, -50, 20)
touchPoint.isFocus = false
touchPoint.alpha = 0
function GetDistanceFromObjects(obj1, obj2)
local xDist = obj1.x - obj2.x
local yDist = obj1.y - obj2.y
local dist = Sqrt((xDist * xDist) + (yDist * yDist))
return dist
end
function getAngleDeg(inX1, inY1, inX2, inY2)
local xDist = inX2 - inX1
local yDist = inY2 - inY1
local angRad = Atan2(yDist, xDist)
return angRad * radD + 90
end
require "sprite"
function VectorFromAngle(inAngle, inVelocity)
local vx = Cos(Rad(inAngle-90))
local vy = Sin(Rad(inAngle-90))
if(inVelocity ~= nil)then
vx = vx * inVelocity
vy = vy * inVelocity
end
return vx,vy
end
require ( "physics" )
physics.start()
physics.setGravity( 1, 1 )
--( x, y )
--physics.setDrawMode ( "hybrid" )
math.randomseed(os.time())
local background = display.newImage("yazd.jpeg")
localGroup:insert(background)
--width of image divided by # of pics lined up from left to right (in the sprite) = the first #
--height of image divided by # of pics lined up from top to bottom (in the sprite) = the second #
local birdSheet = sprite.newSpriteSheet( "enemy.jpg", 59, 50 )
local birdSet = sprite.newSpriteSet(birdSheet, 1, 1)
-- images 1-14
sprite.add( birdSet, "bird", 1, 1, 200, 0 )
-- play 1-14, each image every 200 ms, 0 = loop count, which is infinite
local bird1 = sprite.newSprite( birdSet )
bird1.x = 40 -- starting point
bird1.y = 40 -- starting point
bird1.xScale = 0.5 --scale down x
bird1.yScale = 0.5 --scale down y
bird1:prepare("bird") --prepare sprite sequence
bird1:play() --play sprite
localGroup:insert(bird1)
--only local to this group
local killSheet = sprite.newSpriteSheet("explosion.png", 100, 100)
local killSet = sprite.newSpriteSet(killSheet, 1, 9)
sprite.add(killSet, "kill", 1, 9, 200, 1)
local birdCount = 1
local transDirection12
local function transDirection1()
bird1.xScale = 0.5
transition.to(bird1, {time=math.random(200,500), x = math.random(200,490), y = math.random(10,310), alpha = (math.random(9,100))/100, onComplete = transDirection12})
end
transDirection12 = function()
bird1.xScale = 0.5
transition.to(bird1, {time= math.random(200,500), x = math.random(200,490), y = math.random(10,310), alpha = (math.random(9,100))/100, onComplete = transDirection1})
end
transDirection1()
-- local transDirection1 declares what will be used (local function)
-- transDirection1 = function
-- following it are the function qualities
-- declares it will use object/image called bird1 and scales it to .5
-- time = ____ means it will take a certain time, between ____ and ____ to complete the transition
-- x=____ means that is where it will move to on the x axis
-- y=____ means that is where it will move to on the y axis
-- alpha = ___ means the is how transparent it will be
-- onComplete = ________ means that when the action is complete, it will call another function
-- The next function has the same qualities as transDirection1, but the onComplete part calls transDirection1 and they continue to loop
-- transDirection1() declares transDirection1 so the app knows about it and can use it
-- the other trans do not need to be declared because they are part of transDirection1, which is already declared
--(x, y, size.x, size.y)
local player = display.newImage( "mk11.png" )
player.x = 240
player.y = 260
player.xScale = .5
player.yScale = .5
localGroup:insert( player )
-- add physics to all the objects wanted: (object wanted, "static" or "dynamic")
physics.addBody(player, "static", {radius=30, isSensor = true})
physics.addBody(bird1, "static", {radius=23})
local function shoot(inPointX, inPointY)
-- (start at the x of the player + 10, also start at the y of the player, the radius of the circle is 5)
local bullet = display.newImage( "bullet2.png" )
bullet.x = player.x
bullet.y = player.y
-- add physics to the object, which is the bullet.
-- Make the bullet "dynamic" or moving
physics.addBody(bullet, "dynamic")
bullet.isFixedRotation = true
localGroup:insert( bullet )
local velocity = 300
local vx, vy = VectorFromAngle(player.rotation, velocity)
bullet.rotation = player.rotation
bullet:setLinearVelocity(vx, vy)
end
function RotateToTouchPoint(inPointX, inPointY)
local ang = getAngleDeg(player.x, player.y, inPointX, inPointY)
player.rotation = ang
end
local function ScreenTouchListener(event)
local phase = event.phase
if(phase == "began")then
if(touchPoint.isFocus == false)then
touchPoint.alpha = 1
touchPoint.x = event.x
touchPoint.y = event.y
display.getCurrentStage():setFocus(touchPoint, event.id)
touchPoint.isFocus = true
RotateToTouchPoint(event.x, event.y)
shoot(event.x, event.y)
end
elseif(touchPoint.isFocus)then
if(phase == "moved")then
touchPoint.x = event.x
touchPoint.y = event.y
RotateToTouchPoint(event.x, event.y)
elseif(phase == "ended" or phase == "cancelled")then
display.getCurrentStage():setFocus(touchPoint, nil)
touchPoint.isFocus = false
touchPoint.alpha = 0
end
end
return true
end
local function gotShot (event)
event.target:removeSelf()
event.other:removeSelf()
local explosion = sprite.newSprite(killSet)
explosion.x, explosion.y = event.target.x, event.target.y
explosion:prepare("kill")
explosion:play()
localGroup:insert( explosion )
birdCount = birdCount - 1
-- when there are no more birds, remove the runtime event listener and perform the
-- function with a delay of 500 m.s. The function changes the scene to test.lua
if "ended" then
if birdCount == 0 then
Runtime:removeEventListener("touch", ScreenTouchListener)
timer.performWithDelay(500, function()
director:changeScene("mainPage") end, 1)
end
end
end
bird1:addEventListener("collision", gotShot)
Runtime:addEventListener("touch", ScreenTouchListener)
---------
-- MUST return a display.newGroup()
return localGroup
end
Any help is appreciated!
The error message is perfectly clear -- the variable sprite used at this line:
local bird1 = sprite.birdSheet( birdSet )
has a nil value, meaning it has not been initialized or was set to nil. You need to show the earlier code where you should have set it up.
(After OP updates)
I think this line
require "sprite"
should actually be
sprite = require "sprite"
You can read more in modules tutorial here:
http://lua-users.org/wiki/ModulesTutorial

Attempt to index global 'object' (a nil value)

So this is the error I have been getting:
Game.lua:66: attempt to index global 'Spears' (a nil value)
stack traceback:
Game.lua:66: in function '_listener'
and this is some of the code, showing where the error happens:
local Spears = {}
local SpearsTimer
local SpearsCounter = 1
local delayTimer
local removeListeners
end
end
local field = display.newCircle( 0, 0, 0 ) ; field.alpha = 0.3
field.name = "field"
field.x = display.contentCenterX ; field.y = display.contentCenterY
physics.addBody( field, "static", { isSensor=true, radius=320 } )
local spawnSpears = function()
local Fall = math.random(display.contentWidth * -0.2, display.contentWidth * 1.2)
Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 )
Spears[SpearsCounter].x = Fall
Spears[SpearsCounter].y = -200
physics.addBody( Spears[SpearsCounter], "dynamic", {bounce = 0} )
--Spears[SpearsCounter].collision = onCollision
--Spears[SpearsCounter]:addEventListener( "collision", Spears[SpearsCounter] )
transition.to( Spears[SpearsCounter], { rotation = Spears[SpearsCounter].rotation+720, time=15000, onComplete=spinImage } )
Spears[SpearsCounter].gravityScale = 0.5
sceneGroup:insert(Spears[SpearsCounter])
group:insert(Spears[SpearsCounter])
SpearsCounter = SpearsCounter + 1
end
SpearsTimer = timer.performWithDelay( 5000, spawnSpears, -1 )
The Error points to line 66, which is this line of code:
Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 )
So what am I doing wrong?
Oh, and keep in mind that I am trying to make objects spawn randomly throughout the screen and fall/go to the center of the screen. I put Radial Gravity.
The problem is you have declared the
local Spears = {}
in side a function which is not accessible outside that function. Try declaring it outside the function and access. Learn about the scope of the variables.
This is most likely a scope problem. This tutorial will guide you into understanding scope:
https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Attempt to Index GlobalCredits 'event' (a nil value)

I have just started to learn LUA in school, but i cannot find many helpful tutorials o the internet to aid in my learning. I have made a simple game (which doesn't work yet, i realize that) and a main menu. However, when i try to start the app, it gives me this error:
/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: attempt to index global 'showCredits' (a nil value)
stack traceback:
/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: in main chunk
[Finished in 9.4s]
I have looked the error, and I cannot seem to understand how to fix it. The other questions have said something about returning the function returning a nil value, and that I should add a return statement to the end, but that doesn't work either.
Here is the code # line 47.
function showCredits.touch(e)
playButton.isVisible = false
creditsButton.isVisible = false
creditsView = display.newImage('credits.png', 0, display.contentHeight)
lastY = name.y
transition.to(name, {time = 300, y = display.contentHeight * 0.5 - title.height - 25})
transition.to(creditsView, {time = 300, y = display.contentHeight * 0.5 + creditsView.height, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end
Here is my full code, in case the problem lies elsewhere:
display.setStatusBar(display.HiddenStatusBar)
radius = 40
smallTime = 200
bigTime = 800
score = 0
scoreInc = 2000
--HomePage
local name
local playButton
local creditsButton
local homePage
--Credits
local creditsPage
--Sounds
local circleSpawn = audio.loadSound( "circle_spawn.wav" )
local circleTap = audio.loadSound( "circle_tap.wav" )
function Main()
name = display.newImage('title.png', display.contentWidth / 2, 53)
name:scale( .5, .5 )
playButton = display.newImage('playButton.png', display.contentWidth / 2, 245)
playButton:scale( .5, .5 )
creditsButton = display.newImage('creditsButton.png', display.contentWidth / 2, 305)
creditsButton:scale( .5, .5 )
homePage = display.newGroup(name, playButton, creditsButton)
startButtonListeners('add')
end
function showCredits.touch(e)
playButton.isVisible = false
creditsButton.isVisible = false
creditsView = display.newImage('credits.png', 0, display.contentHeight)
lastY = name.y
transition.to(name, {time = 300, y = display.contentHeight * 0.5 - title.height - 25})
transition.to(creditsView, {time = 300, y = display.contentHeight * 0.5 + creditsView.height, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end
function hideCredits.touch(e)
transition.to(creditsView, {time = 300, y = display.contentHeight, onComplete = function() creditsButton.isVisible = true playButton.isVisible = true creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})
transition.to(name, {time = 300, y = lastY});
end
function startButtonListeners(action)
if(action == 'add') then
playButton:addEventListener('touch', playGame)
creditsButton:addEventListener('touch', showCredits)
else
playButton:removeEventListener('touch', playGame)
creditsButton:removeEventListener('touch', showCredits)
end
end
Main()
printScore = display.newText("Score: " .. tostring(score), display.contentWidth-80, 40, native.systemFontBold, 20)
-- A function that creates random circles
function generateCircle ()
-- Creates a new circle between 0 (the left most bounds) and the width of the display (being the content width), and also
-- 0 (the upper most bounds) and the height of the display (being the content height). The radius of the circle is 'radius'
x = math.random(radius, display.contentWidth-radius)
y = math.random(80, display.contentHeight)
score = score + scoreInc
myCircle = display.newCircle( x, y, radius )
myCircle:setFillColor( math.random(), math.random(), math.random() )
delayTime = math.random(smallTime, bigTime)
score = score + scoreInc
printScore.text = "Score:"..tostring(scores)
local spawnChannel = audio.play( circleSpawn )
timer.performWithDelay( delayTime, generateCircle )
end
generateCircle()
function myCircle:touch( event )
local tapChannel = audio.play( circleTap )
myCircle:removeSelf()
end
myCircle:addEventListener( "touch", myCircle )
The answer by greatwolf will work. But just a tip from my experience. One way I like to create functions is to try to define the name of the function first near the top of the lua file. Then I will define the function later on in the file. Something like this:
--function preallocation
local onPlayTap
local onSoundOnTap
local onSoundOffTap
local onCreditsTap
local onHelpTap
---------------------------------------------------------------------------------
-- Custom Function Definitions
---------------------------------------------------------------------------------
--Called when Sound On Button is tapped, turn off sound
onSoundOnTap = function(event)
end
--Called when Sound Off Button is tapped, turn on sound
onSoundOffTap = function(event)
end
--Called when Credits button is tapped, shows credits
onCreditsTap = function(event)
end
--Called when Help button is tapped, shows help
onHelpTap = function(event)
end
--Callback to Play button. Moves scene to Level Picker Scene
onPlayTap = function(event)
end
What this does is allow each function to be called by any other function in the file. If you do it the way you are doing it by adding the function name before the function like so:
local showCredits = {}
function showCredits.touch(e)
end
local hideCredits = {}
function hideCredits.touch(e)
end
your showCredit function will not be able to call the hideCredits function below it because the hideCredits variable has not been defined yet when the showCredit function was defined. Although this may not effect your current game, in future apps or games, you may need to call functions inside of other functions. To make this work properly, predefine all your function variables first, then define all your function afterwards. Hope this helps.

Corona SDK Level: Can't integrate my level into Storyboard - Assertion Failed error

I hope someone can help me because this problem has been driving me crazy the last few days. So I just started with corona - made a few tutorials and really having fun discovering the possibilities.
Problem: I want to integrate a simple game into a storyboard. I somehonw can't manage to find the right combination and always get this error pop up.
File: assertion failed!
Assertion failed!
stack traceback:
[C]: ?
[C]: in function 'assert'
?: in function 'get0rCreateTable'
?: in function 'addEventListener'
?: in function 'addEventListener'
...ik/Desktop/_DummyProjekt2/Fruit Fliesv10/level01.lua:51: in function
<. . . ik/Desktop/_DummyProjekt2/ Fruit Fliesv10/level0l. lua:44>
?: in function 'dispatchEvent'
?: in function <?:1096>
(tail call): ?
?: in function <?:466>
?: in function <?:218>
It's probably a rookie mistake, but I have been stuck for a few days now and definitely need some help. I've gone thru most of the tutorials and documentation I could find with no luck. So any help would be greately welcome :) So this is my level 1
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require ("widget")
local playfile = require ("play")
function scene:createScene(event)
local screenGroup = self.view
local spawnEnemy
local gameTitle
local scoreTxt
local score = 0
local hitBowl
local planet
local speedBump = 0
background = display.newImage("images/background.png")
background.y = centerY
background.x = centerX
bowl = display.newImage("images/bowl2.png")
bowl.x = centerX
bowl.y = centerY
scoreTxt = display.newText( "Score: 0", 0, 0, "orange juice", 22 )
scoreTxt.x = centerX
scoreTxt.y = 10
scoreTxt:setFillColor(1,1,1)
scoreTxt.y = 255
end
function scene:enterScene(event)
enemypics = {"images/blue.png","images/pink.png", "images/green.png"}
enemy = display.newImage(enemypics[math.random (#enemypics)])
enemy:addEventListener ( "tap", shipSmash )
if math.random(2) == 1 then
enemy.x = math.random ( -100, -10 )
else
enemy.x = math.random ( display.contentWidth + 10, display.contentWidth + 100 )
enemy.xScale = -1
end
enemy.y = math.random (display.contentHeight)
enemy.trans = transition.to ( enemy, { x=centerX, y=centerY, time=math.random(2500-speedBump, 4500-speedBump), onComplete=hitBowl } )
speedBump = speedBump + 50
end
function startGame()
text = display.newText( "Tap here to start. Protect the Fruit Bowl!", 0, 0, "orange juice", 24 )
text.x = centerX
text.y = display.contentHeight - 30
text:setFillColor(0, 0, 0)
local function goAway(event)
display.remove(event.target)
text = nil
display.remove(gameTitle)
spawnEnemy()
scoreTxt.alpha = 1
scoreTxt.text = "Score: 0"
score = 0
bowl.numHits = 10
bowl.alpha = 1
speedBump = 0
end
text:addEventListener ( "tap", goAway )
end
local function bowlDamage()
bowl.numHits = bowl.numHits - 2
bowl.alpha = bowl.numHits / 10
if bowl.numHits < 2 then
bowl.alpha = 0
timer.performWithDelay ( 1000, startGame )
--audio.play ( sndLose )
else
local function goAway(obj)
bowl.xScale = 1
bowl.yScale = 1
bowl.alpha = bowl.numHits / 10
end
transition.to ( bowl, { time=200, xScale=1.2, yScale=1.2, alpha=1, onComplete=goAway} )
end
end
function hitBowl(obj)
display.remove( obj )
bowlDamage()
--audio.play(sndBlast)
if bowl.numHits > 1 then
spawnEnemy()
end
end
local function shipSmash(event)
local obj = event.target
display.remove( obj )
--audio.play(sndKill)
transition.cancel ( event.target.trans )
score = score + 5
scoreTxt.text = "Score: " .. score
spawnEnemy()
end
function scene:exitScene(event)
end
function scene:destroyScene(event)
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
So LUA is fun in lots of regards and this is one you'll learn quickly. So I only think this might be the problem, if not it could lead you on the correct trail.
In your top error message you'll see that it gives you this:
...ik/Desktop/_DummyProjekt2/Fruit Fliesv10/level01.lua:51: in function
That level01.lua:51 signifies a line number, so line 51 (or around there). And the lines above it in the error mention addEventListener. Those two things are our clues.
Here's some code from around there:
enemy = display.newImage(enemypics[math.random (#enemypics)])
enemy:addEventListener ( "tap", shipSmash )
if math.random(2) == 1 then
Looks like there's an addEventListener there, one parameter is a string, the other is a function. Where is that function? It's on line 117 which is after line 51 which is most likely the problem. In LUA (and some other languages) you have to declare things in order. So you could do two things. You could move the shipSmash function up above line 51 or you could forward declare that variable. So up in the top part of your file you could do local shipSmash. This assures line 51 that that name exists (what it's trying to check) and then you can declare it later (sometimes I do this for code structure).
Hopefully that helps. I've been using LUA/Corona SDK since about July, I'm still getting used to a lot of the small things like this that I've spent hours ripping my hair out over.

Issue with attempted to index a nil variable in lua / corona sdk

I'm pretty new to lua as you can probably tell by the code, i'm trying to remove an object that says "Stop!" when the time runs out by using an event listener on a button object that's also created when the time runs out. This returns the error attempt to index global 'stopit' (a nil value). I declared it as a local var in the class that adds it to the screen so i'm not sure whats going on. I have organized and tried this several different ways and I can't get it to loop continually without randomly crashing either immediately or after one or two rounds of the game.
Here is the code:
display.setStatusBar(display.HiddenStatusBar)
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local score = 0
local dextime;
local stopit;
local button3;
function newTarget(event)
timer.performWithDelay(100, function() display.remove(target) end)
transition.to(target, {time=99, xScale=.4, yScale=.4})
timer.performWithDelay(101, dexit)
score = score + 10
scoreTxt.text = ("Score:" .. score)
end
function dexit()
stopit = display.newImage("stop.png")
stopit.x = 300
stopit.y = 600
stopit.isVisible = false
button3 = display.newImage("button3.png")
button3:addEventListener("tap", removeitems)
button3.x = centerX
button3.y = centerY
button3.isVisible = false
target = display.newImage("target.png")
target.xScale = .25
target.yScale = .25
target.x = math.random(50, 550)
target.y = math.random(50, 750)
target:addEventListener("tap", newTarget)
local function removeitems(event)
stopit:removeSelf()
button3:removeSelf()
scoreTxt:removeSelf()
timerTxt:removeSelf()
timer.performWithDelay(500, setup)
dextime = 15
score = 0
end
timer.performWithDelay(15000, function() display.remove(target) end)
timer.performWithDelay( 15000, function() button3.isVisible = true end)
timer.performWithDelay(15000, function() stopit.isVisible = true end)
end
local function dexgo()
timer.performWithDelay(1000, function() dextime = dextime - 1 end, 15)
timer.performWithDelay(1001, function() timerTxt.text = ("Time:" .. dextime) end, 15)
dexit()
end
local function one2()
local one = display.newImage("1.png")
one.x = centerX
one.y = centerY
one.alpha = 0
transition.to(one, {time=1000, alpha =1, onComplete=dexgo})
timer.performWithDelay( 1000, function()
display.remove(one)
end, 1)
end
local function two2()
local two = display.newImage("2.png")
two.x = centerX
two.y = centerY
two.alpha = 0
transition.to(two, {time=1000, alpha =1, onComplete=one2})
timer.performWithDelay( 1000, function()
display.remove(two)
end, 1)
end
local function dexMode()
local three = display.newImage("3.png")
three.x = centerX
three.y = centerY
three.alpha = 0
timerTxt = display.newText("Time:" .. dextime,-1, centerX - 440, "Helvetica", 40)
scoreTxt = display.newText( "Score:" .. score, 440, -140, "Helvetica", 40)
display.remove(mode1)
display.remove(mode2)
display.remove(title)
transition.to(three, {time=1000, alpha =1, onComplete=two2})
timer.performWithDelay( 1000, function()
display.remove(three)
end, 1)
bg = nil
title = nil
mode1 = nil
mode2 = nil
end
function listener(event)
simpleMode()
end
function listener2(event)
dexMode()
end
function startGame()
transition.to( title, { time=2000, y=0, alpha=.9, onComplete=showTitle})
transition.to(bg, { time=2000, y=centerY, alpha=1})
transition.to(mode1, { time=2000, x=centerX, alpha=.9})
transition.to(mode2, { time=2000, x=centerX, alpha=.9})
end
function setup()
dextime = 15;
bg = display.newImage("background.png")
bg.yScale = 1.4
bg.alpha = 0
title = display.newImage("title.png")
title.x = centerX
title.y = -200
title.alpha = 0
mode1 = display.newImage("button1.png")
mode1.xScale = 1.23
mode1.yScale = 1.23
mode1.x = 800
mode1.y = 500
mode1.alpha = 0
mode2 = display.newImage("button2.png")
mode2.xScale = 1.23
mode2.yScale = 1.23
mode2.x = -200
mode2.y = 625
mode2.alpha = 0
mode1:addEventListener( "touch", listener )
mode2:addEventListener( "touch", listener2 )
startGame()
end
setup()
The usage of in stopit button3, scoreTxt and timerTxt in function removeitems(event) are globally scoped. When removeitems gets called in dexit it cannot see the local variables you declared in dexit.
The easiest solution is to make removeitems a closure by moving it into dexit:
function dexit()
local stopit = display.newImage("stop.png")
local button3 = display.newImage("button3.png")
local function removeitems(event)
stopit:removeSelf()
button3:removeSelf()
scoreTxt:removeSelf()
timerTxt:removeSelf()
timer.performWithDelay(500, setup)
end
-- ...
end
Try this:
if(stopit~=nil)then
stopit:removeSelf()
end
You are getting this error due to timer and transition. When you are removing an object without canceling timer or transition, it's calling in loop and getting nil value after removing once.
First of all you need to store all the timer and transition in array and release array while stoping game or changing scene. Then Reinitialize array again.
Ex: local timerId = {}
local TransitionID = {}
timerId[#timerId+1] = timer.performWithDelay(15000, function() display.remove(target) end)
TransitionID[#TransitionID+1] = transition.to(two, {time=1000, alpha =1, onComplete=one2})
When removing all the object remove timer and transition first.
for i = 1, #timerId do
timer.cancel(timerId[i])
timerId[i] = nil
timerId = {} //Initializing array
end
for j = 1, #transitionID do
transition.cancel(transitionID[j])
transitionID[j] = nil
transitionID = {}
end

Resources