Draggable object?

Hi. I want to make an image that I have draggable. The problem is, that the code I have allows this object to be dragged from anywhere on the screen. That’s not what I want.

The code that I have is:

[lua]local function startDrag( event )

local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

ballx0 = ball.x
bally0 = ball.y

print(x0, y0)

– Stop current motion, if any
ball:setLinearVelocity( 0, 0 )
ball.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
ball.x = ballx0 + event.x - x0
ball.y = bally0 + event.y - y0

end

– Stop further propagation of touch event!
return true
end

Runtime:addEventListener( “touch”, startDrag [/lua] [import]uid: 25216 topic_id: 11706 reply_id: 311706[/import]

Try changing the last line to this:

  
ball:addEventListener( "touch", startDrag )  
  

[import]uid: 5833 topic_id: 11706 reply_id: 42561[/import]

[lua]local function startDrag( event )

local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

ballx0 = ball.x
bally0 = ball.y

print(x0, y0)

– Stop current motion, if any
ball:setLinearVelocity( 0, 0 )
ball.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
ball.x = ballx0 + event.x - x0
ball.y = bally0 + event.y - y0

end

– Stop further propagation of touch event!
return true
end

ball:addEventListener( “touch”, startDrag ) – add listner to ball only.[/lua] [import]uid: 48521 topic_id: 11706 reply_id: 42562[/import]

Ok. I’ve changed the variable to “crate” just for fun. The code works great! Thanks guys.
Now, what about when I want there to be a function for spawning more crates?

This is the code that I have. The spawn function works fine as well, but the crates aren’t draggable. (HELP!)

Also, do you guys know any DECENT way to run a function with a method other than [lua]timer.performWithDelay[/lua]? I can’t find one. Anyways, thanks guys!

[lua]local function spawnCrate()
local crate = display.newImage( “cratesmall.png” )
crate.x = math.random(250)
crate.y = math.random(200)
end
timer.performWithDelay( 0, spawnCrate, 3)

local function startDrag( event )

local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

cratex0 = crate.x
cratey0 = crate.y

print(x0, y0)

– Stop current motion, if any
crate:setLinearVelocity( 0, 0 )
crate.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
crate.x = cratex0 + event.x - x0
crate.y = cratey0 + event.y - y0

end

return true
end

crate:addEventListener( “touch”, startDrag )[/lua]
[import]uid: 25216 topic_id: 11706 reply_id: 42565[/import]

try this

[lua]local cratex0, cratey0

local function startDrag( event )
local crate = event.target
local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

cratex0 = crate.x
cratey0 = crate.y

print(x0, y0)

– Stop current motion, if any
crate:setLinearVelocity( 0, 0 )
crate.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
crate.x = cratex0 + event.x - x0
crate.y = cratey0 + event.y - y0
end

return true
end

local function spawnCrate()
local crate = display.newImage( “cratesmall.png” )
crate.x = math.random(250)
crate.y = math.random(200)
crate:addEventListener( “touch”, startDrag )
end
timer.performWithDelay( 0, spawnCrate, 3)[/lua] [import]uid: 48521 topic_id: 11706 reply_id: 42568[/import]

Yay! It works! They’re just a little glitchy though. [import]uid: 25216 topic_id: 11706 reply_id: 42570[/import]

what’s the glitch? [import]uid: 48521 topic_id: 11706 reply_id: 42574[/import]

I have 9 objects on screen. When you drag an object over another, they change. Also, you can’t drag them fast without loosing them.

Also, is there a way to make them so that they’re effected by other physical objects? I have my mode set to “static” but “kenetic” adds gravity. What is “dynamic”? [import]uid: 25216 topic_id: 11706 reply_id: 42578[/import]

When you drag an object over another, they change.
What do you mean exactly by change?

Also, you can’t drag them fast without loosing them.
That happens on your method of implementation I suppose. You can also play around with position iterations setting of physics engine

Also, is there a way to make them so that they’re effected by other physical objects?
By default if there body type is set properly they will interact with other bodies.
I have my mode set to “static” but “kenetic” adds gravity. What is “dynamic”?
I strongly suggest you read documentation about physics bodies provided on this site to learn the difference within the three. [import]uid: 48521 topic_id: 11706 reply_id: 42609[/import]

Hi guys,

I have a question that is similar to this. And was not able to find anyone that has a similar problem. I am new to lua and corona, so apologize if this is such a newbie call.

I created an object that layers 2 images together. when i display these to images, they are each draggable using listen eventlistener
“touch” when dragging them is fine, but when i drag one over the other, that one seems to disappear.
so i have an object class and i call it multiple times. now then i drag buttonCall over buttonCall2, buttonCall2 disappears.

Any reason why that is, i dont really need physics, but i am wondering if i need it. Anyway thanks again in advance.

** updated never mind i found out i needed to focus in and return true. [import]uid: 9114 topic_id: 11706 reply_id: 43067[/import]

Can somebody help me with this also. This code was working great for me and now all I get is a black screen. I basically have a titescreen with some blocks falling, that I want to be able to drag. It was working on one of the blocks and now all I get is a blank screen and I don’t know what I did. This is the code I have.
module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local physics = require(“physics”)
physics.start()

local background = display.newImage (“title2.png”)
background.x = 240
–> This sets the background

local instructions = display.newImage (“instructions.png”)
instructions.x = 100
instructions.y = 240
instructions.xScale = .5
instructions.yScale = .5

local highscores = display.newImage (“highscores.png”)
highscores.x = 380
highscores.y = 240
highscores.xScale = .5
highscores.yScale = .5

local floor = display.newImage(“floor.png”)
floor.x = 230
floor.y = 360

physics.addBody( floor, “static”, { friction=0.5, bounce=0.3 } )

local redblock = display.newImage( “redblock.png” )
redblock.x = 350
redblock.y = 50
redblock.rotation = 5
redblock.xScale = .5
redblock.yScale = .5

physics.addBody( redblock, { density=2.0, friction=0.5, bounce= 0.5 } )

local function startDrag( event )

local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

redblockx0 = redblock.x
redblocky0 = redblock.y

print(x0, y0)

– Stop current motion, if any
redblock:setLinearVelocity( 0, 0 )
redblock.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
redblock.x = ballx0 + event.x - x0
redblock.y = bally0 + event.y - y0
end

– Stop further propagation of touch event!
return true
end

Runtime:addEventListener( “touch”, startDrag ) [import]uid: 72372 topic_id: 11706 reply_id: 44040[/import]