I’m new here and I’m struggling with this error in Corona, when game ends (lives=0) and I try to remove the background (that is moving with a function “move”):
“Attempt to perform arithmetic on field ‘y’ (a nil value)” in line “background.y = background.y + 4”
Is there anybody who can explain me what is the mistake?
THE CODE:
[lua]
–add PHYSICS
local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 0 )
local lives = 1
local died = false
–######################################################################
–add background
background = display.newImageRect( “background.png”, 800, 14000 )
background.x = display.contentCenterX
background.y = display.contentCenterY
background.myName = “background”
–add bottle
bottiglia = display.newImageRect( “bottiglia.png”, 41, 104 )
physics.addBody( bottiglia, “dynamic”, { radius=45, bounce=0.5 } )
bottiglia.x = display.contentCenterX
bottiglia.y = 10
bottiglia.myName = “bottiglia”
–function move
local function move()
bottiglia.y = bottiglia.y + 4
background.y = background.y + 4
end
Runtime:addEventListener( “enterFrame”, move )
–######################################################################
–add player
studente = display.newImageRect( “studente.png”, 98, 79 )
studente.x = display.contentCenterX
studente.y = display.contentHeight - 100
physics.addBody( studente, { radius=40, isSensor=true } )
studente.myName = “studente”
–######################################################################
–function collision
local function onCollision( event )
if ( event.phase == “began” ) then
local obj1 = event.object1
local obj2 = event.object2
if ( ( obj1.myName == “studente” and obj2.myName == “bottiglia” ) or
( obj1.myName == “bottiglia” and obj2.myName == “studente” ) )
then
if ( died == false ) then
died = true
– lives update
lives = lives - 1
livesText.text = "Lives: " … lives
if ( lives == 0 ) then
display.remove( studente )
display.remove( background)
timer.performWithDelay( 100, endGame )
end
else
studente.alpha = 0
timer.performWithDelay( 500, restoreStudente )
end
end
end
end
Runtime:addEventListener( “collision”, onCollision )
livesText = display.newText( "Lives: " … lives, 200, 80, native.systemFont, 36 )
[/lua]