Hi,
Just a quick one on dragging objects. I'm having a problem where by the user can touch just below the object and move it without actually being directly on the object.
I want them to be only able to touch the object on screen in order to move it.
Any help, greatly appreciated.
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 | local background = display.newImage( "images/page63.png", true ) local chessboard = display.newImageRect( "images/chessboard.jpg", 500, 500 ) chessboard:setReferencePoint(display.TopLeftReferencePoint) chessboard.x = 100 chessboard.y = 50 local chess1 = display.newImageRect( "images/chess1.png", 300, 300 ) chess1:setReferencePoint(display.TopLeftReferencePoint) chess1.x = 100 chess1.y = 50 local chess2 = display.newImageRect( "images/chess2.png", 300, 300 ) chess2:setReferencePoint(display.TopLeftReferencePoint) chess2.x = 150 chess2.y = 150 local chess3 = display.newImageRect( "images/chess3.png", 300, 300 ) chess3:setReferencePoint(display.TopLeftReferencePoint) chess3.x = 250 chess3.y = 250 ---- functions local function wrapit (pieces) local obj = chess1 or chess2 or chess3 if obj.x < 10 then obj.x = 10 elseif obj.x > 738 then obj.x = 738 elseif obj.y < 10 then obj.y = 10 elseif obj.y > 994 then obj.y = 994 end end function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 wrapit(t) elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false --change(t) end end return true end chess1:addEventListener( "touch", onTouch ) chess2:addEventListener( "touch", onTouch ) chess3:addEventListener( "touch", onTouch ) |
Search the forums, there where about 2-3 threads on this topics in the last week alone.
Use physics Touch Joints. You can set the reference point to where ever you wish on your objects. Have a look at the DOCs for physics touch joints, and also look at the Air Hockey 2 template to se how objects can be dragged by a reference point.
-David
Does your image have a transparent area around it? If so, it sounds like the user is touching that area which is a part of the object.
Peach :)