Joystick like behaviour without groups? Plug and Play.

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]

Do you even know what “search” means?

http://developer.anscamobile.com/code/simple-analog-stick-joystick-module [import]uid: 79135 topic_id: 15202 reply_id: 56197[/import]

Can you read? “without groups”.

Edit:

I’ve also tried wrapping the moved phase code in:

  
if shape.x \> touchArea.x - touchArea.height / 2 and shape.x \< touchArea.x + touchArea.height / 2 then  
  
 if shape.y \> touchArea.y - touchArea.height / 2 and shape.y \< touchArea.y - touchArea.height / 2 then  
  
 shape.tempJoint:setTarget( event.x, event.y )  
  
 end  
end  
  

Yet this doesn’t work. [import]uid: 87611 topic_id: 15202 reply_id: 56198[/import]

here:

[code]

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

local function setObjectBounds(oObject)

local tBounds = oObject.contentBounds
oObject.xMin = tBounds.xMin
oObject.yMin = tBounds.yMin --+mlText.rowPixelHeight
oObject.xMax = tBounds.xMax
oObject.yMax = tBounds.yMax --+mlText.rowPixelHeight
oObject.objectWidth = tBounds.xMax - tBounds.xMin
oObject.objectHeight = tBounds.yMax - tBounds.yMin

tBounds = nil
return oObject
end
touchArea = setObjectBounds(touchArea)

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
local tX = event.x
local tY = event.y
if tX> touchArea.xMax then tX = touchArea.xMax end
if tY> touchArea.yMax then tY = touchArea.yMax end
if tX< touchArea.xMin then tX = touchArea.xMin end
if tY< touchArea.yMin then tY = touchArea.yMin end

shape.tempJoint:setTarget(tX, tY)

elseif “ended” == phase or “cancelled” == phase then

display.getCurrentStage():setFocus( nil )
shape.tempJoint:removeSelf()

end

end

end

touchArea:addEventListener(“touch”, move)

[/code] [import]uid: 13784 topic_id: 15202 reply_id: 56218[/import]