Corona SDK碰撞检测无法正常工作

时间:2011-08-14 04:58:54

标签: lua corona

我有一个(工作)功能正在生成项目符号:

local function shootBullets ( event ) -- It's called through a timer
    local bullet = display.newImageRect("images/Bullet.png", 12, 12) --Make a bullet


    local convertedGunRotation = tankGun.rotation*(math.pi/180) --Don't worry about this just some math for figure out where to shoot the bullet
    local orientation = math.pi

    bullet.x = tankBody.x
    bullet.y = tankBody.y

    physics.addBody(bullet, "kinematic", {bounce = 0}) --Make it a body so I can give it a velocity with the function setLinearVelocity
    bullets:insert( bullet ) --Add it to my bullets group

    bullet.name = "bullet" --Used later for collision detection
    bullet.collision = onCollision --These two lines are what I am doing for collision, isn't working.
    bullet:addEventListener("collision", bullet)


    --This is how I am moving the bullet
    bullet:setLinearVelocity(0*math.cos(convertedGunRotation+orientation)-100*math.sin(convertedGunRotation+orientation), 0*math.sin(convertedGunRotation+orientation)+100*math.cos(convertedGunRotation+orientation))
end

我在屏幕周围有4个矩形:

local leftWall = display.newRect(0, 0, 10, _H) --Make the rectangle
leftWall.strokeWidth = 3 --Give its edges a width
leftWall:setFillColor(192, 255, 255) --Fill it with a color to make it visible
leftWall:setStrokeColor(192, 255, 255)
leftWall.name = "wall" --Give it a name for reference in collision detection 
physics.addBody( leftWall, "static") --Make sure it can't move and make it a body.

--Same hold true for the other 3 walls, they surround the player so you can't not hit one with a bullet.
local rightWall = display.newRect(_W-10, 0, 10, _H)
rightWall.strokeWidth = 3
rightWall:setFillColor(192, 255, 255)
rightWall:setStrokeColor(192, 255, 255)
rightWall.name = "wall"
physics.addBody( rightWall, "static")

local bottomWall = display.newRect(0, _H-10, _W, 10)
bottomWall.strokeWidth = 3
bottomWall:setFillColor(192, 255, 255)
bottomWall:setStrokeColor(192, 255, 255)
bottomWall.name = "wall"
physics.addBody( bottomWall, "static")

local topWall = display.newRect(0, 0, _W, 10)
topWall.strokeWidth = 3
topWall:setFillColor(192, 255, 255)
topWall:setStrokeColor(192, 255, 255)
topWall.name = "wall"
physics.addBody( topWall, "static")

碰撞检测功能:

local function onCollision( event )
    print("Here") -- Never gets here.
    -- Bullet hit something
    if event.object1.name == "bullet" then
        if event.object2.name == "enemy" then -- I'm going to worry about enemies later
                    --I will do stuff later
        elseif event.object2.name == "wall" then -- It should be hitting a wall
                    --TODO
        end
    end
end

但是,当我在Corona终端中运行时,print("Here")函数甚至没有在onCollision函数中被调用。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

好吧,我最终使用的解决方案(并注意这只有在你的游戏没有任何引力时才有效)是我在将物体添加到物理及其{{"dynamic"时制作了子弹isSensor 1}}属性等于true。出于某种原因,这对我有用。但是我仍会保持开放,直到有人找到一个更好的答案(一个可以使用具有"kinematic"属性的子弹正常工作。(虽然为我工作......)

答案 1 :(得分:1)

检查一下:

http://developer.anscamobile.com/reference/index/physicsaddbody

他们在评论时说:

注意:此API不应在碰撞事件处理程序中使用。

祝你好运=)