main.lua
local widget = require( "widget" ) local joystickClass = require( "joystick" ) \_W = display.contentWidth \_H = display.contentHeight local myGroup = display.newGroup() local game = display.newGroup(); game.x = 0 function moveTo(object, params) local T = display.screenOriginY -- Top local L = display.screenOriginX -- Left local R = display.viewableContentWidth - L -- Right local B = display.viewableContentHeight - T-- Bottom local cX, cY = display.contentCenterX, display.contentCenterY object.x, object.y = 0, 0 local bounds = object.contentBounds local offsetX = (bounds.xMax + bounds.xMin) \* 0.5 local offsetY = (bounds.yMax + bounds.yMin) \* 0.5 local hW, hH = 0.5 \* object.contentWidth , 0.5 \* object.contentHeight if params.left then object.x = params.left+L + hW - offsetX elseif params.right then object.x = R-params.right - hW - offsetX elseif params.centerX then object.x = params.centerX + cX - offsetX end if params.bottom then object.y = B-params.bottom - hH - offsetY elseif params.top then object.y = params.top + T + hH - offsetY elseif params.centerY then object.y = params.centerY + cY - offsetY end return object end local grass = display.newRect(0,0,800,700) grass:setFillColor(0.6,0.2,0.2) grass.x , grass.y = \_W/2+150 , \_H/2+380 myGroup:insert(grass) local wall3 = display.newRect(0,0,300,60) wall3:setFillColor(0.5,0.3,0.2) moveTo(wall3, {centerX=130, centerY= -10}) myGroup:insert(wall3) myGroup.xScale , myGroup.yScale = 0.5 , 0.5 game:insert(myGroup) player = display.newRect(111,111,10,10) player:setFillColor(0.3,0.1,0.2) local function move (xStrength , yStrength ) -- Set Plane Speed Mutliplyer local speed = 8 -- Move X if xStrength ~= false then player.x = math.floor( player.x + ( xStrength \* speed ) ) player:toFront() end -- Move Y if yStrength ~= false then player.y = math.floor( player.y + ( yStrength \* speed ) ) player:toFront() end if player.x \< 12 then player.x = 12 elseif player.x \> \_W-10 then player.x = \_W-10 end if player.y \< 0 then player.y = 0 elseif player.y \> \_H-100 then player.y = \_H-100 end end function planeControl( event ) local joyX =event.joyX local joyY =event.joyY move( joyX , joyY ) end joystick = joystickClass.newJoystick{ outerImage = "", outerRadius = 60, outerAlpha = 0, innerImage = "joystickInner.png", innerRadius = "", innerAlpha = 0.8, backgroundImage = "joystickDial.png", background\_x = 0, background\_y = 0, backgroundAlpha = 0.8, position\_x = \_W-130, position\_y =\_H-130, ghost = 155, joystickAlpha = 0.8, joystickFade = true, joystickFadeDelay = 2000, onMove = planeControl } joystick.xScale , joystick.yScale = 1,1 joystick.alpha = 0.8 moveTo(joystick, {bottom=0, left=0}) function moveCamera() if (player.x \> 0 and player.x \< 1000) then game.x = -player.x end if (player.y \> 10 and player.y \< 220) then game.y =-player.y end end function zoomOut(e) if e.phase == "began" then Runtime:removeEventListener("enterFrame",moveCamera) myGroup.xScale , myGroup.yScale = 0.5,0.5 end end local function zoom(e) if e.phase == "began" then myGroup.xScale , myGroup.yScale = 1 ,1 Runtime:addEventListener( "enterFrame", moveCamera ) elseif e.phase == "ended" then end end button = display.newRect(200,300,40,40) button:addEventListener("touch",zoom) button2 = display.newRect(250,300,40,40) button2:addEventListener("touch",zoomOut) player:toFront()
I have another problem when I zoomOut the object move to different location
joystick.lua
module(..., package.seeall) system.activate( "multitouch" ) local function calculateJoystick( this ) -- Setup Values local joystickTouch = this.joystickTouch local newXPosition = this.joyCentreX local newYPosition = this.joyCentreY local max = this.joyMax local pi = math.pi local sqrt = math.sqrt local atan2 = math.atan2 local sin = math.sin local cos = math.cos -- Calculate X and Y local xDist = joystickTouch.x - newXPosition local yDist = joystickTouch.y - newYPosition -- Calcualte Distance local distance = sqrt ( ( xDist \* xDist ) + ( yDist \* yDist ) ) -- Set Max Distance if distance \> max then distance = max end -- Calculate Angle local angle = ( atan2( yDist , xDist ) \* ( 180 / pi ) ) + 90 if angle \< 0 then angle = 360 + angle end -- Calculate X and Y Limited To Outer Circle local xFinal = ( sin( angle \* pi / 180 ) \* distance ) local yFinal = ( cos( angle \* pi / 180 ) \* distance ) -- Normalize distance = distance / max xFinal = xFinal / max yFinal = yFinal / max -- Set New Values this.joyVector = distance this.joyAngle = angle this.joyX = xFinal this.joyY = -yFinal end ------------------------------------------------------------------------------------------------------------------------ -- Position Inner Joystick local function positionJoystickInner( this ) -- Setup Values local newXPosition = this.joyCentreX local newYPosition = this.joyCentreY local max = this.joyMax local joyX = this.joyX local joyY = this.joyY local floor = math.floor -- Set New Position if joyX ~= false and joyY ~= false then this.joystickInner.x = floor( newXPosition + ( joyX \* max ) ) this.joystickInner.y = floor( newYPosition + ( joyY \* max ) ) else -- Reset Position this.joystickInner.x = floor( newXPosition ) this.joystickInner.y = floor( newYPosition ) end end ------------------------------------------------------------------------------------------------------------------------ -- Handle Joystick Touch local function newJoystickHandler( self , event ) -- Setup Values local joystick = self.parent local this = event.target local phase = event.phase local onMove = joystick.onMove -- Event if "began" == phase then -- Start Focus display.getCurrentStage():setFocus( this , event.id ) this.isFocus = true -- Store Initial Position this.x0 = event.x - this.x this.y0 = event.y - this.y -- Show Ghost --joystick.joystickTouch.alpha = 1 -- Fade Effect if joystick.fadeEffect == true then if joystick.tween then transition.cancel( joystick.tween ) end local tweenTime = ( 1 - joystick.alpha ) \* 300 joystick.tween = transition.to( joystick, { alpha=1, time=tweenTime, delay=0 } ) end elseif this.isFocus then if "moved" == phase then -- Capture Event Position this.x = event.x - this.x0 this.y = event.y - this.y0 -- Perform Calculations calculateJoystick( joystick ) -- Set Position Of Inner Joystick positionJoystickInner( joystick ) elseif "ended" == phase or "cancelled" == phase then -- End Focus display.getCurrentStage():setFocus( this , nil ) this.isFocus = false -- Reset Position To Joystick Centre this.x = joystick.joyCentreX this.y = joystick.joyCentreY -- Set Values To False joystick.joyVector = false joystick.joyAngle = false joystick.joyX = false joystick.joyY = false -- Set Position Of Inner Joystick positionJoystickInner( joystick ) -- Hide ghost joystick.joystickTouch.alpha = 0.01 -- Fade Effect if joystick.fadeEffect == true then local tweenTime = ( 1 - joystick.fadeEffectAlpha ) \* 500 if joystick.tween then transition.cancel( joystick.tween ) end if joystick.alpha \< 1 then joystick.tween = transition.to( joystick , { alpha=joystick.fadeEffectAlpha , time=tweenTime , delay=0 } ) else joystick.tween = transition.to( joystick , { alpha=joystick.fadeEffectAlpha , time=tweenTime , delay=joystick.fadeEffectDelay } ) end end end end -- End Event return true end ------------------------------------------------------------------------------------------------------------------------ -- Generates A New Joystick function newJoystick( params ) -- New Joystick Group local joystick = display.newGroup() -- Load Outer Image if not params.outerRadius or params.outerRadius == "" then params.outerRadius = 60 end if params.outerImage and params.outerImage ~= "" then local joystickOuter = display.newImage( params.outerImage ) joystick:insert( joystickOuter ) joystick.joystickOuter = joystickOuter else -- Or Draw Outer Circle local joystickOuter = display.newCircle( params.outerRadius , params.outerRadius , params.outerRadius ) joystickOuter:setFillColor( 255 , 255 , 255 ) joystick:insert( joystickOuter ) joystick.joystickOuter = joystickOuter end if not params.outerAlpha or params.outerAlpha == "" then params.outerAlpha = 1 end joystick.joystickOuter.alpha = params.outerAlpha -- Load Inner Image if not params.innerRadius or params.innerRadius == "" then params.innerRadius = 24 end if params.innerImage and params.innerImage ~= "" then local joystickInner = display.newImage( params.innerImage ) joystick:insert( joystickInner ) joystick.joystickInner = joystickInner else -- Or Draw Inner Circle local joystickInner = display.newCircle( params.outerRadius , params.outerRadius , params.innerRadius ) joystickInner:setFillColor( 0 , 0 , 0 ) joystick:insert( joystickInner ) joystick.joystickInner = joystickInner end if not params.innerAlpha or params.innerAlpha == "" then params.innerAlpha = 1 end joystick.joystickInner.alpha = params.innerAlpha -- Generate Touch Object if not params.ghost or params.ghost == "" then params.ghost = 0 end joystickTouch = display.newCircle( joystick.joystickOuter.x , joystick.joystickOuter.y , joystick.joystickInner.width / 2 ) joystickTouch:setFillColor( 255 , 255 , 255 , params.ghost ) joystickTouch.alpha = 0.01 joystick:insert( joystickTouch ) joystick.joystickTouch = joystickTouch -- Position Joystick if not params.position\_x or params.position\_x == "" then params.position\_x = 15 end if not params.position\_y or params.position\_y == "" then params.position\_y = display.stageHeight - 15 - joystick.joystickOuter.height end joystick.x = params.position\_x joystick.y = params.position\_y -- Load Background Image if params.backgroundImage and params.backgroundImage ~= "" then local joystickBackground = display.newImage( params.backgroundImage ) if not params.background\_x then params.background\_x = 0 end joystickBackground.x = joystick.joystickOuter.x + params.background\_x if not params.background\_y then params.background\_y = 0 end joystickBackground.y = joystick.joystickOuter.y + params.background\_y joystick:insert( 1 , joystickBackground ) joystick.joystickBackground = joystickBackground if not params.backgroundAlpha then params.backgroundAlpha = 1 end joystick.joystickBackground.alpha = params.backgroundAlpha end -- Set Joystick Alpha if not params.joystickAlpha or params.joystickAlpha == "" then params.joystickAlpha = 1 end joystick.alpha = params.joystickAlpha -- Fade Effect if params.joystickFade == true then joystick.fadeEffect = true joystick.fadeEffectAlpha = joystick.alpha if not params.joystickFadeDelay or params.joystickFadeDelay == "" then params.joystickFadeDelay = 2000 end joystick.fadeEffectDelay = params.joystickFadeDelay end -- Set Default Values joystick.joyMax = ( joystick.joystickOuter.width / 2 ) - ( joystick.joystickInner.width / 2 ) joystick.joyCentreX = joystick.joystickOuter.x joystick.joyCentreY = joystick.joystickOuter.y joystick.joyVector = false joystick.joyAngle = false joystick.joyX = false joystick.joyY = false -- On Move Event if ( params.onMove and ( type(params.onMove) == "function" ) ) then joystick.enterFrame = params.onMove Runtime:addEventListener( "enterFrame" , joystick ) end -- Set Joystick Listener joystick.joystickTouch.touch = newJoystickHandler joystick.joystickTouch:addEventListener( "touch", joystick.joystickTouch ) -- Stop Function function joystick:joystickStop() -- End Focus display.getCurrentStage():setFocus( joystick.joystickTouch , nil ) joystick.joystickTouch.isFocus = false -- Reset Position To Joystick Centre joystick.joystickTouch.x = joystick.joyCentreX joystick.joystickTouch.y = joystick.joyCentreY joystick.joystickTouch.alpha = 0.01 joystick.joystickTouch:removeEventListener( "touch", joystick.joystickTouch ) joystick.joyVector = false joystick.joyAngle = false joystick.joyX = false joystick.joyY = false if joystick.enterFrame then Runtime:removeEventListener( "enterFrame" , joystick ) joystick.enterFrame( joystick ) end positionJoystickInner( joystick ) -- Fade Effect if joystick.fadeEffect == true then if joystick.alpha ~= joystick.fadeEffectAlpha then local tweenTime = ( joystick.alpha - joystick.fadeEffectAlpha ) \* 500 if joystick.tween then transition.cancel( joystick.tween ) end joystick.tween = transition.to( joystick , { alpha=joystick.fadeEffectAlpha , time=tweenTime } ) end end end -- Start Function function joystick:joystickStart() joystick.joyVector = false joystick.joyAngle = false joystick.joyX = false joystick.joyY = false if joystick.enterFrame then Runtime:addEventListener( "enterFrame" , joystick ) end joystick.joystickTouch:addEventListener( "touch", joystick.joystickTouch ) end -- Position Joystick Inner positionJoystickInner( joystick ) -- Return New Joystick return joystick end