On collison

Hi there, Im trying to do this: when an object collides with another then after 5 seconds i display pop up window. I looked but cant figure out how to do this. Any help will be much appreciated . Thanks [import]uid: 11559 topic_id: 20235 reply_id: 320235[/import]

Try using timer.performWithDelay(5000, showMessage)

upon collision

With showMessage being your message function [import]uid: 84637 topic_id: 20235 reply_id: 79018[/import]

@Danny
thanks. My problem is i dont know how to write that code

for example like this

when obj1 collides with obj2 then  
do something  
end  

Thanks [import]uid: 11559 topic_id: 20235 reply_id: 79019[/import]

[code]
object1.name = “object1”

object1:addEventListener(“collision”, oncollision)

local function oncollision(event)
if event.phase == “began” and event.other.name == “object1” then
timer.performWithDelay(5000, showmsg)
end
end

local function showmsg(event)
–msg code here
end
[/code] [import]uid: 31078 topic_id: 20235 reply_id: 79021[/import]

object1.name = "object1"  
object2.name = "object2"  
  
object1:addEventListener("collision", oncollision)  
  
local function oncollision(event)  
 if event.phase == "began" and event.other.name == "object2" then  
 timer.performWithDelay(5000, showmsg)  
 end  
end  
  
local function showmsg()  
--msg code here  
end  

sry i messed up the other code [import]uid: 31078 topic_id: 20235 reply_id: 79022[/import]

Here how i did it and it doesnt work

local function onCollision (event )  
 if event.phase == "began" and event.other.name == "shape" then  
 timer.performWithDelay (5000, showmsg)  
 end  
end  
  
local function shawmsg()  
text = display.newText("Pop up goes here", 0 , 0 , native.systemFont , 16)  
text:setTextColor (255)  
text.x = 240  
text.y = 160  
group:insert(text)  
end  
  
rope:addEventListener("collision", onCollision)  

Something wrong here? [import]uid: 11559 topic_id: 20235 reply_id: 79027[/import]

make sure you add a name to the shape

shape.name = "shape"  

you have to tell it what its name is [import]uid: 31078 topic_id: 20235 reply_id: 79033[/import]

I did but my showmsg() is not being called and the text is not displaying . I dont know why [import]uid: 11559 topic_id: 20235 reply_id: 79037[/import]

try replacing:

timer.performWithDelay (5000, showmsg)  

with:

timer1 = timer.performWithDelay (5000, showmsg)  

if that doesnt work get rid of the local that is before the function on both of them [import]uid: 31078 topic_id: 20235 reply_id: 79039[/import]

Still nothing . Well here is my entire code that might help

-----------------------------------------------------------------------------------  
-- Play scene  
-----------------------------------------------------------------------------------  
module(...,package.seeall)  
  
-----------------------------------------------------------------------------------  
-- local objects declaration   
-----------------------------------------------------------------------------------  
  
local group  
local shape  
local background   
local rope  
local ground  
local xover  
local xover2  
local shapeGroup  
local musicButton  
local soundButton  
local i  
local text  
-----------------------------------------------------------------------------------  
-- Start Physics engine   
-----------------------------------------------------------------------------------  
  
physics.start()  
  
  
-----------------------------------------------------------------------------------  
--Button press handlers goes here  
-----------------------------------------------------------------------------------  
  
-- When shapes colided with rope then do this   
function onCollision (event )  
 if event.phase == "began" and event.other.name == "rope" then  
 timer.performWithDelay (5000, showmsg)  
 end  
end  
  
local function shawmsg()  
text = display.newText("Pop up goes here", 0 , 0 , native.systemFont , 16)  
print "ok"  
text:setTextColor (255)  
text.x = 240  
text.y = 160  
group:insert(text)  
end  
-- When reset pressed then do this  
function resetPressed(b)  
 game.playEventSound(game.soundPressed)  
 shapeGroup:removeSelf()  
 -- Add squere shape to the scene  
 shapeGroup = display.newGroup()  
 shape = display.newImageRect("levels/shape.png" , 25 , 25)  
 shapeGroup.x = 240  
 shapeGroup.y = 120  
 shapeGroup:insert(shape)  
 group:insert(shapeGroup)  
 physics.addBody(shapeGroup, {density = 3.0 , friction = 0.5, bounce = 0.3} )  
 i = 1  
local function shapePressed()  
 physics.removeBody(shapeGroup)  
 if i \> 5 then return end   
 local function addNewBody()  
 physics.addBody(shapeGroup, {density = 3.0 , friction = 0.5, bounce = 0.3} )  
 end  
 transition.to(shape, {time=10, xScale = shape.xScale + 0.3, yScale = shape.yScale + 0.3, onComplete=addNewBody })   
 i = i+1   
  
end  
 shape:addEventListener("tap", shapePressed)   
end  
 -- When Manu button pressed  
local function menuPressed(b)  
 game.playEventSound(game.soundPressed)  
 game.changeScene("mainscene","overFromRight")  
end  
  
--Music button pressed function  
local function musikPressed(b)  
 if game.music then  
 musicButton.image="buttons/musicover.png"  
 game.music = false  
 audio.stop()  
 else  
 musicButton.image="buttons/musicicon.png"  
 audio.play(game.musicStream,{channel=1,loops=-1})  
 end  
 game.playEventSound(game.soundPressed)  
end  
  
-- Sound button pressed function  
local function soundPressed(b)  
 if game.sound then  
 soundButton.image="buttons/soundover.png"  
 game.sound = false  
 else  
 soundButton.image="buttons/soundicon.png"  
 game.sound = true  
 end  
 game.playEventSound(game.soundPressed)  
