Plug and play code, try it

Hi everyone.

I have this simple code and my goal is, when i create the object by clicking on the spawner i will not take off my finger of the screen and i can drag the object to the pipe.

If you can, try it and if you have the solution and can help me, it will be great, Thanks

[code]
function new()

– Librarys
local physics = require “physics”

– Groups
local LocalGroup = display.newGroup()

physics.start()
physics.setGravity( 0, 0 )

local pipe = display.newRect( 150, 0, 20, 80 )
physics.addBody(pipe, “static”, {isSensor = true})

pipe:addEventListener(“collision”, pipe)

function pipe:collision (event)
event.other:removeSelf()
end

local spawner = display.newRect( 140, 400, 40, 40 )
spawner:setFillColor(150, 0, 0)
LocalGroup:insert(spawner)

local function spawnMyObject (event)
if event.phase == “began” then
local myObject = display.newCircle( spawner.x, spawner.y, 25 )
physics.addBody(myObject, “dynamic”)

local function dragObject( event )
local t = event.target
local phase = event.phase

if phase == “began” then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif t.isFocus then

if (phase == “moved”) then
event.target.bodyType = “dynamic”
t.x = event.x - t.x0
t.y = event.y - t.y0

myObject.x = t.x
myObject.y = t.y

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end

myObject:addEventListener(“touch”, dragObject)
end
end
spawner:addEventListener(“touch”, spawnMyObject)

return LocalGroup

end

[/code] [import]uid: 26056 topic_id: 22984 reply_id: 322984[/import]

up [import]uid: 26056 topic_id: 22984 reply_id: 92060[/import]

Hey @Blickon, is this what you had in mind?

[code]
function new()

– Librarys
local physics = require “physics”

– Groups
local LocalGroup = display.newGroup()

physics.start()
physics.setGravity( 0, 0 )

local pipe = display.newRect( 150, 0, 20, 80 )
physics.addBody(pipe, “static”, {isSensor = true})

pipe:addEventListener(“collision”, pipe)

function pipe:collision (event)
event.other:removeSelf()
end

local spawner = display.newRect( 140, 400, 40, 40 )
spawner:setFillColor(150, 0, 0)
LocalGroup:insert(spawner)

local function spawnMyObject (event)

if event.phase == “began” then
local myObject = display.newCircle( spawner.x, spawner.y, 25 )
physics.addBody(myObject, “dynamic”)

local function dragObject( event )
local t = event.target
local phase = event.phase

print("object phase: " … phase)

if phase == “moved” then
if not t.isFocus then
print(“in moved”)
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
end

end

if t.isFocus then
print(“in infocus”)
if (phase == “moved”) then
event.target.bodyType = “dynamic”
t.x = event.x - t.x0
t.y = event.y - t.y0

myObject.x = t.x
myObject.y = t.y

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

return true
end

myObject:addEventListener(“touch”, dragObject)
end
end
spawner:addEventListener(“touch”, spawnMyObject)

return LocalGroup

end
new()
[/code] [import]uid: 120 topic_id: 22984 reply_id: 92067[/import]

Wow is really it, the problem was on event.phase ==“began” and i am not seeing it :slight_smile:

Thanks you so much, it is really important for me. Thanks bryan.vaccaro :slight_smile: [import]uid: 26056 topic_id: 22984 reply_id: 92071[/import]

Now i am trying to drag my ball, only inside of the screen, but i need some help to resolve this little problem, i stopped my object when it is less or equal _W*0, but if we drag the object out of the screen it will stop the object and we need to continue dragging it to out of the screen and at the same time drag to up or to bottom, it will not drag and should drag, can anyone give me a hand with this little problem?
Thanks

[code]
function new()

– Librarys
local physics = require “physics”

local _W = display.contentWidth
local _H = display.contentHeight

– Groups
local LocalGroup = display.newGroup()

physics.start()
physics.setGravity( 0, 0 )

local pipe = display.newRect( 150, 0, 20, 80 )
physics.addBody(pipe, “static”, {isSensor = true})

pipe:addEventListener(“collision”, pipe)

function pipe:collision (event)
event.other:removeSelf()
end

local spawner = display.newRect( 140, 400, 40, 40 )
spawner:setFillColor(150, 0, 0)
LocalGroup:insert(spawner)

local function spawnMyObject (event)

if event.phase == “began” then
local myObject = display.newCircle( spawner.x, spawner.y, 25 )
physics.addBody(myObject, “dynamic”)

local function dragObject( event )
local t = event.target
local phase = event.phase

print("object phase: " … phase)

if phase == “moved” then
if not t.isFocus then
print(“in moved”)
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
end

end

if t.isFocus then
print(“in infocus”)
if (phase == “moved”) then
event.target.bodyType = “dynamic”

local horizontalDrag = event.x - t.x0

if t.x > _W*0 then
t.x = event.x - t.x0
t.y = event.y - t.y0

myObject.x = t.x
myObject.y = t.y

elseif t.x <= _W*0 and horizontalDrag > 0 then

t.x = horizontalDrag
t.y = event.y - t.y0

myObject.x = t.x
myObject.y = t.y

end

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

return true
end

myObject:addEventListener(“touch”, dragObject)

end
end
spawner:addEventListener(“touch”, spawnMyObject)

return LocalGroup

end

new()
[/code] [import]uid: 26056 topic_id: 22984 reply_id: 94852[/import]

Now i am trying to drag my ball, only inside of the screen, but i need some help to resolve this little problem, i stopped my object when it is less or equal _W*0, but if we drag the object out of the screen it will stop the object and we need to continue dragging it to out of the screen and at the same time drag to up or to bottom, it will not drag and should drag, can anyone give me a hand with this little problem?
Thanks

[code]
function new()

– Librarys
local physics = require “physics”

local _W = display.contentWidth
local _H = display.contentHeight

– Groups
local LocalGroup = display.newGroup()

physics.start()
physics.setGravity( 0, 0 )

local pipe = display.newRect( 150, 0, 20, 80 )
physics.addBody(pipe, “static”, {isSensor = true})

pipe:addEventListener(“collision”, pipe)

function pipe:collision (event)
event.other:removeSelf()
end

local spawner = display.newRect( 140, 400, 40, 40 )
spawner:setFillColor(150, 0, 0)
LocalGroup:insert(spawner)

local function spawnMyObject (event)

if event.phase == “began” then
local myObject = display.newCircle( spawner.x, spawner.y, 25 )
physics.addBody(myObject, “dynamic”)

local function dragObject( event )
local t = event.target
local phase = event.phase

print("object phase: " … phase)

if phase == “moved” then
if not t.isFocus then
print(“in moved”)
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
end

end

if t.isFocus then
print(“in infocus”)
if (phase == “moved”) then
event.target.bodyType = “dynamic”

local horizontalDrag = event.x - t.x0

if t.x > _W*0 then
t.x = event.x - t.x0
t.y = event.y - t.y0

myObject.x = t.x
myObject.y = t.y

elseif t.x <= _W*0 and horizontalDrag > 0 then

t.x = horizontalDrag
t.y = event.y - t.y0

myObject.x = t.x
myObject.y = t.y

end

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

return true
end

myObject:addEventListener(“touch”, dragObject)

end
end
spawner:addEventListener(“touch”, spawnMyObject)

return LocalGroup

end

new()
[/code] [import]uid: 26056 topic_id: 22984 reply_id: 94853[/import]