attempt to perform arithmetic on field 'x' (a nil value)

I try to repeat the same scene with a different picture I get the error message below, but when I leave blank in function scene:hide( event ) scene change but previous scene still on the screen

                              ---------------------Error  message-----------------------

C:\Users\game\FR\syllabe\AP\ap11.lua:101: attempt to perform arithmetic on field ‘x’ (a nil value)
stack traceback:
C:\Users\game\FR\syllabe\AP\ap11.lua:101: in function ‘func’
D:\a\corona\corona\platform\resources\init.lua:202: in function <D:\a\corona\corona\platform\resources\init.lua:189>
-----------------------------------------------------------------
someone can help me please !
below the code

local composer = require( “composer” )
local scene = composer.newScene()

local function gotoGame()
composer.gotoScene( “game.AP.ap11” )
end

– create()
function scene:create( event )
local sceneGroup = self.view
------------------------------background----------------------------------------
local anleekran = display.newImageRect( sceneGroup, “image4/anleekran.png”, 765, 1370 )
anleekran.x = display.contentCenterX
anleekran.y = display.contentCenterY
–anleekran.alpha=0.7

local tPrevious = system.getTimer()

-------------- Set up physics engine---------------
local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 9.8 )
physics.setDrawMode( “normal” )

---------------- Declare initial variables ---------------------------
local letterboxWidth = math.abs(display.screenOriginX)
local letterboxHeight = math.abs(display.screenOriginY)

---------------- Create wall around the screen--------------------------

local wallL = display.newRect( sceneGroup, 470-letterboxWidth, display.contentCenterY, 140, display.actualContentHeight )
wallL.anchorX = 1
wallL.isVisible = false
physics.addBody( wallL, “static”, { bounce=1, friction=0.1 } )

local wallR = display.newRect( sceneGroup, 160+letterboxWidth, display.contentCenterY, 140, display.actualContentHeight )
wallR.anchorX = 0
wallR.isVisible = false
physics.addBody( wallR, “static”, { bounce=1, friction=0.1 } )

local wallT = display.newRect( sceneGroup, display.contentCenterX, 1350-letterboxHeight, display.actualContentWidth, 20 )
wallT.anchorY = 1
wallT.isVisible = false
physics.addBody( wallT, “static”, { bounce=0, friction=0 } )

local wallB = display.newRect( sceneGroup, display.contentCenterX, -350+letterboxHeight, display.actualContentWidth, 20 )
wallB.anchorY = 0
wallB.isVisible = false
physics.addBody( wallB, “static”, { bounce=0.4, friction=0.6 } )

– Create ball (character)
local jumper = display.newImageRect( sceneGroup, “image/image1/boulviolet.png”, 100, 100 )
physics.addBody( jumper, “dynamic”, { radius=1, bounce=0.3, friction=0.1 } )
jumper.x = 308 ; jumper.y = 70
jumper.alpha = 0.7

------------------------------------Create platforms-----------------------------

local bgap = display.newImageRect( sceneGroup, “image/image1/joyau6.png”, 150, 160 )
bgap.x=2000
bgap.y=820
physics.addBody( bgap, “static”, { bounce=0.3, friction=0.1 } )
bgap.collType=“gap”

local gap = display.newText(sceneGroup, “gap”, 1000, 820, native.systemFontBold, 65, 1 )
gap:setFillColor( 0, 0, 0)

local moon = display.newImage( sceneGroup, “test/soleil.png”, 22, 19 )


– Frame (runtime) listener to move objects
local function move( event )
– Move scene objects
local tDelta = event.time - tPrevious
tPrevious = event.time
local xOffset = ( 0.1tDelta )
local yOffset = ( 0.1
tDelta )

local xOffset4 = ( 0.5*tDelta )


moon.x = moon.x + xOffset0.02
moon.y = moon.y + yOffset
0.02


  gap.x = gap.x - xOffset4 *0.4
 bgap.x = gap.x - xOffset4 *0.4

– Move objects to other side of scene if they pass a horizontal position

if moon.x > 680 + moon.width/2 then
moon:translate ( -6802 , -6802)
end

if gap.x < -280 then
gap:translate( 5000, 0 )
end
-------------------

if bgap.x < -280 then
bgap:translate( 2000, 0 )
end

end
– Start runtime frame listener
Runtime:addEventListener( “enterFrame”, move )

local function onCollision( self, event )

local collideObject = event.other

if ( collideObject.collType ==“gap” ) then

 gotoGame()

end
end

jumper.collision = onCollision
jumper:addEventListener( “collision” )

local function doJump( event )

