碰撞检测:myName nil值

时间:2013-03-30 13:01:20

标签: lua sdk null corona

我正在尝试使用名为“碰撞检测”的Corona SDK示例中的代码为我的游戏设置碰撞。 每次球碰撞时我都会得到这个奇怪的错误 -

  

运行时错误           ... er 1 \ documents \ corona projects \ earthyball \ level1.lua:114:尝试连接字段'myName'(零值)堆栈追溯:           [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:尝试连接字段'myName'(零值)堆栈追溯:           [C]:?

这是我的代码

    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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你的所有物体都是物理学的一部分。这意味着,它们都相互碰撞。这就是为什么在每次碰撞中,都可以调用碰撞事件监听器。我想说的是,你的球对象,line1,line2和finish上有一个碰撞事件监听器。而且你认为如果这些对象之间存在冲突,你的事件监听器就会被调用。但是,例如,当球对象与line2ver,line1ver等碰撞时,你的事件监听器ball()正在调用,并且该函数试图到达不存在的line1ver.myName。

因此,您必须将.myName部分添加到所有对象中,或者只是不打印.myName部分。

给你一点建议: 我想你想要检测与球的碰撞 - 其他物体。为此,您只能使用一个碰撞事件侦听器,即ball()

希望它有所帮助...