I use this right now. I have objects that are static and turn dynamic after touching, or the other way around dynamic to static. Not sure if this will apply to you, if not it’s still fun to play with.
Etc etc.
This is basic stuff for the dragging, based on various code I’ve looked at and samples etc etc.
Darkmod will have the super advanced knowledge, I’m just sharing code that may or may not apply to your situation.
I am still noob, so yea.
[code]
local physics = require “physics”
physics.setPositionIterations(60) – prevents *some pass through
physics.setVelocityIterations(60) --another counter measure to “box2d tunneling”
physics.start(true)
physics.setGravity( 0,10)
physics.setDrawMode( “normal”)
physics.setScale(60)
–=================================
–DECLARE WIDTH AND HEIGHT SHORTCUT VARIABLES
–=================================
local _W = display.contentWidth
local _H = display.contentHeight
–=================================
–DECLARE GROUPS
–=================================
local dgroup = display.newGroup() – DRAG OBJECT GROUP
local ggroup = display.newGroup() – GOAL OBJECT GROUP
local d1 = display.newRect (_W/2-50,_H/2,130,130)
d1.myName = “dobject”
d1:setFillColor (0,255,0)
physics.addBody(d1, “dynamic”)
dgroup:insert(d1 )
–=================================
–DRAG CODE
–CREATES TEMPORARY TOUCH JOINTS BASED ON X AND Y AT TIME OF TOUCH
–=================================
local function dragBody( event )
–print (“drag started”)
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if “began” == phase then
stage:setFocus( body, event.id )
body.isFocus = true
–Change body to dynamic
event.target.bodyType = “dynamic”
– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y )
–THIS PREVENTS ROTATION WHEN DRAGGING
–COMMENT OUT IF YOU DON’T WANT THIS
body.isFixedRotation = true
elseif body.isFocus then
if “moved” == phase then
– Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )
elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false
–I use this to pass information somewhere else
–event.target:setLinearVelocity( 0, 0 )
–event.target.angularVelocity = 0
–=======================================================================
–=======================================================================
–After the event ends, change body back to dynamic OR static.
– event.target.bodyType = “dynamic”
event.target.bodyType = “static”
–I haven’t gotten this one working. I’m sure there is a way, I haven’t spent enough time with it.
–event.target.bodyType = “kinematic”
–=======================================================================
–=======================================================================
– Remove the joint when the touch ends
body.tempJoint:removeSelf()
–print (“drag ended”)
end
end
– Stop further propagation of touch event
return true
end
d1:addEventListener( “touch”, dragBody )
[/code] [import]uid: 61600 topic_id: 26371 reply_id: 107105[/import]