MNOT
MNOT

Reputation: 21

Attempt to perform arithmetic on field 'y' (a nil value)

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:

--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 = 730
    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 )

--thank you all

Upvotes: 2

Views: 7272

Answers (2)

A. Denis
A. Denis

Reputation: 562

If you don't want remove listener, you can add checks for nil:

--function move
local function move()
    if (bottiglia ~= nil and bottiglia.y ~= nil) then 
        bottiglia.y = bottiglia.y + 4
    end

    if (background~= nil and background.y ~= nil) then 
        background.y = background.y + 4
    end
end

This is also pretty risky to use such global variables: bottiglia and background.

You can make it little safer if make them as (or something like that):

myGlobalsVars = { }
myGlobalsVars.myGlobalsVars = display.newGroup()
myGlobalsVars.background = display.newGroup()

Upvotes: 0

ldurniat
ldurniat

Reputation: 1702

The Runtime listener (move function) is working all the time. It changes position of bottiglia and background objects but since background does not exist any more you get an error.

A simple solution is to remove the global listener using Runtime:removeEventListener() before you remove the background object.

Use Runtime:removeEventListener("enterFrame", move)

Upvotes: 1

Related Questions