Some collision problems

Hey guys!

  I want to add collission on my objects,the problem is that all the objects have to be static,and static objects do not interactionate with each other.

So how can I make objects interact,using collisions?

If I make one of the objects dynamic,it runs out from the screen.

Help me!

Perhaps this tutorial will help you:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

Hey Rob,

 Thanks for the link.I did someting similar check it out. The problem is that when object one touches the second object it prints 2 times (“works”).

In the final game,when the object one will touch object two I asume that the animation will play 2 times,is that correct?I want the animation only to play one.

Untitled.jpg?dl=0

https://www.dropbox.com/s/ayn1wt6frnyljz7/Untitled.jpg?dl=0  Image url

Can you post the code you’re using?

local ufo = display.newImageRect("ufo2.png" ,100,90) ufo.x = 100 ufo.y = 100 local beam = display.newImageRect("Beam.png" ,90,200) beam.isVisible = false local obj = display.newImageRect ("Icon.png",70,70) obj.x = 100 obj.y = 300 local sheetData = {width = 100,height = 90,numFrames = 8,sheetContentWidth = 200,sheetContentHeight = 360} local mySheet = graphics.newImageSheet ("ufo.png",sheetData) local sequenceData = {name = "ufo1",start = 1,count = 8,time = 950,loopcount = 0,looDirection = "forward"} local ufo1 = display.newSprite (mySheet,sequenceData) ufo1.x = 500 ufo1.y = 100 ufo1Intro = transition.to (ufo1, {time = 1500,x = 240}) ufo1.isVisible = false function beamOn (event) if event.phase == "began" then beam.isVisible = true beam.x = ufo.x beam.y = ufo.y + 110 if beam.x \< 50 then beam.isVisible = false end end end ufo:addEventListener ("touch",beamOn) function beamOff (event) if event.phase == "ended" then beam.isVisible = false end end ufo:addEventListener ("touch",beamOff) function onCollision (event) if ( event.phase == "began" and beam.y == obj.y - 90 ) then ufo1:play() ufo1.isVisible = true if (event.phase == "ended") then ufo1:pause() ufo1.isVisible = false end end end ufo:addEventListener("touch",onCollision)

Here it is. The ufo1 (the animation) doesn’t pause and doesn’t disaper. Also how can I make the animation play and stop and disaper when it’s finished?

I don’t see where you are printing out “works” and “worked”.  I also don’t see where you are using the non-physics collisions.  Where are you trying to use physics? The collision detection method at the bottom only works with physics.R

Rob

Check the photo  I have attached,this is  meant to be the final code,but I doubt. The no-physics collision doesn’t fit with my game.So I tried this,a rough version.

But I do not understand why the animation doesn’t stop when the touch is released.

Here’s the photo: https://www.dropbox.com/s/ayn1wt6frnyljz7/Untitled.jpg?dl=0 

Is there any way you can make dynamic physics objects not to fall from the screen?

I see your issue now.  First of all, trying to read code from a screen shot is difficult. 

Basically your onCollision isn’t testing for the different touch phases.  Since you did your code as an image, I can’t copy and paste it here to show you the problem.

If your touch handler you should do something like:

function onCollision( event )

    if event.phase == “began” then

        if beam.y == obj.y - 90 then

            print(“works”

        end

    elseif event.phase == “ended” then

         print(“worked”)

    end

    return true

end

Perhaps this tutorial will help you:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

Hey Rob,

 Thanks for the link.I did someting similar check it out. The problem is that when object one touches the second object it prints 2 times (“works”).

In the final game,when the object one will touch object two I asume that the animation will play 2 times,is that correct?I want the animation only to play one.

Untitled.jpg?dl=0

https://www.dropbox.com/s/ayn1wt6frnyljz7/Untitled.jpg?dl=0  Image url

Can you post the code you’re using?

local ufo = display.newImageRect("ufo2.png" ,100,90) ufo.x = 100 ufo.y = 100 local beam = display.newImageRect("Beam.png" ,90,200) beam.isVisible = false local obj = display.newImageRect ("Icon.png",70,70) obj.x = 100 obj.y = 300 local sheetData = {width = 100,height = 90,numFrames = 8,sheetContentWidth = 200,sheetContentHeight = 360} local mySheet = graphics.newImageSheet ("ufo.png",sheetData) local sequenceData = {name = "ufo1",start = 1,count = 8,time = 950,loopcount = 0,looDirection = "forward"} local ufo1 = display.newSprite (mySheet,sequenceData) ufo1.x = 500 ufo1.y = 100 ufo1Intro = transition.to (ufo1, {time = 1500,x = 240}) ufo1.isVisible = false function beamOn (event) if event.phase == "began" then beam.isVisible = true beam.x = ufo.x beam.y = ufo.y + 110 if beam.x \< 50 then beam.isVisible = false end end end ufo:addEventListener ("touch",beamOn) function beamOff (event) if event.phase == "ended" then beam.isVisible = false end end ufo:addEventListener ("touch",beamOff) function onCollision (event) if ( event.phase == "began" and beam.y == obj.y - 90 ) then ufo1:play() ufo1.isVisible = true if (event.phase == "ended") then ufo1:pause() ufo1.isVisible = false end end end ufo:addEventListener("touch",onCollision)

Here it is. The ufo1 (the animation) doesn’t pause and doesn’t disaper. Also how can I make the animation play and stop and disaper when it’s finished?

I don’t see where you are printing out “works” and “worked”.  I also don’t see where you are using the non-physics collisions.  Where are you trying to use physics? The collision detection method at the bottom only works with physics.R

Rob

Check the photo  I have attached,this is  meant to be the final code,but I doubt. The no-physics collision doesn’t fit with my game.So I tried this,a rough version.

But I do not understand why the animation doesn’t stop when the touch is released.

Here’s the photo: https://www.dropbox.com/s/ayn1wt6frnyljz7/Untitled.jpg?dl=0 

Is there any way you can make dynamic physics objects not to fall from the screen?

I see your issue now.  First of all, trying to read code from a screen shot is difficult. 

Basically your onCollision isn’t testing for the different touch phases.  Since you did your code as an image, I can’t copy and paste it here to show you the problem.

If your touch handler you should do something like:

function onCollision( event )

    if event.phase == “began” then

        if beam.y == obj.y - 90 then

            print(“works”

        end

    elseif event.phase == “ended” then

         print(“worked”)

    end

    return true

end