I am using the following code to run this simple game. It runs as expected the first time when I run it. However, when the game is restarted, at onCollision, it outputs the following error
main.lua: 207: attempt to index upvalue ‘happy’ a nil value
the code is provided below:
[lua]
-- -- a simple game to learn -- --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- hidding the status bar display.setStatusBar(display.HiddenStatusBar) --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- common variables centerX = display.contentCenterX centerY = display.contentCenterY screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight -------------- forward refs local gameTitle local myPic local score = 0 local goodie local createPlayScreen local startGame local GameOn ----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- loading sound volumes local startGameSound = audio.loadSound("sound/startTrack.mp3") local tahaRunSound = audio.loadSound("sound/zoom.wav") ----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function createPlayScreen() local background = display.newImage("background.png") background.width = screenWidth background.height= screenHeight background:scale (1.4,1.4) background.x = centerX background.y = centerY -- audio.play(startGameSound) --audio.play( startGameSound, { loops=-1 } ) local backgroundMusicChannel = audio.play( startGameSound, { loops=-1 } ) myPic = display.newImage( "happy.png") myPic:scale (1,1) myPic.x = centerX myPic.y = centerY + 60 transition.to( background, { time=4000, alpha=1, xScale = 1, yScale =1} ) local function showTitle() gameTitle = display.newImage("gametitle.png") gameTitle.x = centerX gameTitle.y = screenTop +90 gameTitle:scale (1,1) gameTitle.alpha = 0 gameTitle:scale(4, 4) scoreTxt = display.newText( "0", 0, 0, "Arial", 400 ) scoreTxt.x = centerX scoreTxt.y = centerY scoreTxt.alpha = 0.5 scoreTxt: setTextColor (0.56, 0.56, 0.56) transition.to( gameTitle, {time=500, alpha=1, xScale=3, yScale=3} ) startGame() end transition.to( myPic, { time=2000, alpha=1, xScale = 3, yScale =3, rotation = 360, y=centerY, onComplete=showTitle } ) end function startGame() local text = display.newText( "Tap me to start", 0, 0, "Arial", 60 ) text.x = centerX text.y = display.contentHeight -30 text:setTextColor(230, 250, 12) local function goAway(event) display.remove(event.target) audio.stop( ) display.remove(myPic) display.remove(gameTitle) display.remove( text ) GameOn() text = nil return true end function fancy() transition.to ( myPic, {time = 1000, xScale = 5, yScale =5, onComplete = goAway} ) end myPic:addEventListener ( "tap", fancy ) end function GameOn() local physics = require "physics" physics.start() physics.setGravity(0,0) -- SETUP GRAPHICS background = display.newImage("background.png") background.alpha = 0 --background: scale (screenWidth,screenHeight) local happy = display.newImage("happy.png") background.width = screenWidth background.height= screenHeight happy.x = screenTop + 80 happy.y = screenLeft + 80 physics.addBody(happy, "dynamic") happy:scale(2,2) happy.type = "player" local angry = display.newImage("rat.png") angry.x = centerX angry.y = centerY --angry: scale(0.5,0.5) physics.addBody(angry, "static") angry.type = "bad" local angry2 = display.newImage("dragon.png") angry2.x = centerX angry2.y = centerY --angry2: scale(0.5,0.5) physics.addBody(angry2, "static") angry2.type = "bad" local angry3 = display.newImage("dog.png") angry3.x = centerX angry3.y = centerY --angry3: scale(0.5,0.5) physics.addBody(angry3, "static") angry3.type = "bad" local angry4 = display.newImage("bear.png") angry4.x = centerX angry4.y = centerY --angry4: scale(0.5,0.5) physics.addBody(angry4, "static") angry4.type = "bad" local angry5 = display.newImage("monkey.png") angry5.x = centerX angry5.y = centerY angry5: scale(0.5,0.5) physics.addBody(angry5, "static") angry5.type = "bad" local function touchScreen(event) if event.phase == "ended" then transition.to(happy,{time=300, x=event.x, y=event.y}) end -- audio.play(mySnd) end Runtime:addEventListener("touch", touchScreen) local function createGoodies() local goodiePics = {"eyecandy\_1.png","eyecandy\_2.png", "eyecandy\_3.png", "eyecandy\_4.png"} goodie = display.newImage(goodiePics[math.random (#goodiePics)]) goodie.x=math.random(120,screenWidth-100) goodie.y=math.random(120,screenHeight-100) goodie: scale(3,3) physics.addBody(goodie, "static") goodie.type = "goodie" end createGoodies() -- CREATE ANGRY MOVEMENT local function moveAngry() transition.to(angry,{time=1500, x=math.random(80,screenWidth), y=math.random(60,screenHeight), onComplete=moveAngry}) transition.to(angry2,{time=1500, x=math.random(80,screenWidth), y=math.random(60,screenHeight)}) transition.to(angry3,{time=1500, x=math.random(80,screenWidth), y=math.random(60,screenHeight)}) transition.to(angry4,{time=1500, x=math.random(80,screenWidth), y=math.random(60,screenHeight)}) transition.to(angry5,{time=1500, x=math.random(80,screenWidth), y=math.random(60,screenHeight)}) end moveAngry() -- RESPOND TO COLLISIONS local function gameOverAndReset () local text2 = display.newText( "Your Scored " .. score .. " Tap to restart", 0, 0, "Arial",50 ) text2.x = centerX text2.y = centerY text2:setTextColor(0.43, 0.82, 0) happy: removeSelf() happy = nil local function reborn() angry:removeSelf() angry= nil angry2:removeSelf() angry2= nil angry3:removeSelf() angry3= nil angry4:removeSelf() angry4= nil angry5:removeSelf() angry5= nil text2:removeSelf() text2 = nil goodie: removeSelf() goodie = nil ------ createPlayScreen() end text2:addEventListener ( "tap", reborn) end local function onCollision( event) local agro = event.object1 local hit = event.object2 if ( event.phase == "began" ) then if (agro.type == "player" and hit.type == "goodie") then print( agro.type .. hit.type) display.remove (goodie) score = score + 1 scoreTxt.text = score timer.performWithDelay ( 100, createGoodies ) -- display.remove (goodie) else scoreTxt.alpha = 0 -- print( agro.type .. hit.type) gameOverAndReset () end end end Runtime:addEventListener("collision", onCollision) end createPlayScreen()
I know that the code is not written to its best, but my focus is to fix the problem with your help
Please help me fixing this problem and kindly provide some explanation so I can understand why I am getting the error.
Thanks in advance.
Shei7141