[SOLVED] - Moving object in touch event

Hi there!, I’m failing to see what’s going wrong with this code that I’m sketching for a game.

There are two display groups:

  • grupoObjs -> will contain 4 object groups in an horizontal row
  • object -> will contain 2 display objects, simple squares with different sizes

A touch handler for the object group that will move the group horizontally in the move event, and will go back to its origin x position in ended event.

The problem is that when the group moved it’s not moving following the cursor (I’m on simulator), but a bit far to the right, and when I release the button it does not go back to it’s origin.

This happens only if I move the entire object group, if I set the touch event to the first display object it works fine. It must be something that I don’t get about the groups so any help is appreciated.

[code]

_W = display.contentWidth
_H = display.contentHeight

display.setStatusBar(display.HiddenStatusBar)

local maxObjs = 4
local objects = {}

local function objectTouchHandler(event)
local target = event.target

if event.phase == “began” then

–target.xScale = 1.1
–target.yScale = 1.1
display.getCurrentStage():setFocus( target )
target.isFocus = true
target.alpha = 0.5
elseif event.phase == “moved” then
if hasCollided(target.boundsToCheck, objects[4]) then
print(“Colision”)
end
target.x = event.x
elseif event.phase == “ended” or event.phase == “cancelled” then
–target.xScale = 1
–target.yScale = 1
target.x = target.origen
print("Target x = "… target.x)
target.alpha = 1
display.getCurrentStage():setFocus( nil )
target.isFocus = false
end
return true
end
– Parent group
local grupoObjs = display.newGroup( )

for i = 1, maxObjs do
local xPos = ( ( _W - 65 ) / maxObjs ) * i
print(“xPos”… i … " = " … xPos )

grupoObjs.x = 0
grupoObjs.y = 100

– To see the reference from the uppermost goup
local xyRefParentGroup = display.newRect(0,0,15,15)
xyRefParentGroup:setReferencePoint(display.CenterReferencePoint)
centerxyRefParentGroupGroup.x = grupoObjs.x
xyRefParentGroup.y = grupoObjs.y

–Object group
local object = display.newGroup()

– 1st display object
local o = display.newRect(object, 0, 0, 50, 50 )
o:setReferencePoint( display.CenterReferencePoint)
o.x = xPos
o.y = 0

grupoObjs:insert(object)

– 2nd display object
local objectCenter = display.newRect(0, 0, 15, 15)
object:insert(objCenter)
objectCenter:setReferencePoint(display.CenterReferencePoint)
objectCenter:setFillColor( 0, 225, 0 )
objectCenter.x = o.x
objectCenter.y = o.y

if i == 4 then
objectCenter.isCollidable = true
else
objectCenter.isCollidable = false
end

object.boundsToCheck = center
object.origen = xPos – X coord to return when ended event is triggered
object:addEventListener( “touch”, objectTouchHandler )

table.insert(objects, o)

end

[/code] [import]uid: 8933 topic_id: 25643 reply_id: 325643[/import]

Hi, I found the answer in this wonderful tutorial and I thought it would be nice to share and close the thread and maybe someone finds this useful.

http://blog.anscamobile.com/2011/09/tutorial-how-to-drag-objects/

If you see the comments below you’ll see the explanation for this problem:

Jonathan BeebeSeptember 25th, 2011 at 6:10 pm

@Abstract Edge: The reason I stored the object’s x/y coordinates at the start of the touch, and then add them on every move phase, is because where the user touched the object may not be where the it’s reference point is, causing the image to jump into place as soon as you move it.

When you modify an object’s x/y coordinates, it positions the reference point of the object to those coordinates. By adding it’s stored location, you’re really just adding an offset so that the object will appear to move from where it was at, to where you dragged it.

I encourage you to try omitting the storing of the object’s coordinates (and adding during the move event) and see what happens. Sometimes that’s the best way to learn and understand new things.
[import]uid: 8933 topic_id: 25643 reply_id: 106834[/import]