if ( "began" == event.phase ) then

	jumper:applyLinearImpulse( nil, 200/200,  jumper.x, jumper.y )

end
return true

end

– Create an invisible touch-sensitive rectangle to handle screen touches
local touchRect = display.newRect( sceneGroup, 50, -200, 1460, 1600, 400 )
touchRect.isVisible = false
touchRect.isHitTestable = true
touchRect.anchorY = 0

touchRect:addEventListener( “touch”, doJump )

end
– show()
function scene:show( event )

local sceneGroup = self.view

local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == “did” ) then
– Code here runs when the scene is entirely on screen
audio.play( DeFond, —
{ channel=1, ----channel=1 instructs the audio library to explicitly play the music on channel 1
loops=-1 } —loops=-1 tells the audio system to repeat (loop) the file indefinitely.
) -----

end

end

– hide()
function scene:hide( event )

local sceneGroup = self.view
local phase = event.phase

if ( phase == "will" ) then
	-- Code here runs when the scene is on screen (but is about to go off screen)

Runtime:removeEventListener( “enterFrame”, move )

composer.removeScene( “game.FR.syllabe.AP.ap1” )

elseif ( phase == "did" ) then
	-- Code here runs immediately after the scene goes entirely off screen

audio.stop( 1 )

composer.removeScene( “game.AP.ap1” )

end

end

– destroy()
function scene:destroy( event )
local sceneGroup = self.view
– Code here runs prior to the removal of scene’s view
–audio.dispose( DeFond )
–composer.removeScene( “game.AP.ap1” )

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene

That’s quite a bit to digest. :slightly_smiling_face:

I’m not sure if something got lost with the copy and paste, but here’s the first thing I see…
Your “move” function listener is created as “local” inside scene:create function, which means that Runtime:removeEventListener( “enterFrame”, move ) in your scene:hide will not work, and it’s not removed at all.

Your best option is to restructure the code a bit; declare your moon, gap, and bgap variables outside of the create function since these are the ones accessed in the move loop. Follow that by creating your move function there, like so:

local function gotoGame()
	composer.gotoScene( "game.AP.ap11" )
end

local moon, gap, bgap, tPrevious

-- Frame (runtime) listener to move objects
local function move( event )
	-- Move scene objects
	local tDelta = event.time - tPrevious
	tPrevious = event.time
	local xOffset = ( 0.1tDelta )
	local yOffset = ( 0.1tDelta )

	local xOffset4 = ( 0.5*tDelta )

	moon.x = moon.x + xOffset0.02
	moon.y = moon.y + yOffset0.02

	  gap.x = gap.x - xOffset4 *0.4
	 bgap.x = gap.x - xOffset4 *0.4
	--Move objects to other side of scene if they pass a horizontal position
	if moon.x > 680 + moon.width/2 then
	moon:translate ( -6802 , -6802)
	end

	if gap.x < -280 then
	gap:translate( 5000, 0 )
	end
-------------------

	if bgap.x < -280 then
	bgap:translate( 2000, 0 )
	end

end

Also not sure what “xOffset0.02” does in the above code, but again, maybe something got lost in the copy and paste?

Then, in your create function you don’t need to use “local” for the variables declared above as they are already declared as local, and should look like this:

bgap = display.newImageRect( sceneGroup, "image/image1/joyau6.png", 150, 160 )
bgap.x=2000
bgap.y=820
physics.addBody( bgap, "static", { bounce=0.3, friction=0.1 } )
bgap.collType="gap"

gap = display.newText(sceneGroup, "gap", 1000, 820, native.systemFontBold, 65, 1 )
gap:setFillColor( 0, 0, 0)

moon = display.newImage( sceneGroup, "test/soleil.png", 22, 19 )

I would also not add the event listener inside your create function, instead add it in scene:show, like so:

function scene:show( event )

	local sceneGroup = self.view
	local phase = event.phase
	if ( phase == "will" ) then
	-- Code here runs when the scene is still off screen (but is about to come on screen)
	elseif ( phase == "did" ) then
	-- Code here runs when the scene is entirely on screen
		audio.play( DeFond, { channel=1, ----channel=1 instructs the audio library to explicitly play the music on channel 1
		loops=-1 } --loops=-1 tells the audio system to repeat (loop) the file indefinitely.
		)
		
		-- Start runtime frame listener
		Runtime:addEventListener( "enterFrame", move )
	end
end

Hope that helps!

Thanks for your reply
I am inspired of your reply to restructure the scene, I make only 2 scenes, one for play game and the other one to refresh the first scene. instead repeated the scenes I let object repeated by themselves inside the scene. Now the big problem it is how to exit the scene. for that , I create 2 functions gotoGame and Gotorefresh. The codes works perfectly on simulator but sometime it’s not working on device. it create a random trouble on device sincerely I can’t describe ( freeze or not responding or …) but not all at time.
What I am bad ?

