Hi,
If you try the code below ( please do it
), you can see that :
- One touch on the blue square button creates a red circle just above the button
- If you don’t remove your finger (or cursor) from the screen, you can’t drag the circle
- if you remove your finger (or cursor) from the screen and touch a second time the circle, this time, you can drag it.
WHAT I’D LIKE TO DO:
The red circle has to be draggable immediately after I’ve touched the blue square button without removing my finger from the screen.
Thanks for your help :))
CODE ( build.settings and config.lua files are the default one)
main.lua file
-- We need this module to create a draggable circle
local circleCreator = require("CircleDrag")
local draggableCircle
-- Create and init the button
-- If you press this button, you create a circle which you can drag
local squareCreateButton = display.newRect( 0, 0, 50 , 50 )
squareCreateButton:setReferencePoint(display.CenterReferencePoint)
squareCreateButton.x = display.contentWidth/2
squareCreateButton.y = display.contentHeight/2
squareCreateButton:setFillColor(0 , 0, 255)
--The listener function which is called when the squareCreateButton is touched
local function onSquareCreateButton(event)
if event.phase == "began" then
draggableCircle = circleCreator.new{}
end
end
--Set the listener for the squareCreationButton
squareCreateButton:addEventListener( "touch", onSquareCreateButton )
CircleDrag.lua file
[code]
module(…, package.seeall)
function new()
local myCircle = display.newCircle( 0, 0, 20 )
myCircle:setReferencePoint(display.CenterReferencePoint)
myCircle.x = display.contentWidth/2
myCircle.y = display.contentHeight/2
myCircle:setFillColor(255, 0, 0)
– touch listener function
function myCircle:touch(event)
if event.phase == “began” then
– begin focus
display.getCurrentStage():setFocus( myCircle, event.id )
myCircle.isFocus = true
myCircle.markX = myCircle.x – store x location of myCircle
myCircle.markY = myCircle.y – store y location of myCircle
elseif myCircle.isFocus then
if event.phase == “moved” then
– drag touch object
local x = (event.x - event.xStart) + myCircle.markX
local y = (event.y - event.yStart) + myCircle.markY
– move object based on calculations above
myCircle.x = x
myCircle.y = y
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( myCircle, nil )
myCircle.isFocus = false
end
end
return true
end
myCircle:addEventListener( “touch”, myCircle )
return myCircle
end
[/code] [import]uid: 160159 topic_id: 30046 reply_id: 330046[/import]