Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Dragging an object to the whole screen with less finger movement
Started by DevilDev May 29 2019 11:53 PM

1 reply to this topic
drag touch
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

DevilDev

[GLOBAL: userInfoPane.html]
DevilDev
  • Enthusiast

  • 69 posts
  • Corona SDK

I want to create a game where user moves his finger in the screen and an object is dragged corresponding to finger position.But the user will move his finger for short distance.But the object should move for long distance.

For example if the user moves his finger for 10px left,then the object should move (10*5)=50px left.

pink circle is the object.

black rectangle is where object should move.

red rectangle is where user can swipe his/her finger

Attached File  doubt.png   6.08KB   0 downloads

My code to drag is below

local physics = require"physics"
physics.start()
local xc = display.contentCenterX
local yc = display.contentCenterY
local w = display.actualContentWidth
local ball=display.newCircle(xc,yc,30)
ball.radius=30
local function drag(event)
	phase=event.phase
 if ("began" == phase) then
        display.currentStage:setFocus(ball)
        ball.touchOffsetX = event.x - ball.x
    elseif ("moved" == phase) then
        if (event.x - ball.touchOffsetX > ball.radius and event.x - ball.touchOffsetX < display.viewableContentWidth - ball.radius) then
            ball.x = event.x - ball.touchOffsetX
        end
    elseif ("ended" == phase or "cancelled" == phase) then
        display.currentStage:setFocus(nil)
    end
end
Runtime:addEventListener( "touch", drag)


[TOPIC: post.html]
#2

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 45 posts
  • Corona SDK

Hi Devil Dev, 

 

Try this

local centerX = display.contentCenterX
local centerY = display.contentCenterY

local userArea = display.newRect(centerX, centerY + 200, 200, 50)
userArea:setFillColor(1,0,0)

local ballArea = display.newRect(centerX, centerY - 200, 300, 80)
ballArea:setFillColor(0,0,1)

local ball = display.newCircle(ballArea.x, ballArea.y, 30)
ball:setFillColor(1,0.3,0)

local eventX                    -- important to declare this

local function touch(event)

	local target = event.target

	if (event.phase == "began") then

		eventX = event.x
		display.getCurrentStage():setFocus(target)

	elseif (event.phase == "moved") then

		ball.x = ball.x + (event.x - eventX) * 5
		eventX = event.x

	elseif (event.phase == "ended") then

		display.getCurrentStage():setFocus(nil)

	end
	return true

end

userArea:addEventListener("touch", touch)




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: drag, touch