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)