local composer = require( “composer” )
local scene = composer.newScene()

local function gotorefresh()
Runtime:removeEventListener( “enterFrame”, move )
composer.gotoScene( “game.FR.syllabe1.AV.av” )
end


local function gotomenu()
Runtime:removeEventListener( “enterFrame”, move )
composer.gotoScene( “menuRequest” )-------fonction Parametre

end
local DeFond
audio.setVolume( 0.15, { channel=1 } )

DeFond = audio.loadStream( “audio/oiseau.ogg” ) --------produire le son de depart du programme

local FLECHE = audio.loadSound( “audio/flame.ogg” ) ------- produire le son de C

– create()
function scene:create( event )
local sceneGroup = self.view

local anleekran = display.newImageRect( sceneGroup, “image4/anleekran.png”, 765, 1370 )
anleekran.x = display.contentCenterX
anleekran.y = display.contentCenterY
–anleekran.alpha=0.7
--------------le bas d’ecran ---------
local anbaekran = display.newImageRect( sceneGroup, “image/chien/anleekran.png”, 765, 410 )
anbaekran.x = display.contentCenterX
anbaekran.y = display.contentHeight +80

-------------------------Menu Parametre-----------------
local menu = display.newImageRect( sceneGroup, “image/settings.png”, 80, 80 )
menu.x = 710
menu.y = -50
menu.alpha=0.1
menu:addEventListener( “tap”, gotomenu) — fonction Parametre

local refresh = display.newImageRect( sceneGroup, “image/refresh.png”, 80, 80 )
refresh.x = 80
refresh.y = -50
–refresh.alpha=0.1
refresh:addEventListener( “tap”, gotorefresh) — fonction Parametre

– Set up physics engine

physics = require( “physics” )
physics.start()
physics.setGravity( 0, 9.8 )
physics.setDrawMode( “normal” )

– Declare initial variables

letterboxWidth = math.abs(display.screenOriginX)
letterboxHeight = math.abs(display.screenOriginY)

– Créer des murs autour de l’écran pour empecher a la boule de disparaitre de l’écran

wallL = display.newRect( sceneGroup, 470-letterboxWidth, display.contentCenterY, 140, display.actualContentHeight )
wallL.anchorX = 1
wallL.isVisible = false
physics.addBody( wallL, “static”, { bounce=1, friction=0.1 } )

wallR = display.newRect( sceneGroup, 160+letterboxWidth, display.contentCenterY, 140, display.actualContentHeight )
wallR.anchorX = 0
wallR.isVisible = false
physics.addBody( wallR, “static”, { bounce=1, friction=0.1 } )

wallT = display.newRect( sceneGroup, display.contentCenterX, 1250-letterboxHeight, display.actualContentWidth, 20 )
wallT.anchorY = 1
wallT.isVisible = false
physics.addBody( wallT, “static”, { bounce=0, friction=0 } )

wallB = display.newRect( sceneGroup, display.contentCenterX, -350+letterboxHeight, display.actualContentWidth, 20 )
wallB.anchorY = 0
wallB.isVisible = false
physics.addBody( wallB, “static”, { bounce=0.4, friction=0.6 } )

– Create ball (character)

jumper = display.newImageRect( sceneGroup, “image/image1/boulviolet.png”, 100, 100 )
physics.addBody( jumper, “dynamic”, { radius=1, bounce=0.3, friction=0.1 } )
jumper.x = 308 ; jumper.y = 70
jumper.alpha = 0.7


– Create platforms

bbap = display.newImageRect( sceneGroup, “image/image1/joyau1.png”, 150, 160 )
bbap.x=-50
bbap.y=920
physics.addBody( bbap, “static”, { bounce=0.3, friction=0.1 } )
bbap.collType=“bap”

bcap = display.newImageRect( sceneGroup, “image/image1/joyau5.png”, 150, 160 )
bcap.x=-200
bcap.y=680
physics.addBody( bcap, “static”, { bounce=0.3, friction=0.1 } )
bcap.collType=“cap”

bap = display.newText(sceneGroup, “bav”, -50, 920, native.systemFontBold, 65, 1 )
bap:setFillColor( 0, 0, 0)
cap = display.newText(sceneGroup, “cav”, -200, 680, native.systemFontBold, 65, 1 )
cap:setFillColor( 0, 0, 0)

-----------------------------------------------------------------------------------

moon = display.newImage( sceneGroup, “test/soleil.png”, 22, 19 )

