Hi,
I want that when the ball collision with any rectangle, THESE rectangle self delete but I not get detect collision and I don’t know the code for self delete these rectangle, can be self.remove?
Please help, see the code and error I saw the API Docs but Im lost… :(
Thanks in advance
The code is this:
local paddle\_width = 100 local ball local ball2 local rectangle local physics = require('physics') physics.start( ) physics.setScale( 10 ) physics.setGravity( 0, 0 ) local function moveRandomly() --cuadrado rectangle:setLinearVelocity(math.random(-90,300), math.random(-300,300)); end local function generar\_rectangulos() rectangle = display.newRect(100, 300, 20, 20) physics.addBody(rectangle, {bounce = 0.9, radius = 25, friction = 1.0 }) rectangle.myName = "rectangle" end timer.performWithDelay(500, moveRandomly, -1); local endzone1 = display.newRect( display.contentWidth/2, display.contentHeight-100, display.contentWidth, 200 ) endzone1.alpha = 0.5 local endzone2 = display.newRect( display.contentWidth/2, 100, display.contentWidth, 200 ) endzone2.alpha = 0.5 local player\_wall\_1 = display.newRect( display.contentWidth/2, 2.5+200, display.contentWidth, 5 ) physics.addBody( player\_wall\_1, "static", {density = 1, friction = 0, bounce = 1, isSensor = false}) local goal1 = display.newRect( display.contentWidth/2, 2.5+200, 0, 5 ) physics.addBody( goal1, "static", {density = 1, friction = 0, bounce = 1, isSensor = false}) goal1:setFillColor( 0, 0, 0 ) local player\_wall\_2 = display.newRect( display.contentWidth/2, display.contentHeight-2.5-200, display.contentWidth, 5) physics.addBody( player\_wall\_2, "static", {density = 1, friction = 0, bounce = 1, isSensor = false}) local goal2 = display.newRect( display.contentWidth/2, display.contentHeight-2.5-200, 0, 5) physics.addBody( goal2, "static", {density = 1, friction = 0, bounce = 1, isSensor = false}) goal2:setFillColor( 0, 0, 0 ) local wall\_left = display.newRect( 2.5, display.contentHeight/2, 5, display.contentHeight-400, {density = 1, friction = 0, bounce = 1, isSensor = false} ) physics.addBody( wall\_left, "static") local wall\_right = display.newRect( display.contentWidth-2.5, display.contentHeight/2, 5, display.contentHeight-400, {density = 1, friction = 0, bounce = 1, isSensor = false} ) physics.addBody( wall\_right, "static") function bulletBounce( event ) if event.phase=="began" then audio.play( blip ) end end function launchBall() ball = display.newCircle( display.contentWidth/2, display.contentHeight/2, 20, 20) ball.isBullet = true physics.addBody( ball, "dynamic", {density = 1, friction = 0, radius = 5, isSensor = false, bounce = 1} ) ball.myName = "ball" ball:addEventListener( "collision", bulletBounce ) local launchx = math.random(90,110) local launchy = math.random(90,110) if (math.random(0,1)==0) then launchx = -launchx end if (math.random(0,1)==0) then launchy = -launchy end ball:applyLinearImpulse(launchx, launchy) end launchBall() generar\_rectangulos() timer.performWithDelay(1, generar\_rectangulos, 4) function deleteRectangle(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 rectangle.collision = deleteRectangle rectangle:addEventListener( "collision", rectangle ) ball.collision = deleteRectangle ball:addEventListener( "collision", ball )
I have this error:
main.lua:243: attempt to concatenate field ‘myName’ (a nil value)
stack traceback: