local physics = require "physics"
physics.setDrawMode( "hybrid" )
physics.start( )
physics.setGravity( 0, 2)
local shape = display.newRect( 0, 0, 50, 50 )
shape.isVisible = false
local touchArea = display.newRect( 70, 200, 150, 150 )
touchArea.alpha = 0.3
function move( event )
local phase = event.phase
local T = event.target
if "began" == phase then
physics.addBody (shape, "dynamic", { friction = 5, bounce = 0, density = 5 })
shape.isVisible = true
display.getCurrentStage():setFocus( T )
T.isFocus = true
shape.isVisible = true
shape.x = event.x --ycoord where touch is
shape.y = event.y --ycoord is where touch is
shape.tempJoint = physics.newJoint( "touch", shape, shape.x, shape.y )
shape.tempJoint.frequency = 2
shape.tempJoint.maxForce = 10000
elseif T.isFocus then
if "moved" == phase then
shape.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
shape.tempJoint:removeSelf()
end
end
end
touchArea:addEventListener("touch", move)
I need the smaller square to stay inside the larger square, but the smaller square also to be able to moved about in the larger square when the user isn’t touching the larger square.
So the smaller square can be controlled from anywhere on the screen, once the larger square has been touched, just the small square cannot leave the larger square by more than half its height or width?
Any ideas please? [import]uid: 87611 topic_id: 15202 reply_id: 315202[/import]