I can't get my onCollision function to call through the 'floor1' collision event listener. I can get it to call through the 'present1' collision event listener, but I want the floor1 to have it since multiple objects will be hitting it. Thanks in advance
Also I could only forward declare onCollision function as a table, can someone explain why that is?
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 | --scene1-- ---------------------------------------------------------------------------------- -- -- scenetemplate.lua -- ---------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() physics = require('physics') physics.start() physics.setGravity(0,9.3) --physics.setDrawMode('hybrid') local p = 0 local bg1, floor1, present1, dropButton local phys1 = {friction = .7, bounce = .1, density = .3} local h = display.contentHeight local w = display.contentWidth local level1Presents local onTouch local createDropButton local dropPresents local onCollision = {} ---------------------------------------------------------------------------------- -- -- NOTE: -- -- Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view bg1 = display.newRect(0, 0, w, h) bg1:setFillColor(200,0,0) group:insert(bg1) floor1 = display.newRect(0, h-1, w, 1) floor1.name = 'floor1' physics.addBody(floor1, "static",{density = 9, friction = .7, bounce = .2}) floor1:addEventListener('collision', onCollision) group:insert(floor1) ----------------------------------------------------------------------------- -- CREATE display objects and add them to 'group' here. -- Example use-case: Restore 'group' from previously saved state. ----------------------------------------------------------------------------- end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view print( "1: enterScene event" ) local function level1Presents() p = p+1 print (p) if (p ==1) then present1 = display.newRect(0,0, 50, 50) physics.addBody(present1, 'kinematic', phys1) present1.x = w/2 present1.y = h/7 present1.name = 'present1' present1:addEventListener('touch', onTouch) present1:addEventListener('collision', onCollision) group:insert(present1) elseif (p == 2) then storyboard.gotoScene('scene2') end end function onTouch(event) t = event.target t.name = 't' local phase = event.phase if event.phase == "began" then display.getCurrentStage():setFocus(t) t.isFocus = true t.x0= event.x - t.x elseif t.isFocus then if event.phase == "moved" then t.x = event.x - t.x0 elseif event.phase == 'ended' or 'cancelled' then display.getCurrentStage():setFocus(nil) t.isFocus = false createDropButton() t:removeEventListener('touch', onTouch) end end return true end function createDropButton() dropButton = display.newRect(w-100,h/4, 80, 80) dropButton:addEventListener('tap', dropPresent) end function dropPresent() t.bodyType = 'dynamic' timer.performWithDelay(3000, level1Presents, 1) display.remove(dropButton) --dropButton:removeEventListener end function onCollision(event) if event.other.name =='present1' then floor1:setFillColor(255, 0, 0) print 'collision' end end level1Presents() ----------------------------------------------------------------------------- -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) ----------------------------------------------------------------------------- end -- Called when scene is about to move offscreen: function scene:exitScene( event ) print( "1: exitScene event" ) --local group = self.view present1:removeEventListener('touch', onTouch) --dropButton:removeEventListener('tap', dropPresent) ----------------------------------------------------------------------------- -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) ----------------------------------------------------------------------------- end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) --local group = self.view print( "((destroying scene 1's view))" ) ----------------------------------------------------------------------------- -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) ----------------------------------------------------------------------------- end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene |
Hey there, try this;
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 | ---------------------------------------------------------------------------------- -- -- scenetemplate.lua -- ---------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() physics = require('physics') physics.start() physics.setGravity(0,9.3) --physics.setDrawMode('hybrid') local p = 0 local bg1, floor1, present1, dropButton local phys1 = {friction = .7, bounce = .1, density = .3} local h = display.contentHeight local w = display.contentWidth local level1Presents local onTouch local createDropButton local dropPresents local onCollision = {} ---------------------------------------------------------------------------------- -- -- NOTE: -- -- Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view bg1 = display.newRect(0, 0, w, h) bg1:setFillColor(200,0,0) group:insert(bg1) floor1 = display.newRect(0, h-1, w, 1) floor1.name = 'floor1' physics.addBody(floor1, "static",{density = 9, friction = .7, bounce = .2}) floor1:addEventListener('collision', floor1) group:insert(floor1) function floor1:collision () print "test" end ----------------------------------------------------------------------------- -- CREATE display objects and add them to 'group' here. -- Example use-case: Restore 'group' from previously saved state. ----------------------------------------------------------------------------- end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view print( "1: enterScene event" ) local function level1Presents() p = p+1 print (p) if (p ==1) then present1 = display.newRect(0,0, 50, 50) physics.addBody(present1, 'kinematic', phys1) present1.x = w/2 present1.y = h/7 present1.name = 'present1' present1:addEventListener('touch', onTouch) present1:addEventListener('collision', onCollision) group:insert(present1) elseif (p == 2) then storyboard.gotoScene('scene2') end end function onTouch(event) t = event.target t.name = 't' local phase = event.phase if event.phase == "began" then display.getCurrentStage():setFocus(t) t.isFocus = true t.x0= event.x - t.x elseif t.isFocus then if event.phase == "moved" then t.x = event.x - t.x0 elseif event.phase == 'ended' or 'cancelled' then display.getCurrentStage():setFocus(nil) t.isFocus = false createDropButton() t:removeEventListener('touch', onTouch) end end return true end function createDropButton() dropButton = display.newRect(w-100,h/4, 80, 80) dropButton:addEventListener('tap', dropPresent) end function dropPresent() t.bodyType = 'dynamic' timer.performWithDelay(3000, level1Presents, 1) display.remove(dropButton) --dropButton:removeEventListener end function onCollision(event) if event.other.name =='present1' then floor1:setFillColor(255, 0, 0) print 'collision' end end level1Presents() ----------------------------------------------------------------------------- -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) ----------------------------------------------------------------------------- end -- Called when scene is about to move offscreen: function scene:exitScene( event ) print( "1: exitScene event" ) --local group = self.view present1:removeEventListener('touch', onTouch) --dropButton:removeEventListener('tap', dropPresent) ----------------------------------------------------------------------------- -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) ----------------------------------------------------------------------------- end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) --local group = self.view print( "((destroying scene 1's view))" ) ----------------------------------------------------------------------------- -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) ----------------------------------------------------------------------------- end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene |
views:1928 update:2012/1/4 9:12:54