I'm trying to set up collision for my game with code from Corona SDK sample called "Collision Detection". I'm getting this realy strange error every time ball collides with something -
Runtime error ...er 1\\documents\\corona projects\\earthyball\\level1.lua:114: attempt to concatenate field 'myName' (a nil value) stack traceback: [C]: ? ...er 1\\documents\\corona projects\\earthyball\\level1.lua:114: in function <...er 1\\documents\\corona prRuntime error ...er 1\\documents\\corona projects\\earthyball\\level1.lua:118: attempt to concatenate field 'myName' (a nil value) stack traceback: [C]: ?
This is my code
display.setStatusBar(display.HiddenStatusBar)
local storyboard = require "storyboard"
local scene = storyboard.newScene()
local physics = require "physics"
physics.start()
physics.setScale( 60 )
physics.setGravity( 0, 12 )
system.setAccelerometerInterval( 100 )
function scene:createScene(event)
local screenGroup = self.view
bkg = display.newImage("bkg.png")
screenGroup:insert(bkg)
finish = display.newImage("finishflag.png")
finish.x = 400
finish.y = 260
finish.myName ="flag"
physics.addBody(finish,"static",element)
blockl1 = display.newImage("blockl1.png")
blockl1:setReferencePoint(display.CentreReferencePoint)
blockl1.x = 165
blockl1.y = 230
physics.addBody(blockl1,"static",element)
screenGroup:insert(blockl1)
l1block2 = display.newImage("l1block2.png")
l1block2:setReferencePoint(display.CentreReferencePoint)
l1block2.x = 300
l1block2.y = 90
physics.addBody(l1block2,"static",element)
screenGroup:insert(l1block2)
line1 = display.newImage("line1.png")
line1:setReferencePoint(display.CentreReferencePoint)
line1.x = 230
line1.y = 300
physics.addBody(line1, "static", element)
line1.myName = "pod"
screenGroup:insert(line1)
line2 = display.newImage("line1.png")
line2:setReferencePoint(display.CentreReferencePoint)
line2.x = 230
line2.y = 10
physics.addBody(line2, "static", element)
line2.myName = "pod2"
screenGroup:insert(line2)
line1Ver = display.newImage("lineVer.png")
line1Ver:setReferencePoint(display.CentreReferencePoint)
line1Ver.x = 20
line1Ver.y = 230
physics.addBody(line1Ver, "static", element)
screenGroup:insert(line1Ver)
line2Ver = display.newImage("lineVer.png")
line2Ver:setReferencePoint(display.CentreReferencePoint)
line2Ver.x = 440
line2Ver.y = 230
physics.addBody(line2Ver, "static", element)
screenGroup:insert(line2Ver)
ball = display.newImage("ball.png")
ball.x = 50
ball.y = 30
physics.addBody(ball,"dynamic",{density=3.1, bounce=0.6, friction=0.5, radius=17})
finish.myName ="ball"
screenGroup:insert(ball)
end
function scene:enterScene(event)
finish.collision = onLocalCollision
finish:addEventListener( "collision", finish )
line1.collision = onLocalCollision
line1:addEventListener( "collision", line1 )
line2.collision = onLocalCollision
line2:addEventListener( "collision", line2 )
ball.collision = onLocalCollision
ball:addEventListener( "collision", ball )
end
function onLocalCollision( 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
function onTilt( event )
physics.setGravity( ( 12.5 * event.xGravity ), ( -12.5 * event.yGravity ) )
end
function scene:exitScene(event)
end
function scene:destroyScene(event)
end
Runtime:addEventListener( "accelerometer", onTilt )
element = { friction=0.5, bounce=0.3 }
scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene)
scene:addEventListener("exitScene",scene)
scene:addEventListener("destroyScene",scene)
return scene
Thanks for helping!
Your all objects are a part of physics. That means, they all collide with each other. Thats why in every collision, it is possible to call a collision event listener. What I'm trying to saying is that, you have a collision event listener on your ball object, line1, line2 and finish. And you think that if there is a collision pnly between these objects your event listener will be called. But for example, when ball object collides with line2ver, line1ver etc., your event listener ball() is calling and that function trys to reach line1ver.myName which does not exist.
So, you must wheter add .myName part to all your objects, or just didn't print .myName part.
And a little tip to you: I think you want to detect collisions with ball - other objects. For that purpose, you can use just one collision event listener, which is ball()
Hope it helps...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.