That’s also pretty simple. All you’d do is, inside of the object’s touch listener, set a flag to check if the object’s at the center of your x and y axes, and then some sort of a flag to check if you moved the mouse up and down or left and right. Then, you’d just set the X of the object to event.x, and not the Y, if it was left-right, and just the Y and not the X if it was up-down. This isn’t a very good example, it’s just off the top of my head, and it’s not perfect, but here’s a prototype idea:
[lua]local rect=display.newRect(0, 0, 60, 60) – Create the moving object
rect.x, rect.y=display.contentCenterX, display.contentCenterY – In the center of the screen
rect:setFillColor(255, 255, 0)
local test=display.newRect(0, 0, 20, 20) – And a rectangle to check (coarsely) if the touch object is in the center
test.x, test.y=display.contentCenterX, display.contentCenterY – Also in the center
local prevX, prevY=0, 0
local snap=“NIL” – Make the snap axis
local function touchRect(e)
local inTestBounds=
(rect.x<=test.contentBounds.xMax) and (rect.x>=test.contentBounds.xMin) and (rect.y<=test.contentBounds.yMax) and (rect.y>=test.contentBounds.yMin) – See if the rectangle’s near the center
if “began”==e.phase then
prevX, prevY=e.x, e.y – Reset the prevX and prevY
display.getCurrentStage():setFocus(e.target) – Set focus
e.target.isFocus=true
elseif “moved”==e.phase then
if inTestBounds then – If the rectangle’s near the center…
local distX, distY=e.x-prevX, e.y-prevY
if distX>distY then – …Reset the snap
snap=“X”
elseif distX snap=“Y”
else
snap=“X”
end
e.target.x, e.target.y=e.x, e.y – Move it
elseif not inTestBounds then
if snap==“X” then
e.target.x=e.x – Only set the X if the snap is on the X axis
elseif snap==“Y” then
e.target.y=e.y – Same here, but Y
end
end
elseif “ended”==e.phase then
e.target.x, e.target.y=display.contentCenterX, display.contentCenterY – Back to the center
display.getCurrentStage():setFocus(nil) – Un-focus it
e.target.isFocus=false
end
end
rect:addEventListener(“touch”, touchRect) – Add the listener[/lua] [import]uid: 147322 topic_id: 34357 reply_id: 136770[/import]