碰撞分离 - CORONA

时间:2014-05-19 12:35:11

标签: lua corona collision

当喷射物撞击任何矿井或水果时,它会爆炸,而当我试图将碰撞与胶体能物体分开时,非胶体能够不起作用。这就是我使用的代码

jetSpriteSheet = sprite.newSpriteSheet("greenman.png", 225, 225)
jetSprites = sprite.newSpriteSet(jetSpriteSheet, 1, 4)
sprite.add(jetSprites, "jets", 1, 16, 500, 0)
jet = sprite.newSprite(jetSprites)
jet.x = 180
jet.y = 280
jet:prepare("jets")
jet:play()
jet.collided = false
jet.name = "jett"
physics.addBody(jet, {density=0.1, bounce=0.5, friction=1, radius=12})
screenGroup:insert(jet)

胶体对象

mine1 = display.newImage("mine.png")
mine1.x = 850
mine1.y = 250
mine1.name="mine1"
mine1.speed = math.random(2,6)
mine1.initY = mine1.y
mine1.amp = math.random(20,100)
mine1.angle = math.random(1,360)
physics.addBody(mine1, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(mine1)

非胶体对象

food1 = display.newImage("fruits.png")
food1.x = 650
food1.y = 250
food1.speed = math.random(2,9)
food1.initY = food1.y
food1.name = "food1"
food1.isFood = true
food1.isVisible =true
food1.amp = math.random(20,200)
food1.angle = math.random(1,180)
physics.addBody(food1, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
screenGroup:insert(food1)

碰撞方法

function onCollision(event)

  if event.phase == "began" and "jett"  == event.object1.name then
    if jet.collided == false then 
        jet.collided = true
        jet.bodyType = "static"
        explode()
        storyboard.gotoScene("restart", "fade", 400)
    end
end
end 

我需要更改以指定“我的”对象爆炸的变化和“食物”对象隐藏并继续移动喷气机?请帮我解决

1 个答案:

答案 0 :(得分:3)

您需要在最后一个方法onCollision中指定它,检查object2名称是"mine1"还是"food1",然后您可以针对每个条件执行特定操作:< / p>

function onCollision(event)
    local object1Name = event.object1.name
    if event.phase == "began" and "jett" == object1Name then
        if "mine1" == object1Name then
            -- Do something with the mine
        elseif "food1" == object1Name then
            -- Do something with food
        else
            if jet.collided == false then 
                jet.collided = true
                jet.bodyType = "static"
                explode()
                storyboard.gotoScene("restart", "fade", 400)
            end
        end
    end
end