drag an object after pressing a button to create it

[I am making a level editor!]

As the subject suggests, I have buttons that you press to generate objects. I would like to touch the button, which generates a display object under my finger, and then be able to drag the display object without lifting my finger from the initial button press. Does anyone have any experience with this, or know if it is possible? (Surely it is)
Thanks,
Dane
PS: My first game, “Hero the Hamster” is available on the App Store and Android, through:
www.dogfishapps.com :slight_smile:
It is one of the most satisfyingly designed puzzle games you will ever play (and gets difficult!) And the best part is it was made in Corona!!! [import]uid: 117490 topic_id: 30122 reply_id: 330122[/import]

Hey Dane,

I did finally get to play Hero the Hamster, although I haven’t yet gotten around to messaging you back on FB, sorry about that. (Travel has thrown me way off on social media.) Great stuff! :slight_smile:

For your Q, absolutely it is. Here is a sample I previously wrote for someone on the forum where you press the red square to spawn and immediately drag an object. (It is removed when it touches white rectangle.)

[lua]require ( “physics” )
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)

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)
myObject.x = event.x
myObject.y = event.y
end
myObject:addEventListener(“touch”, dragObject)
end
end
spawner:addEventListener(“touch”, spawnMyObject)[/lua]

A fast swipe will drop the object though as I didn’t do setFocus, so be sure to add that.

Peach :slight_smile: [import]uid: 52491 topic_id: 30122 reply_id: 120606[/import]

Hey Dane,

I did finally get to play Hero the Hamster, although I haven’t yet gotten around to messaging you back on FB, sorry about that. (Travel has thrown me way off on social media.) Great stuff! :slight_smile:

For your Q, absolutely it is. Here is a sample I previously wrote for someone on the forum where you press the red square to spawn and immediately drag an object. (It is removed when it touches white rectangle.)

[lua]require ( “physics” )
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)

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)
myObject.x = event.x
myObject.y = event.y
end
myObject:addEventListener(“touch”, dragObject)
end
end
spawner:addEventListener(“touch”, spawnMyObject)[/lua]

A fast swipe will drop the object though as I didn’t do setFocus, so be sure to add that.

Peach :slight_smile: [import]uid: 52491 topic_id: 30122 reply_id: 120606[/import]