Hello i had error game.lua:288: attempt to perform arithmetic on field 'y' (a nil value)

Hello the error happening after i out on menu

function scene:create( event )

local sceneGroup = self.view


physics.pause()  -- Temporarily pause the physics engine



backGroup = display.newGroup()  -- Display group for the background image

sceneGroup:insert( backGroup )  -- Insert into the scene's view group

mainGroup = display.newGroup()  -- Display group for the ship, asteroids, lasers, etc.

sceneGroup:insert( mainGroup )  -- Insert into the scene's view group

uiGroup = display.newGroup()    -- Display group for UI objects like the score

sceneGroup:insert( uiGroup )    -- Insert into the scene's view group





display.setStatusBar( display.HiddenStatusBar)



_W = display.contentWidth;

_H = display.contentHeight;

scrollSpeed = 1;

local bg1 = display.newImageRect( backGroup, "background.png", 800, 1400 )

bg1.x = display.contentCenterX

bg1.y = _H/2

local bg2 = display.newImageRect( backGroup, "background.png", 800, 1400 )

bg2.x = display.contentCenterX

bg2.y = bg1.y+1400

local bg3 = display.newImageRect( backGroup, "background.png", 800, 1400 )

bg3.x = display.contentCenterX

bg3.y = bg1.y+1400

local function move(bt)

    bg1.y = bg1.y + scrollSpeed

    bg2.y = bg2.y + scrollSpeed

    bg3.y = bg3.y + scrollSpeed

    if (bg1.y) >2100 then

        bg1:translate( 0, -2800)

       end

       if (bg2.y) > 2100 then

        bg2:translate( 0, -2800 )

       end

       if (bg3.y) > 2100 then

        bg3:translate( 0, -2800 )

       end

end      



Runtime:addEventListener( "enterFrame", move )



ship = display.newImageRect( mainGroup, objectSheet, 4, 98, 79 )

ship.x = display.contentCenterX

ship.y = display.contentHeight - 100

physics.addBody( ship, { radius=30, isSensor=true } )

ship.myName = "ship"

-- Display lives and score

livesText = display.newText( uiGroup, "Lives: " .. lives, 200, 80, native.systemFont, 36 )

scoreText = display.newText( uiGroup, "Score: " .. score, 400, 80, native.systemFont, 36 )



local out = display.newImageRect( sceneGroup, "out.png", 50, 50, native.systemFont, 36 )

out:setFillColor( 0.82, 0.86, 1 )

out.x = 550

out.y = 85

out:addEventListener( "tap", endGame )

ship:addEventListener( "tap", fireLaser )

ship:addEventListener( "touch", dragShip )

explosionSound= audio.loadSound("audio/explosion.wav")

fireSound = audio.loadSound( "audio/fire.wav" )

musicTrack = audio.loadStream( "audio/80s-Space-Game_Looping.wav")

end

What’s going on on line 288 of game.lua?

bg1.y = bg1.y + scrollSpeed
moves the picture across the screen

This might be a scoping issue. Can you make sure bg1 exists before bg1.y = bg1.y + scrollSpeed ?

local bg1 = display.newImageRect( backGroup, “background.png”, 800, 1400 )
yes exist

I saw that and it’s not what I’m saying. Can you make sure your move() function can access bg1? You can do so by simply adding a print (bg1) statement above bg1.y = bg1.y + scrollSpeed.

Try assigning an actual number to bg1.y instead of a variable and see what happens.

if bg1~=nil then
     bg1.y = bg1.y + scrollSpeed
end

stop your enterframe when you leave the scene