Any way to solve this problem?

I’ve a project where there are numbers at bottom which should be destroyed when collided with ball.When you tap on rect, rect fires a ball on number.The collision works. But I want to add some twist in game. I want those numbers to be increasing by 10 with time. I have written a code assuming to be working but its not. It shows an error .Its a nil value traceback.

So have you any idea to increase these numbers and to remove when hit by ball??

The code I tried…

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require("physics") physics.start() local score = 0 -- code to increase numbers---- local function num(self, event) if self.surfaceType == "scoreText" then score = score + 10 scoreText.text = "" .. score end end numTimer = timer.performWithDelay(1000,num,0) --- code to detect collision and remove numbers---- local function onCollisionA(self,event) local collideObject = event.other if ( collideObject.surfaceType == "ball" ) then self:removeSelf() end end ---- code to display numbers randomly----- local function scoregameloop() local rand = math.random(0,1) if (rand == 0) then local scoreText = display.newText(""..score,100,350,native.systemFontBold, 30) scoreText:setFillColor (1,0,2 ) physics.addBody( scoreText, "static" ) scoreText.surfaceType = "scoreText" scoreText.collision = onCollisionA scoreText:addEventListener("collision") scoreText.enterFrame = num Runtime:addEventListener("enterFrame",scoreText) elseif (rand == 1) then local scoreText = display.newText(""..score,200,350,native.systemFontBold, 30) scoreText:setFillColor (1,0,2 ) physics.addBody( scoreText, "static" ) scoreText.surfaceType = "scoreText" scoreText.collision = onCollisionA scoreText:addEventListener("collision") scoreText.enterFrame = num Runtime:addEventListener("enterFrame",scoreText) end end scoreloopTimer = timer.performWithDelay(1000,scoregameloop,0) -----rects----- local myRectangle = display.newRect( 100, 100, 50, 50 ) myRectangle.strokeWidth = 3 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 1, 0, 0 ) local myRectangle2 = display.newRect( 200, 100, 50, 50 ) myRectangle2.strokeWidth = 3 myRectangle2:setFillColor( 0.5 ) myRectangle2:setStrokeColor( 1, 0, 0 ) ----functions to fire ball----- local function fire() local myCircle = display.newCircle( 100, 100, 10 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) physics.addBody(myCircle, "dynamic" ) myCircle:applyLinearImpulse( 0, 0.15, myCircle.x, myCircle.y ) myCircle.surfaceType = "ball" myCircle.collision = onCollisionA myCircle:addEventListener("collision") end local function fire2() local myCircle = display.newCircle( 200, 100, 10 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) physics.addBody(myCircle, "dynamic" ) myCircle:applyLinearImpulse( 0, 0.15, myCircle.x, myCircle.y ) myCircle.surfaceType = "ball" myCircle.collision = onCollisionA myCircle:addEventListener("collision") end myRectangle:addEventListener( "tap", fire ) myRectangle2:addEventListener( "tap", fire2)

Try adding a runtime listener to the increase numbers function.

That is also not working…

We really need to know the exact error and line number.

Try 

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require("physics") physics.start() local score = 0 -- code to increase numbers---- local function increaseScore( self, event )         if ( self.surfaceType == "scoreText" ) then  self.score = self.score + 10  self.text = "" .. self.score end --[[-- use to manually stop timer if ( self.score \>= 100 ) then   timer.cancel( event.source ) end --]] end --- code to detect collision and remove numbers----  local function onCollisionA(self,event)    local collideObject = event.other    if ( collideObject.surfaceType == "ball" ) then    self:removeSelf()      end    end     ---- code to display numbers randomly----- local function scoregameloop() local r = math.random(0,1) local scoreText  = display.newText(""..score,100 + r \* 100,350,native.systemFontBold, 30) scoreText:setFillColor (1,0,2 ) physics.addBody( scoreText, "static" ) scoreText.surfaceType = "scoreText" scoreText.collision = onCollisionA scoreText:addEventListener("collision") scoreText.timer = increaseScore scoreText.score = 0 function scoreText:finalize() -- On remove, cleanup instance if scoreText and scoreText.timerId then timer.cancel( scoreText.timerId ) end end scoreText.timerId = timer.performWithDelay( 1000, scoreText, -1 ) end scoreloopTimer = timer.performWithDelay(1000,scoregameloop, 1) -----rects----- local myRectangle = display.newRect( 100, 100, 50, 50 ) myRectangle.strokeWidth = 3 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 1, 0, 0 ) local myRectangle2 = display.newRect( 200, 100, 50, 50 ) myRectangle2.strokeWidth = 3 myRectangle2:setFillColor( 0.5 ) myRectangle2:setStrokeColor( 1, 0, 0 ) ----functions to fire ball----- local function fire() local myCircle = display.newCircle( 100, 100, 10 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) physics.addBody(myCircle, "dynamic" ) myCircle:applyLinearImpulse( 0, 0.15, myCircle.x, myCircle.y ) myCircle.surfaceType = "ball" myCircle.collision = onCollisionA myCircle:addEventListener("collision") end local function fire2() local myCircle = display.newCircle( 200, 100, 10 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) physics.addBody(myCircle, "dynamic" ) myCircle:applyLinearImpulse( 0, 0.15, myCircle.x, myCircle.y ) myCircle.surfaceType = "ball" myCircle.collision = onCollisionA myCircle:addEventListener("collision") end myRectangle:addEventListener( "tap", fire ) myRectangle2:addEventListener( "tap", fire2)

