hi gurus,
i am planing to create two button (top, down), and if i hold & drag the top button to bottom button, the top button's x, y will be the same as the bottom button when they 'collide'
it's try to simulate the RPG inventory, when we drag an item to a slot, it should automatically snap on the slot.
but i found that the 'collide' event doesn't trigger bcoz i am using a body type 'static' :(
Can anyone modify my code to make it work? i believe it should be a small tweak :)
sample code below
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 | local physics = require("physics") physics.start() local button1 = display.newImage('buttonbg.png') button1.x, button1.y = 100, 100 local button2 = display.newImage('buttonbg.png') button2.x, button2.y = 100, 200 physics.addBody( button1, "static", { density=3.0, friction=0.5, bounce=0.3 } ) physics.addBody( button2, "static", { density=3.0, friction=0.5, bounce=0.3 } ) -- ADD COLLISION EVENT local function onDragCollision(self, event) print('coll function') if ( event.phase == "began" ) then print('collision began') self.bodyType = 'static' elseif ( event.phase == "ended" ) then print('collision ended') end end button1.collision = onDragCollision button1:addEventListener( "collision", button1 ) -- ADD DRAG EVENT local function startDrag(e) local t = e.target local phase = e.phase if phase == 'began' then t.x0, t.y0 = e.x - t.x, e.y - t.y display.getCurrentStage():setFocus(t) t.isFocus = true elseif t.isFocus then if phase == 'moved' then -- t.x, t.y = e.x - t.x0, e.y - t.y0 elseif phase == 'ended' then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end end button1:addEventListener('touch', startDrag) button2:addEventListener('touch', startDrag) |
it is not working because all the bodies are static which cant collide with each other only dynamic body can collide with all type of body static and kinematic body cant collide with each other
:)