I’m making a mini golf game in the theme of a blow fish, but I’m currently stuck on developing a firing mechanism of the golf ball object. I want to place a large circular touch area around the golf ball such that the player doesn’t need to be very precise in directly touching the golf ball. But, here’s my dilemma. I am able to adjust the force applied to the ball based on how far the touch is from the golf object and if the touch is released within the touch area, the golf ball fires. But, if it’s done outside of the circular area, then it doesn’t get released. How can I fix this?
local physics = require('physics') local math = require('math') local w, h = display.contentWidth, display.contentHeight local cX, cY = display.contentCenterX, display.contentCenterY physics.start() local blowfishGroup = display.newGroup() local blowfishGroup = display.newGroup() -- Create the blowfish local rad = 40 local blowfish = display.newCircle(blowfishGroup, 300, 50, rad) blowfish:setFillColor(.8,.3,0) -- Bloat fish force is set by a player moving the finger away from the canon blowfish.force = 0 blowfish.arrowLength = 0 physics.addBody(blowfish, 'dynamic', {radius = rad}) -- Minimum and maximum amount of force indicator local arrowLengthMin, arrowLenghtMax = 25, 200 -- Indicates force value local forceArrow = display.newRect(blowfishGroup, blowfish.x, blowfish.y , 15, 45) forceArrow.isVisible = false forceArrow.anchorX = 0.5; forceArrow.anchorY = 1 -- Touch area is larger than blowfish image so plyaer does not need to be very accurate with the fingers local touchMax = 300 local touchArea = display.newCircle(blowfishGroup, blowfish.x, blowfish.y, touchMax) touchArea.isVisible = true touchArea.isHitTestable = true touchArea:addEventListener('touch', blowfish) local touchAreaVis = display.newCircle(blowfishGroup, blowfish.x, blowfish.y, 0) touchAreaVis.isVisible = false touchAreaVis.isHitTestable = true touchAreaVis:setFillColor(1,0,0,.2) function blowfish:setForce(radius, rotation) self.rotation = math.deg(rotation) forceArrow.rotation = 90 - self.rotation self.forceX, self.forceY = radius \* .5 \* math.cos(rotation), radius \* .5 \* -math.sin(rotation) return self.rotation end function blowfish:engageforce() self:applyForce(self.forceX, self.forceY, self.x, self.y) self.linearDamping = 0.85 --self.angularDamping = 0.5 end function blowfish:enterFrame() local vx, vy = blowfish:getLinearVelocity() if vx \< 0.1 and vy \< 0.1 then touchArea.x, touchArea.y = blowfish.x, blowfish.y touchAreaVis.x, touchAreaVis.y = blowfish.x, blowfish.y forceArrow.x, forceArrow.y = blowfish.x, blowfish.y touchArea:addEventListener('touch',blowfish) end end Runtime:addEventListener('enterFrame', blowfish) function blowfish:touch(event) if event.phase == 'began' then -- Arrow appears and force controlled by user self.isFocused = true forceArrow.isVisible = true touchAreaVis.isVisible = true elseif event.phase == 'moved' then local touchX, touchY = event.x - self.x, self.y - event.y local rotation = math.atan2( touchY , touchX ) local radius = math.sqrt( touchX ^ 2 + touchY ^ 2) touchAreaVis.path.radius = radius if radius \< touchMax then self:setForce(radius, rotation) else self:setForce(30,rotation) end elseif event.phase == 'ended' then -- Releases the blowfish based on force display.getCurrentStage():setFocus(self,nil) self.isFocused = false forceArrow.isVisible = false touchArea:removeEventListener('touch', blowfish) self:engageforce() end return true end