Thanks for the replies guys.
@Rob - looking back at your code does the controller only allow for four direction movement (ie not free movement of the object) and I would need to add code to enable this?
@Michael W - thanks for the info although I’ve downloaded the latest daily build and cannot find the example you have mentioned…
@roaminggamer - Ah that makes sense now looking more at your code. I’ll try and get my head round that code, have just started trying to get my head around the way you set-up object listening for event so i’ll have to look into that further.
The old code below made by X-Pressive was working for me but the issue I have is that while I’m using the joystick I cannot click elsewhere on the screen without it screwing up the joystick. Is there something I can enter that will stop this from happening?
local playerM = require("player") ]]-- local js = {} local Pi = math.pi local Sqr = math.sqrt local Rad = math.rad local Sin = math.sin local Cos = math.cos local Ceil = math.ceil local Atan2 = math.atan2 local NewStick local Group ---------------------------------------------------------------- -- FUNCTION: CREATE ---------------------------------------------------------------- function NewStick( Props ) Group = display.newGroup() Group.x = Props.x Group.y = Props.y Group.Timer = nil Group.angle = 0 Group.distance = 0 Group.percent = 0 Group.maxDist = Props.borderSize Group.snapBackSpeed = Props.snapBackSpeed ~= nil and Props.snapBackSpeed or .7 --Group.Border = display.newCircle(0,0,Props.borderSize) -- picture here? Group.Border = display.newImageRect("joystickmain1a.png",64,64) --Group.Border.strokeWidth = 1 --Group.Border:setFillColor (255,0,0,46) --Group.Border:setStrokeColor(Props.R,Props.G,Props.B,255) Group:insert(Group.Border) --Group.Thumb = display.newCircle(0,0,Props.thumbSize) -- picture here? Group.Thumb = display.newImageRect("joystickmain1b.png", 48, 48) -- picture here? -- Group.Thumb.strokeWidth = 1 -- Group.Thumb:setFillColor (Props.R,Props.G,Props.B,96) -- Group.Thumb:setStrokeColor(Props.R,Props.G,Props.B,255) Group.Thumb.x0 = 0 Group.Thumb.y0 = 0 Group:insert(Group.Thumb) --------------------------------------------- -- METHOD: DELETE STICK --------------------------------------------- function Group:delete() self.Border = nil self.Thumb = nil if self.Timer ~= nil then timer.cancel(self.Timer); self.Timer = nil end self:removeSelf() end --------------------------------------------- -- METHOD: MOVE AN OBJECT --------------------------------------------- function Group:move(Obj, maxSpeed, rotate) if rotate == true then Obj.rotation = self.angle end print("self.angle = "..tostring(self.angle)) print("self.distance = "..tostring(self.distance)) print("maxSpeed \* self.percent = "..tostring(maxSpeed \* self.percent)) Obj.x = Obj.x + Cos( Rad(self.angle-90) ) \* (maxSpeed \* self.percent) Obj.y = Obj.y + Sin( Rad(self.angle-90) ) \* (maxSpeed \* self.percent) end --------------------------------------------- -- GETTER METHODS --------------------------------------------- function Group:getDistance() return self.distance end function Group:getPercent () return self.percent end function Group:getAngle () return Ceil(self.angle) end --------------------------------------------- -- HANDLER: ON DRAG --------------------------------------------- Group.onDrag = function ( event ) local T = event.target -- THUMB local S = T.parent -- STICK local phase = event.phase local ex,ey = S:contentToLocal(event.x, event.y) ex = ex - T.x0 ey = ey - T.y0 if "began" == phase then if S.Timer ~= nil then timer.cancel(S.Timer); S.Timer = nil end display.getCurrentStage():setFocus( T ) T.isFocus = true -- STORE INITIAL POSITION T.x0 = ex - T.x T.y0 = ey - T.y elseif T.isFocus then if "moved" == phase then ----------- S.distance = Sqr (ex\*ex + ey\*ey) if S.distance \> S.maxDist then S.distance = S.maxDist end S.angle = ( (Atan2( ex-0,ey-0 )\*180 / Pi) - 180 ) \* -1 S.percent = S.distance / S.maxDist ----------- T.x = Cos( Rad(S.angle-90) ) \* (S.maxDist \* S.percent) T.y = Sin( Rad(S.angle-90) ) \* (S.maxDist \* S.percent) elseif "ended"== phase or "cancelled" == phase then T.x0 = 0 T.y0 = 0 T.isFocus = false display.getCurrentStage():setFocus( nil ) S.Timer = timer.performWithDelay( 33, S.onRelease, 0 ) S.Timer.MyStick = S --added so that angle reverted to 0 when stick is released S.angle = 0 end end -- STOP FURTHER PROPAGATION OF TOUCH EVENT! return true end --------------------------------------------- -- HANDLER: ON DRAG RELEASE --------------------------------------------- Group.onRelease = function( event ) local S = event.source.MyStick local T = S.Thumb local dist = S.distance \> S.maxDist and S.maxDist or S.distance dist = dist \* S.snapBackSpeed T.x = Cos( Rad(S.angle-90) ) \* dist T.y = Sin( Rad(S.angle-90) ) \* dist local ex = T.x local ey = T.y ----------- S.distance = Sqr (ex\*ex + ey\*ey) if S.distance \> S.maxDist then S.distance = S.maxDist end S.angle = ( (Atan2( ex-0,ey-0 )\*180 / Pi) - 180 ) \* -1 S.percent = S.distance / S.maxDist ----------- if S.distance \< .5 then S.distance = 0 S.percent = 0 T.x = 0 T.y = 0 timer.cancel(S.Timer); S.Timer = nil end end Group.Thumb:addEventListener( "touch", Group.onDrag ) return Group end js.NewStick = NewStick return js