end  
  
-----------------------------------------------------------------------------------  
  
-- Shapes fucnctions are here to detect touch and physics bodys  
-----------------------------------------------------------------------------------  
i = 1  
local function shapePressed()  
 physics.removeBody(shapeGroup)  
 if i \> 5 then return end   
 local function addNewBody()  
 physics.addBody(shapeGroup, {density = 3.0 , friction = 0.5, bounce = 0.3} )  
 end  
 transition.to(shape, {time=10, xScale = shape.xScale + 0.5, yScale = shape.yScale + 0.5, onComplete=addNewBody })   
 --transition.to(shape, {time=10, width = shape.width \* 1.3, height = shape.height \* 1.3, onComplete=addNewBody} )  
 i = i+1   
end  
  
-----------------------------------------------------------------------------------  
-- Collision detection goes here  
-----------------------------------------------------------------------------------  
--[[ add collision to the shapes  
local function onCollision (self , event)  
 if (event.phase == "began" ) then  
 end  
end ]]  
  
-----------------------------------------------------------------------------------  
-- All the scene objects must go here  
-----------------------------------------------------------------------------------  
  
function new()  
 physics.start()  
 group = display.newGroup()  
  
  
 -- Add Background to the scene  
 background = display.newImageRect( "backgrounds/bkg.png", 480, 320 )  
 background.x = 240  
 background.y = 160  
 group:insert(background)  
  
  
  
 -- Add rope to the scene  
 rope =display.newImageRect ( "levels/rope.png", 480, 9)  
 rope.x = 240  
 rope.y = 110  
 rope.name = "rope"  
 group:insert(rope)  
  
 -- Add ground to the scene   
 ground = display.newImageRect ("levels/ground.png", 200, 64)  
 ground.x = 240  
 ground.y = 210  
 group:insert (ground)  
  
  
 -- Add squere shape to the scene  
 shapeGroup = display.newGroup()  
 shape = display.newImageRect("levels/shape.png" , 25 , 25)  
 shapeGroup.x = 240  
 shapeGroup.y = 120  
 shapeGroup:insert(shape)  
 group:insert(shapeGroup)  
  
  
  
  
  
  
-----------------------------------------------------------------------------------  
  
-- All the buttons must be created here  
-----------------------------------------------------------------------------------  
  
 button:create(group,1,{x=45,y=295,w=40,h=40,handler=menuPressed},"buttons/menuicon.png")  
 button:create(group,1,{x=130,y=300,w=71,h=21,handler=resetPressed}, "buttons/restarticon.png")  
 soundButton=button:create(group,1,{x=450,y=300,w=34,h=28,handler=soundPressed}, "buttons/soundicon.png")  
 if game.sound == false then soundButton.image="buttons/soundover.png" end  
 musicButton=button:create(group,1,{x=400,y=300,w=34,h=31,handler=musikPressed}, "buttons/musicicon.png")  
 if game.music == false then musicButton.image="buttons/musicover.png" end  
  
  
  
-----------------------------------------------------------------------------------  
-- All the event listeners must go here  
-----------------------------------------------------------------------------------  
  
 -- Squere shape event listener  
 shapeGroup:addEventListener("tap", shapePressed)  
 rope:addEventListener("collision", onCollision)  
  
  
-----------------------------------------------------------------------------------  
-- All the physics bodys must me created here  
-----------------------------------------------------------------------------------  
 -- Ground physics body  
 physics.addBody(ground, "static",{density = 3.0 , friction = 0.5, bounce = 0.1 } )  
 -- Squere shape physics body  
 physics.addBody(shapeGroup, {density = 3.0 , friction = 0.5, bounce = 0.3} )  
 -- Add rope physics body  
 physics.addBody(rope, "static",{density = 0 , friction = 0, bounce = 0})  
  
  
  
  
 return group  
end  
  

I want rope to collide with the shape and then call function showmsg()
[import]uid: 11559 topic_id: 20235 reply_id: 79045[/import]

simple typo i think

change to :

function showmsg()  
text = display.newText("Pop up goes here", 0 , 0 , native.systemFont , 16)  
print "ok"  
text:setTextColor (255)  
text.x = 240  
text.y = 160  
group:insert(text)  
end  

your function name I put was shawmsg() not showmsg() sry about that [import]uid: 31078 topic_id: 20235 reply_id: 79048[/import]

Still no luck. Thanks [import]uid: 11559 topic_id: 20235 reply_id: 79058[/import]

Try switching the onCollision() and shwmsg() positions in your code. Maybe the shwmsg function is not seen because it is after the call for it inside the onCollision function? Or you can try something like this at the top of your code:

Local showmsg, onCollision

That way the positions of those functions won’t matter.

No sure…also, do you get any error messages?

Good luck! [import]uid: 49236 topic_id: 20235 reply_id: 79074[/import]

Got it to work thanks man [import]uid: 11559 topic_id: 20235 reply_id: 79081[/import]

That’s great to hear! I am very happy for you. Just curious, was simply moving the functions? I was not sure about that suggestion but I am glad it help if that was the case.

In any event good luck for the rest of your app!

Mo [import]uid: 49236 topic_id: 20235 reply_id: 79087[/import]

Yes just switching positions of functions. Thanks again [import]uid: 11559 topic_id: 20235 reply_id: 79094[/import]

Thank you for the confirmation!

Mo [import]uid: 49236 topic_id: 20235 reply_id: 79096[/import]