how to detect on which object the touch ended phase happend

i  have 2 circles on screen and i want to draw a line between them (just between these circles)

how to detect that where the ended phase happend??

local function touched(event)

           if event.phase == “began” then

                   print(event.target.id)

                   display.getCurrentStage():setFocus( event.target )

                   event.target.isFocus = true

 

        elseif event.phase == “ended” or event.phase == “cancelled” then

                   display.getCurrentStage():setFocus( nil)

                   event.target.isFocus = false

                     – ====================

                     – where is it happenning?? is it happening on a circle?? or its “ended” on a bg object?

                     --===========

                    local L = display.newLine(  event.xStart, event.yStart, event.x, event.y )

                    L.strokeWidth = 2

                    L:setStrokeColor( 0, 0 ,256 )

                    print(event.target.id)

         end

    return true

end

local bg = display.newRect(centerX,centerY,screenWidth,screenHeight)

local a = display.newRect(  100, 100, 50, 50 )

a:setFillColor ( 255, 0, 0)

a.id=1

a:addEventListener ( “touch”, touched )

b= display.newRect( 300, 300, 50, 50 )

b:setFillColor ( 0, 255, 0)

b.id=2

b:addEventListener ( “touch”, touched )

Hi @devil_vorojack_666,

Basically, since you added a touch listener to both objects, they’ll both receive all of the phases of touch: “began”, “moved”, “ended”, “cancelled” (although the last one only occurs if the system cancels the touch, so it’s rare). Then, in your touch listener function (“touched” as you named it), you can detect which item received the event by checking “event.target”… this will be the internal Lua reference of the object, so with that, you can do whatever action upon it (change its color, remove it, etc. etc.).

You won’t get any touch sensing on any background (“bg”) objects, unless you add touch listeners to them. So, no need to worry that those are picking up touch responses.

Hope this helps,

Brent

hi
thanks for answering
sorry but i dont get it,what should i do then??!!
where(and which object) should i add a touch eventListener to accsess objects that the phases happening to them??

is it possible to write a sample code???
sorry for my bad english…im not native english… :slight_smile:
thanks a lot

========== Solved ==========

br removing these tow lines my problem solved
 

                   display.getCurrentStage():setFocus( nil)

                   event.target.isFocus = false

when there is a focus on an object the event phases never happen on other objects…

Hi @devil_vorojack_666,

Basically, since you added a touch listener to both objects, they’ll both receive all of the phases of touch: “began”, “moved”, “ended”, “cancelled” (although the last one only occurs if the system cancels the touch, so it’s rare). Then, in your touch listener function (“touched” as you named it), you can detect which item received the event by checking “event.target”… this will be the internal Lua reference of the object, so with that, you can do whatever action upon it (change its color, remove it, etc. etc.).

You won’t get any touch sensing on any background (“bg”) objects, unless you add touch listeners to them. So, no need to worry that those are picking up touch responses.

Hope this helps,

Brent

hi
thanks for answering
sorry but i dont get it,what should i do then??!!
where(and which object) should i add a touch eventListener to accsess objects that the phases happening to them??

is it possible to write a sample code???
sorry for my bad english…im not native english… :slight_smile:
thanks a lot

========== Solved ==========

br removing these tow lines my problem solved
 

                   display.getCurrentStage():setFocus( nil)

                   event.target.isFocus = false

when there is a focus on an object the event phases never happen on other objects…