0

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

运行时错误 ...er 1\documents\corona projects\earthyball\level1.lua:114: 尝试连接字段“myName”(一个 nil 值)堆栈回溯:[C]:?...er 1\documents\corona projects\earthyball\level1.lua:114: 在函数 <...er 1\documents\corona prRuntime 错误 ...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

感谢您的帮助!

4

1 回答 1

0

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

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

给你一个小提示:我认为你想检测与球的碰撞 - 其他物体。为此,您可以只使用一个碰撞事件侦听器,即 ball()

希望能帮助到你...

于 2013-03-31T12:17:32.527 回答