Error attempt to index local "..."

Now I’m getting this error:

Runtime error
d:\coronasdk\apps\skysoccer\spstart.lua:118: attempt to index local ‘ball’

Do you know how to solve this?

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local background, ball, pauseBtn, resumeBtn, leftWall, rightWall, ceiling, floorr, lock

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 49 )

–physics.setDrawMode(“hybrid”)

function scene:createScene( event )
local screenGroup = self.view

background = display.newImage ( “road.jpg” )
screenGroup:insert( background )

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = display.contentCenterY+240
–ball.myName = “first ball”
screenGroup:insert( ball )

pauseBtn = display.newImageRect ( “pausebtn.gif”, 50, 50 )
pauseBtn.x = 20
pauseBtn.y = 20
screenGroup:insert( pauseBtn )

resumeBtn = display.newImageRect ( “resumebtn.gif”, 50, 50 )
resumeBtn.x = 20
resumeBtn.y = 20
resumeBtn.isVisible = false
screenGroup:insert( resumeBtn )

leftWall = display.newRect( -10, 0, 10, display.contentHeight )
screenGroup:insert( leftWall )
rightWall = display.newRect( display.contentWidth, 0, 10, display.contentHeight )
screenGroup:insert( rightWall )
ceiling = display.newRect( 0, -10, display.contentWidth, 10 )
screenGroup:insert( ceiling )
floorr = display.newRect( 0, 480, display.contentWidth, 10 )
–floorr.myName = “second ball”
screenGroup:insert( floorr )

physics.addBody( leftWall, “static”, { bounce = 0.3 } )
physics.addBody( rightWall, “static”, { bounce = 0.3 } )
physics.addBody( ceiling, “static”, { bounce = 0.3 } )

physics.addBody( ball, { bounce = 0, radius = 40, friction = 1.0 } )

physics.addBody( floorr, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

lock = physics.newJoint( “weld”, leftWall, floorr, floorr.x, floorr.y )
screenGroup:insert( lock )

end

function moveBall( event )
if event.phase == “began” then
local x, y = ball.x-event.x, ball.y-event.y
ball:applyLinearImpulse( x / 30, -2, ball.x, ball.y )
return true
end
end

function pauseGame( event )
–if end of touch event
if(event.phase == “ended”) then
–pause the physics
physics.pause()
–pause the ball movement
ball:removeEventListener( “touch”, moveBall )
–make pause button invisible
pauseBtn.isVisible = false
–make resume button visible
resumeBtn.isVisible = true
– indicates successful touch
return true
end
end

function resumeGame( event )
–if end of touch event
if(event.phase == “ended”) then
–resume physics
physics.start()
– resume the ball movement
ball:addEventListener( “touch”, moveBall )
–make pause button visible
pauseBtn.isVisible = true
–make resume button invisible
resumeBtn.isVisible = false
– indicates successful touch
return true
end
end

–[[local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )

elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end]]–

–[[ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )

floorr.collision = onLocalCollision
floorr:addEventListener( “collision”, floorr )]]–

ball.isFixedRotation = true

ball:addEventListener( “touch”, moveBall )

pauseBtn:addEventListener( “touch”, pauseGame )

resumeBtn:addEventListener( “touch”, resumeGame )

scene:addEventListener( “createScene”, scene )

return scene[/code] [import]uid: 203192 topic_id: 34270 reply_id: 334270[/import]

Hello,
This is a simple scope error. You are trying to manipulate the “ball” (lines 118,120) before you actually call the createScene function where you create the ball. So the ball doesn’t exist when you first try to access it.

And to clarify, while you are properly up-referencing the ball at the start of the file, just saying “local ball” doesn’t actually make it non-nil. So when you try to assign a property like " isFixedRotation" to it, you’re trying to assign that to a nil variable, hence the error.

Brent [import]uid: 200026 topic_id: 34270 reply_id: 136270[/import]

move line 118 to 124 into the bottom of your createscene event. Currently that code is being ran before your createScene event so the ball is nil at that point. [import]uid: 147305 topic_id: 34270 reply_id: 136271[/import]

thanx, that solved the problem [import]uid: 203192 topic_id: 34270 reply_id: 136273[/import]

Hello,
This is a simple scope error. You are trying to manipulate the “ball” (lines 118,120) before you actually call the createScene function where you create the ball. So the ball doesn’t exist when you first try to access it.

And to clarify, while you are properly up-referencing the ball at the start of the file, just saying “local ball” doesn’t actually make it non-nil. So when you try to assign a property like " isFixedRotation" to it, you’re trying to assign that to a nil variable, hence the error.

Brent [import]uid: 200026 topic_id: 34270 reply_id: 136270[/import]

move line 118 to 124 into the bottom of your createscene event. Currently that code is being ran before your createScene event so the ball is nil at that point. [import]uid: 147305 topic_id: 34270 reply_id: 136271[/import]

thanx, that solved the problem [import]uid: 203192 topic_id: 34270 reply_id: 136273[/import]