How to put objects in a group and make them draggable?

I currently am a Newbie and started a week ago. I’m just messing around and trying to learn new stuff and what I’m currently learning is dragging objects. here’s my code:

display.setStatusBar(display.HiddenStatusBar)

local physics = require “physics”

physics.start()

physics.setGravity(0, 0)

local bg = display.newRect(display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight)

bg:setFillColor(0, 450, 220)

local big = display.newImage(“big.png”)

big.x = 100

big.y = 0

physics.addBody(big, “dynamic”, {density = 0.4, friction = 0.2, bounce = 0.3})

local small = display.newImage(“Icon.png”)

small.x = 280

small.y = 0

physics.addBody(small, “dynamic”, {density = 0.4, friction = 0.2, bounce = 0.3})

local floor = display.newRect(160, 700, display.contentWidth, display.contentHeight)

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

function small:touch(event)

if event.phase == “began” then

self.markX = self.x

self.markY = self.y

elseif event.phase == “moved” then

local x = (event.x - event.xStart) + self.markX

local y = (event.y - event.yStart) + self.markY

self.x, self.y = x, y

end

return true

end

small:addEventListener(“touch”, small)

My question is: am I able to create a group where I can place all the objects into and then create a function with the name of the group to make ALL of the objects draggable or do I have to create a function for each object? Hope this isn’t confusing to understand…?

It’s basically determined by what you add the touch event listener to. Right now you’re adding the touch listener to an image. But you can add it to any display object, including a display group. So if you created a display group, added a bunch of images to it, and added the event listener to the display group, the whole group would be dragged when you drag one of the images. Note that this approach would drag all the images together, so if you want to drag them individually, you need to call addEventListener(“touch”) on each of them.

That’s what I figured what I had to do. I just wanted to make sure. Thanks!

Hi @creationliberty,

Just a note, in addition to what @_memo says: you’re going to encounter problems if you’re using physics, collision detection, and you’re moving physical objects around in their own independent group (separate from other groups which contain physics objects that need to interact with the group being moved). This will cause physics collisions to misbehave. Please see the following page for details on this:

http://docs.coronalabs.com/guide/physics/limitations/index.html

Take care,

Brent

It’s basically determined by what you add the touch event listener to. Right now you’re adding the touch listener to an image. But you can add it to any display object, including a display group. So if you created a display group, added a bunch of images to it, and added the event listener to the display group, the whole group would be dragged when you drag one of the images. Note that this approach would drag all the images together, so if you want to drag them individually, you need to call addEventListener(“touch”) on each of them.

That’s what I figured what I had to do. I just wanted to make sure. Thanks!

Hi @creationliberty,

Just a note, in addition to what @_memo says: you’re going to encounter problems if you’re using physics, collision detection, and you’re moving physical objects around in their own independent group (separate from other groups which contain physics objects that need to interact with the group being moved). This will cause physics collisions to misbehave. Please see the following page for details on this:

http://docs.coronalabs.com/guide/physics/limitations/index.html

Take care,

Brent