Hello!
I want to make a game where I can move a ball in any direction of the screen by touch and below is the code that works fine to do this, but the ball is going too fast. I want the ball to go slower but at a constant speed. I have already tried to give the ball a density but then the speed isn’t constant and then the speed depends on how far I reach my finger away from the ball.
Some help would be great!
Greetings,
Thodan
local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local physics = require("physics") physics.start() physics.setScale( 60 ) physics.setGravity( 0, 0 ) display.setStatusBar( display.HiddenStatusBar ) local background = display.newRect(centerX,centerY,\_W,\_H) background:setFillColor(1,1,1) local ball = display.newCircle(centerX, centerY, 16) physics.addBody(ball, "dynamic", {radius = 16}) ball.alpha = 0 ball:setFillColor(0,0,0) function shotBall( event ) local b = event.ball local phase = event.phase if phase == "began" then display.getCurrentStage():setFocus(b) ball.isFocus = true ball.x = event.x ball.y = event.y ball:setLinearVelocity(0,0) ball.angularVelocity = 0 local showBall = transition.to (ball, {alpha=1, xScale=1, yScale=1,time=200}) myLine = nil elseif ball.isFocus then if phase == "moved" then if (myLine) then myLine.parent:remove(myLine) end myLine = display.newLine( ball.x,ball.y, event.x,event.y ) myLine:setStrokeColor( 1, 1, 1, 50/255 ) myLine.strokeWidth = 9 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) ball.isFocus = false if ( myLine ) then myLine.parent:remove( myLine ) end ball:applyForce( (event.x - ball.x),(event.y - ball.y), ball.x, ball.y ) end end return true end background:addEventListener("touch", shotBall)