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
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
On collison
Try using timer.performWithDelay(5000, showMessage)
upon collision
With showMessage being your message function
@Danny
thanks. My problem is i dont know how to write that code
for example like this
1 2 3 | when obj1 collides with obj2 then do something end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
Here how i did it and it doesnt work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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) |
make sure you add a name to the shape
1 | shape.name = "shape" |
I did but my showmsg() is not being called and the text is not displaying . I dont know why
try replacing:
1 | timer.performWithDelay (5000, showmsg) |
Still nothing . Well here is my entire code that might help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | ----------------------------------------------------------------------------------- -- 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 |
simple typo i think
change to :
1 2 3 4 5 6 7 8 | 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 |
Still no luck. Thanks
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!
Got it to work thanks man
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
Yes just switching positions of functions. Thanks again
Thank you for the confirmation!
Mo
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Random Question... Code Formatting, which language is close to lua?
Status of Univeral Apps
TexturePacker update 2.3.2 for Windows and Mac
Irreversible iTunes app data
Orientation Requirements?
Create/Grabbing a User Id
Ninja Turkey Dashing War (Free!)
CPM Feature Discussion - Drag and Drop Editor
Repeat sprite after two second interval?