I am having a problem with a rotating twinkling star in my game. I rotate the star using code that runs on a timer. I cancel the timer on the function that terminates the game, but it still gives me the error: Corona Simulator[192]: Runtime error /classic1.lua:2118: attempt to perform arithmetic on field ‘rotation’ (a nil value). Even though I remove the timer, it still tries to rotate the star.
This is the code where I create the twinkling star:
[lua]
local function spawnTStar()
local tstar = display.newImage(“TStar1-1.png”)
tstar.x = math.random(0,300)
tstar.y = math.random(0,300)
local function tstarAnimation()
local function GlowAlpha()
tstarGlowing = transition.to(tstar,{time=1500, alpha=0.3, onComplete=starsAnimation})
end
tstarGlowing = transition.to(tstar,{time=1500, alpha=1.0, onComplete=GlowAlpha})
end
tstarGlowing = transition.to(tstar,{time=1500, alpha=0, onComplete=starsAnimation})
local function rotateTStar()
if (tstar ~= nil) then
tstar.rotation = tstar.rotation + 1
end
end
rttmr = timer.performWithDelay(1,rotateTStar,-1)
local function eraseTStar()
display.remove(tstar)
tstar = nil
timer.cancel(rttmr)
end
ettmr = timer.performWithDelay(2200,eraseTStar,1)
local function addScoreTStar(xLoc,yLoc)
local xLoc = xLoc
local yLoc = yLoc
local function displayScoreText(textLine, theX, theY)
local string = textLine
local theX = theX
local theY = theY
local scoreText = display.newText(string, 0, 0, “Apple Casual”, 20)
scoreText:setTextColor( 1, 1, 1, 1 )
scoreText.alpha = 1.0
scoreText.xOrigin = theX
scoreText.yOrigin = theY
localGroup:insert(scoreText)
local function destroyText()
display.remove(scoreText)
scoreText = nil
end
local newY = theY - 30
textAnim = transition.to( scoreText, {time=1200, alpha=0, y=newY, onComplete=destroyText} )
end
score = score + 2
scoreDisplayText.text = score
tstarScore = tstarScore + 1
saveValue(“tstarScore.data”,tstarScore)
print(tstarScore, “: tstarScore”)
displayScoreText( “+2”, xLoc, yLoc)
end
local function addScore(event)
if (event.phase == “ended”) then
addScoreTStar(tstar.x,tstar.y)
display.remove(tstar)
tstar = nil
timer.cancel(rttmr)
end
end
tstar:addEventListener(“touch”, addScore)
return tstar
end
ststmr = timer.performWithDelay(4000, spawnTStar, -1)
[/lua]
This is the function where the game is terminated and I cancel the timer:
[lua]
local function loseGame()
if (spaceshipLives == 2) then
display.remove(spaceship3)
spaceship3 = nil
end
if (spaceshipLives == 1) then
display.remove(spaceship2)
spaceship2 = nil
end
if (spaceshipLives == 0) then
display.remove(spaceship1)
spaceship1 = nil
display.remove(redspaceship)
redspaceship = nil
Runtime:removeEventListener(“enterFrame”, spawnRedSpaceships)
Runtime:removeEventListener(“enterFrame”, loseGame)
timer.cancel(rttmr)
saveValue(“score1.data”, score)
timer.cancel(ststmr)
cstmr = timer.performWithDelay(1000, director:changeScene(“levelslosescreen”, “fade”), 1)
if score > highScore then
highScore = score
saveValue(“highscore1.data”, highScore2)
print(“High Score Received!”)
end
end
end
Runtime:addEventListener(“enterFrame”, loseGame)
highScore = tonumber(loadValue(“highscore1.data”))
[/lua]
If someone can tell me why this error is occurring, that would be great! Thanks a lot.