nuage2 = display.newImage( sceneGroup, “image4/cloud.png”,-50, 292 )
nuage1 = display.newImage( sceneGroup, “image4/cloud.png”,650-48, 292 )

mont = display.newImage( sceneGroup, “test/mountain_big.png”,0, 892 )
mont2 = display.newImage( sceneGroup, “test/mountain_big.png”, 480, 892 )

mountain_sma = display.newImage( sceneGroup, “test/mountain_small.png”, -284, 890 )
mountain_sma2 = display.newImage( sceneGroup, “test/mountain_small.png”, -284-480, 890 )

tPrevious = system.getTimer()


– Frame (runtime) listener to move objects
function move( event )
– Move scene objects
tDelta = event.time - tPrevious
tPrevious = event.time
xOffset = ( 0.14tDelta )
yOffset = ( 0.14
tDelta )


moon.x = moon.x + xOffset0.02
moon.y = moon.y + yOffset
0.02

nuage1.x = nuage1.x + xOffset0.05
nuage2.x = nuage2.x + xOffset
0.05


bap.x = bap.x - xOffset*1
cap.x = cap.x - xOffset

bbap.x = bbap.x - xOffset*1
bcap.x = bcap.x - xOffset


– Move objects to other side of scene if they pass a horizontal position

if moon.x > 680 + moon.width/2 then
moon:translate ( -6802 , -6802)
end

if nuage1.x > 780 + nuage1.width/2 then
nuage1:translate( -7802, 0 )
end
if nuage2.x > 780 + nuage2.width/2 then
nuage2:translate( -780
2, 0 )
end


if bap.x < -80 then
bap:translate( 1500, 0 )
end

if cap.x < -50 then
cap:translate( 1000, 0 )
end

                -------------------

if bbap.x < -80 then
bbap:translate( 1500, 0 )
end

if bcap.x < -40 then
bcap:translate( 1000, 0 )
end

end

function onCollision( self, event )

 collideObject = event.other

if ( collideObject.collType ==“bap” ) then

---------I remove the event under this function to shortly the post

elseif ( collideObject.collType ==“cap” ) then

---------I remove the event under this function to shortly the post

end
end

jumper.collision = onCollision
jumper:addEventListener( “collision” )

function doJump( event )

if ( "began" == event.phase ) then

	jumper:applyLinearImpulse( nil, 200/200,  jumper.x, jumper.y )

audio.play(FLECHE)
end
return true
end

– Create an invisible touch-sensitive rectangle to handle screen touches

touchRect = display.newRect( sceneGroup, 50, -200, 1460, 1600, 400 )
touchRect.isVisible = false
touchRect.isHitTestable = true
touchRect.anchorY = 0

touchRect:addEventListener( “touch”, doJump )

end
– show()
function scene:show( event )

local sceneGroup = self.view

local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == “did” ) then
– Code here runs when the scene is entirely on screen
audio.play( DeFond, —cette commande fait demarrer le son de depart
{ channel=1, ----channel=1 instructs the audio library to explicitly play the music on channel 1
loops=-1 } —loops=-1 tells the audio system to repeat (loop) the file indefinitely.
) -----fin de la commande du son de depart
Runtime:addEventListener( “enterFrame”, move )

end

end

– hide()
function scene:hide( event )

local sceneGroup = self.view
local phase = event.phase

if ( phase == "will" ) then
	-- Code here runs when the scene is on screen (but is about to go off screen)

Runtime:removeEventListener( “enterFrame”, move )

composer.removeScene( “game.FR.syllabe1.AV.av1” )

elseif ( phase == "did" ) then
	-- Code here runs immediately after the scene goes entirely off screen

audio.stop( 1 )

end

end

– destroy()
function scene:destroy( event )
local sceneGroup = self.view
– Code here runs prior to the removal of scene’s view
–audio.dispose( DeFond )

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene

  1. When posting code to the forums, please format it correctly so that it can be easily read.
  2. Please do not dump all of your code here and ask others to “fix it”. Try to keep your questions clear and to the point. Make it easier for others to help you.

Well, at least it’s a little better now. :slightly_smiling_face:

@steevemoore, some of your variables are global, that “might” cause issues. As mentioned before, it’s better to declare your variables ‘local’ on top of your file (similar to DeFond and FLECHE), you don’t necessarily need to give them a value at this point, just declare them as empty variables to be used in this scene.

But I think the real problem is actually the physics.

  1. When you’re transitioning to a new scene you should pause/stop the physics engine.
  2. You should destroy all physics body before going to next scene as they are not automatically destroyed.

oh !
I see thank you