Sticky problem : How to drag an object immediately after its creation?

Hi,

If you try the code below ( please do it :slight_smile: ), 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]

Simplest way I guess :slight_smile:

[lua]local function onSquareCreateButton(event)
if event.phase == “began” then
draggableCircle = circleCreator.new{}
draggableCircle:touch( event )
end
end[/lua] [import]uid: 41153 topic_id: 30046 reply_id: 120311[/import]

In your main.lua you will likely need to give the focus to the circle.

Try adding:

display.getCurrentStage():setFocus( draggableCircle, event.id )  
 draggableCircle.isFocus = true  

just below:

draggableCircle = circleCreator.new{}

I’ve not tested it, but if that fails you can use dispatchEvent() to forward the event to the new object.

[import]uid: 19626 topic_id: 30046 reply_id: 120317[/import]

Setting focus to circle won’t be enough.

Dispatch event will do exactly same thing as my code does, so both would work. [import]uid: 41153 topic_id: 30046 reply_id: 120318[/import]

@delwing : wahooooo! It works! Simplest way as you said :slight_smile: Thanks so much, I was totally blind on this stuff…

@robmiracle : thanks for your help, but I already give the focus to the circle in the CircleDrag.lua file

For people who are interested by the subject, below is the new main.lua file (
If you have any question, please ask :slight_smile:
Olivier

CODE ( CircleDrag.lua file is unchanged, check above, build.settings and config.lua files are the default one)

new main.lua file

[code]
– 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{}
– we Pass the event parameter to the draggableCircle touch function
–So the red circle is draggable immediately after I’ve touched the blue square button without
–removing my finger from the screen. Simple and magical, thanks delwing :slight_smile:

draggableCircle:touch(event)
end
end

–Set the listener for the squareCreationButton
squareCreateButton:addEventListener( “touch”, onSquareCreateButton )
[import]uid: 160159 topic_id: 30046 reply_id: 120461[/import]

You are welcome :slight_smile:

Of course key thing here is still

display.getCurrentStage():setFocus( myCircle, event.id )

This is very powerful, while having touch event.id you can juggle with objects that have focus :slight_smile: [import]uid: 41153 topic_id: 30046 reply_id: 120463[/import]

Simplest way I guess :slight_smile:

[lua]local function onSquareCreateButton(event)
if event.phase == “began” then
draggableCircle = circleCreator.new{}
draggableCircle:touch( event )
end
end[/lua] [import]uid: 41153 topic_id: 30046 reply_id: 120311[/import]

In your main.lua you will likely need to give the focus to the circle.

Try adding:

display.getCurrentStage():setFocus( draggableCircle, event.id )  
 draggableCircle.isFocus = true  

just below:

draggableCircle = circleCreator.new{}

I’ve not tested it, but if that fails you can use dispatchEvent() to forward the event to the new object.

[import]uid: 19626 topic_id: 30046 reply_id: 120317[/import]

Setting focus to circle won’t be enough.

Dispatch event will do exactly same thing as my code does, so both would work. [import]uid: 41153 topic_id: 30046 reply_id: 120318[/import]

@delwing : wahooooo! It works! Simplest way as you said :slight_smile: Thanks so much, I was totally blind on this stuff…

@robmiracle : thanks for your help, but I already give the focus to the circle in the CircleDrag.lua file

For people who are interested by the subject, below is the new main.lua file (
If you have any question, please ask :slight_smile:
Olivier

CODE ( CircleDrag.lua file is unchanged, check above, build.settings and config.lua files are the default one)

new main.lua file

[code]
– 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{}
– we Pass the event parameter to the draggableCircle touch function
–So the red circle is draggable immediately after I’ve touched the blue square button without
–removing my finger from the screen. Simple and magical, thanks delwing :slight_smile:

draggableCircle:touch(event)
end
end

–Set the listener for the squareCreationButton
squareCreateButton:addEventListener( “touch”, onSquareCreateButton )
[import]uid: 160159 topic_id: 30046 reply_id: 120461[/import]

You are welcome :slight_smile:

Of course key thing here is still

display.getCurrentStage():setFocus( myCircle, event.id )

This is very powerful, while having touch event.id you can juggle with objects that have focus :slight_smile: [import]uid: 41153 topic_id: 30046 reply_id: 120463[/import]