How to move only one object?

Hello everyone.

I want to move object A and only object A, but when center of this object coincide with center of object B then I can move two obj. and I can’t separate objects. All code that I use:

CenterY=display.contentCenterY CenterX=display.contentCenterX W=display.contentWidth H=display.contentHeight local background=display.newImageRect("Images/background.png",720,1280) background.x=CenterX background.y=CenterY local base=display.newImageRect("Images/2.png",50,50) base.x=CenterX+100 base.y=CenterY+250 local roof=display.newImageRect("Images/1.png",50,50) roof.x=CenterX-50 roof.y=CenterY-125 function base:touch(b) if b.phase=="began" then print("Touch base status: true.") elseif b.phase=="moved" then base.x=b.x;base.y=b.y; elseif b.phase=="ended" then print("Touch base status: false") end end function roof:touch(r) if r.phase=="began" then print("Touch roof status: true.") elseif r.phase=="moved" then roof.x=r.x;roof.y=r.y; elseif r.phase=="ended" then print("Touch roof status: false.") end end base:addEventListener("touch",move) roof:addEventListener("touch",move)

You need to put “return true” at the end of your touch functions, i.e.

 

function base:touch(b) if b.phase=="began" then print("Touch base status: true.") elseif b.phase=="moved" then base.x=b.x;base.y=b.y; elseif b.phase=="ended" then print("Touch base status: false") end return true end

This way the touch won’t go through the object and only affects the top most display object.

You probably also want to set focus on the object that you are currently touching. If you don’t set focus, then the touch event that is on top will always take priority. You can read about the touch event and how to set focus in the documentation: https://docs.coronalabs.com/api/event/touch/index.html.

Thanks XeduR @Spyric

You need to put “return true” at the end of your touch functions, i.e.

 

function base:touch(b) if b.phase=="began" then print("Touch base status: true.") elseif b.phase=="moved" then base.x=b.x;base.y=b.y; elseif b.phase=="ended" then print("Touch base status: false") end return true end

This way the touch won’t go through the object and only affects the top most display object.

You probably also want to set focus on the object that you are currently touching. If you don’t set focus, then the touch event that is on top will always take priority. You can read about the touch event and how to set focus in the documentation: https://docs.coronalabs.com/api/event/touch/index.html.

Thanks XeduR @Spyric