Note: With scoreloopTimer = timer.performWithDelay(1000,scoregameloop, 0) you create newText object on top previuos one so they overlap. This is not nice. 

Thanks. And as you pointed the overlapping the numbers, I will move the numbers downwards. I have that functionality written but I didn’t mention it here because I thought that extend the code and confuse to read.

Try adding a runtime listener to the increase numbers function.

That is also not working…

We really need to know the exact error and line number.

Try 

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require("physics") physics.start() local score = 0 -- code to increase numbers---- local function increaseScore( self, event )         if ( self.surfaceType == "scoreText" ) then  self.score = self.score + 10  self.text = "" .. self.score end --[[-- use to manually stop timer if ( self.score \>= 100 ) then   timer.cancel( event.source ) end --]] end --- code to detect collision and remove numbers----  local function onCollisionA(self,event)    local collideObject = event.other    if ( collideObject.surfaceType == "ball" ) then    self:removeSelf()      end    end     ---- code to display numbers randomly----- local function scoregameloop() local r = math.random(0,1) local scoreText  = display.newText(""..score,100 + r \* 100,350,native.systemFontBold, 30) scoreText:setFillColor (1,0,2 ) physics.addBody( scoreText, "static" ) scoreText.surfaceType = "scoreText" scoreText.collision = onCollisionA scoreText:addEventListener("collision") scoreText.timer = increaseScore scoreText.score = 0 function scoreText:finalize() -- On remove, cleanup instance if scoreText and scoreText.timerId then timer.cancel( scoreText.timerId ) end end scoreText.timerId = timer.performWithDelay( 1000, scoreText, -1 ) end scoreloopTimer = timer.performWithDelay(1000,scoregameloop, 1) -----rects----- local myRectangle = display.newRect( 100, 100, 50, 50 ) myRectangle.strokeWidth = 3 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 1, 0, 0 ) local myRectangle2 = display.newRect( 200, 100, 50, 50 ) myRectangle2.strokeWidth = 3 myRectangle2:setFillColor( 0.5 ) myRectangle2:setStrokeColor( 1, 0, 0 ) ----functions to fire ball----- local function fire() local myCircle = display.newCircle( 100, 100, 10 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) physics.addBody(myCircle, "dynamic" ) myCircle:applyLinearImpulse( 0, 0.15, myCircle.x, myCircle.y ) myCircle.surfaceType = "ball" myCircle.collision = onCollisionA myCircle:addEventListener("collision") end local function fire2() local myCircle = display.newCircle( 200, 100, 10 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) physics.addBody(myCircle, "dynamic" ) myCircle:applyLinearImpulse( 0, 0.15, myCircle.x, myCircle.y ) myCircle.surfaceType = "ball" myCircle.collision = onCollisionA myCircle:addEventListener("collision") end myRectangle:addEventListener( "tap", fire ) myRectangle2:addEventListener( "tap", fire2)

Note: With scoreloopTimer = timer.performWithDelay(1000,scoregameloop, 0) you create newText object on top previuos one so they overlap. This is not nice. 

Thanks. And as you pointed the overlapping the numbers, I will move the numbers downwards. I have that functionality written but I didn’t mention it here because I thought that extend the code and confuse to read.