Hi everyone,
I’m having a problem during execution of my game: the error message is “attempt to index field ‘other’ (a nil value)” that happens when there is a collision between the ball and the brick that is handled by the function removeBrick.
I removed some parts of code like variables declaration, and small function to load/save tables.
Can someone help me? Thanks.
function scene:create(event) sceneGroup = self.view print("game: create event") end function scene:show(event) sceneGroup = self.view print("game: show event") buildScene() loadSettings() buildLevel(gameSettings.currentLevel) end function scene:hide() print("game: hide event") end function scene:destroy() print("game: destroy event") end scene:addEventListener("create",scene) scene:addEventListener("show",scene) scene:addEventListener("hide",scene) scene:addEventListener("destroy",scene) function buildScene() print("building scene...") addBackground() addPaddleAndBall() setPaddleAndBall() -- function to set at start position paddle and ball (in case of restart or death) addUi() sceneGroup:insert(mainGroup); sceneGroup:insert(uiGroup); sceneGroup:insert(bricksGroup); sceneGroup:insert(alertGroup); sceneGroup:insert(pauseGroup) print("scene ready, tap listener up on background") end function addBackground() background = display.newImageRect("resources/background.png",W,H) background.x = display.contentCenterX; background.y = display.contentCenterY mainGroup:insert(background) print("add background") end function addPaddleAndBall() paddle = display.newImageRect("resources/paddle.png",200,50) ball = display.newImageRect("resources/ball.png",50,50) ball.name = "ball"; paddle.name = "paddle" mainGroup:insert(paddle); mainGroup:insert(ball) physics.addBody(paddle, "static",{density = 1, friction = 0, bounce = 0}) physics.addBody(ball, "dynamic", {density = 1, friction = 0, bounce = 0}) print("added paddle and ball and loaded on physics") end function setPaddleAndBall() paddle.x = W/2; paddle.y = 1660 ball.x = W/2; ball.y = 1600 background:addEventListener("tap",startGame) print("set paddle and ball") end function addUi() scoreText = display.newText("Score:",200,150,font,40) scoreNumber = display.newText(score,400,150,font,40) livesText = display.newText("Lives:",W-300,150,font,40) livesNumber = display.newText(lives,W-100,150,font,40) addPauseBtn() uiGroup:insert(scoreText); uiGroup:insert(scoreNumber); uiGroup:insert(livesText); uiGroup:insert(livesNumber); uiGroup:insert(pauseBtn) uiGroup:toFront() print("add ui elements") end function buildLevel(levelNumber) local length = table.maxn(levels[levelNumber]) bricksGroup:toFront() for i = 1, length do for j = 1, bricks\_row\_length do if(levels[levelNumber][i][j] == 1) then insertBrick(levels[levelNumber][i][j],i,j) end end end end function insertBrick(type,i,j) brick = display.newImageRect("resources/0.png",brick\_width,brick\_height) brick.name = "brick" brick.x = 100 + brick\_width \* j brick.y = 400 + brick\_height \* i physics.addBody(brick, "static", {density = 1, friction = 0, bounce = 0}) bricksGroup:insert(brick) end function startGame() print("starting game") background:removeEventListener("tap", startGame) gameListeners("add") end function bounce(event) ySpeed = -5 if((ball.x + ball.width \* 0.5) \< paddle.x) then -- left side of paddle \>\> bounce to left xSpeed = -5 elseif((ball.x + ball.width \* 0.5) \>= paddle.x) then -- right side of paddle \>\> bounce to right xSpeed = 5 end end function ballUpdate(event) ball.x = ball.x + xSpeed ball.y = ball.y + ySpeed if(ball.x \< 0) then ball.x = ball.x + 3 xSpeed = -xSpeed end --Left if((ball.x + ball.width) \> display.contentWidth) then ball.x = ball.x - 3 xSpeed = -xSpeed end --Right if(ball.y \< 200) then ySpeed = -ySpeed end --Up if(ball.y + ball.height \> paddle.y + paddle.height) then ballDown() end --down/lose end function movePaddle(event) if event.phase == "began" then moveX = event.x - paddle.x; elseif event.phase == "moved" then paddle.x = event.x - moveX; end if((paddle.x - paddle.width \* 0.5) \< 0) then paddle.x = paddle.width \* 0.5; elseif((paddle.x + paddle.width \* 0.5) \> display.contentWidth) then paddle.x = display.contentWidth - paddle.width \* 0.5; end end function ballDown() updateLives() if lives == 0 then lose() else xSpeed = 0 ySpeed = 0 setPaddleAndBall() end end function updateLives() lives = lives - 1 livesNumber.text = lives livesNumber.anchorX = 0 livesNumber.x = W-100 end function removeBrick(event) -- Check the which side of the brick the ball hits, left, right if event.other.name == "brick" and ball.x + ball.width \* 0.5 \< event.other.x + event.other.width \* 0.5 then xSpeed = -xSpeed elseif event.other.name == "brick" and ball.x + ball.width \* 0.5 \>= event.other.x + event.other.width \* 0.5 then xSpeed = xSpeed end -- Bounce, Remove if event.other.name == "brick" then ySpeed = ySpeed \* -1 event.other:removeSelf() event.other = nil bricksGroup.numChildren = bricksGroup.numChildren - 1 end -- Check if all bricks are destroyed if bricksGroup.numChildren \< 0 then --alertScreen("YOU WIN!", "Continue") --gameEvent = "win" end end function gameListeners(action) if(action == 'add') then Runtime:addEventListener('enterFrame', ballUpdate) paddle:addEventListener('collision', bounce) paddle:addEventListener("touch",movePaddle) ball:addEventListener('collision', removeBrick) else Runtime:removeEventListener('enterFrame', ballUpdate) paddle:removeEventListener('collision', bounce) ball:removeEventListener('collision', removeBrick) paddle:removeEventListener("touch",movePaddle) end end
I don’t personally do anything with the values, but you do. 