Fazil
Fazil

Reputation: 1390

Collision separation - CORONA

While the jet object hits any of the Mine or fruit its getting explode, instead when I try to separate collision from colloid able object non Colloid able its not working.Here the code which I have used

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)

Colloidable object

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)

Non Colloidable object

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)

Collision method

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 

Where i need to change to specify the changes that "mine" object to explode and "food" objects to hide and keep moving the jet?Please help me to resolve

Upvotes: 1

Views: 50

Answers (1)

Basilio German
Basilio German

Reputation: 1819

You need to specify it in the last method, onCollision, check if the object2 name is either "mine1" or "food1", and then you can do specific things with each condition:

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 

Upvotes: 3

Related Questions