myName a nil value, collision error

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:

Hi @Kiritosuu,

At a quick glance, this could be a scope issue. When you generate the rectangles, instead of using one variable/pointer above (“local rectangle”), create each rectangle as its own local object within the loop. 

Hope this helps,

Brent

Then, what is your suggestion? I eliminated the “local rectangle” but the error is the same… :frowning:

I think I’ll need to see your updated code… :slight_smile:

hehe sorry

local paddle\_width = 100 local ball local ball2 -- on here I delete the "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 )

Thank you in advance!!

Now you’re making it a global variable object, which is actually worse than a local (globals should be avoided in almost every instance). You should localize each new rectangle inside the function where you generate them.

Take care,

Brent

how I can do that? I am somewhat newbie in Corona, sorry

Since scope is such a fundamental concept in Lua, I suggest that you learn and understand it thoroughly before progressing to things like physics. The video below should help you with this, along with the various guides, videos, and tutorials at Corona University:

http://www.coronalabs.com/resources/tutorials/getting-started-with-corona/

http://www.youtube.com/watch?v=2ATlcGP2zMY

Brent, please tell me where is the error and what should I replace, I think that way learn faster, but tried to try climbing up and down functions do not achieve anything, still gives the same error, but is “nil” the myName tells me it is “nil” box, I find the right “position”, I have read several threads in the forum but I see that there are many ways, I just want something that says “if ball collides with any rectangle, remove that rectangle”  I want nothing more in this regard. Please tell me what code should I use and try to understand based on the code, thanks.

I can show you some tips, but your code has other issues too, so changing one thing with the rectangles would cause errors in other areas. It’s really important that you understand scope, because you will encounter more confusion later if you don’t fully understand scope.

For example, this is how you should properly generate the rectangles:

[lua]

local function generar_rectangulos()

   local rectangle = display.newRect(100, 300, 20, 20)

   physics.addBody(rectangle, {bounce = 0.9, radius = 25, friction = 1.0 })

   rectangle.myName = “rectangle”

end

[/lua]

However, this will cause your function that randomly moves the rectangles to break, because Lua doesn’t know the scope. So, you would need to re-write that movement function too. Also, you will need to (and should) remove the collision listeners on the rectangles, which are not necessary if they will only interact with (and get removed) with collision with the ball. In that case, only the ball needs to have a collision listener, and any time the ball hits a rectangle, “event.other” will point to the rectangle.

Best regards,

Brent

Hi @Kiritosuu,

At a quick glance, this could be a scope issue. When you generate the rectangles, instead of using one variable/pointer above (“local rectangle”), create each rectangle as its own local object within the loop. 

Hope this helps,

Brent

Then, what is your suggestion? I eliminated the “local rectangle” but the error is the same… :frowning:

I think I’ll need to see your updated code… :slight_smile:

hehe sorry

local paddle\_width = 100 local ball local ball2 -- on here I delete the "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 )

Thank you in advance!!

Now you’re making it a global variable object, which is actually worse than a local (globals should be avoided in almost every instance). You should localize each new rectangle inside the function where you generate them.

Take care,

Brent

how I can do that? I am somewhat newbie in Corona, sorry

Since scope is such a fundamental concept in Lua, I suggest that you learn and understand it thoroughly before progressing to things like physics. The video below should help you with this, along with the various guides, videos, and tutorials at Corona University:

http://www.coronalabs.com/resources/tutorials/getting-started-with-corona/

http://www.youtube.com/watch?v=2ATlcGP2zMY

Brent, please tell me where is the error and what should I replace, I think that way learn faster, but tried to try climbing up and down functions do not achieve anything, still gives the same error, but is “nil” the myName tells me it is “nil” box, I find the right “position”, I have read several threads in the forum but I see that there are many ways, I just want something that says “if ball collides with any rectangle, remove that rectangle”  I want nothing more in this regard. Please tell me what code should I use and try to understand based on the code, thanks.

I can show you some tips, but your code has other issues too, so changing one thing with the rectangles would cause errors in other areas. It’s really important that you understand scope, because you will encounter more confusion later if you don’t fully understand scope.

For example, this is how you should properly generate the rectangles:

[lua]

local function generar_rectangulos()

   local rectangle = display.newRect(100, 300, 20, 20)

   physics.addBody(rectangle, {bounce = 0.9, radius = 25, friction = 1.0 })

   rectangle.myName = “rectangle”

end

[/lua]

However, this will cause your function that randomly moves the rectangles to break, because Lua doesn’t know the scope. So, you would need to re-write that movement function too. Also, you will need to (and should) remove the collision listeners on the rectangles, which are not necessary if they will only interact with (and get removed) with collision with the ball. In that case, only the ball needs to have a collision listener, and any time the ball hits a rectangle, “event.other” will point to the rectangle.

Best regards,

Brent