Dispatch event touch

Hello all,

it’s possible when i create a Image and add event listener touch. dispatch it?

this, don’t work:

obj= display.newImage(“levels/1.png”)

obj:addEventListener(“touch”,onTouch)
obj:dispatchEvent({name=touch,phase=‘began’, target=obj })

the touch event drag my object to touch position

this is the touch function

local function onTouch(event)
        local t=event.target
        local phase =event.phase
       if “began” == phase then
            local parent =t.parent
            parent:insert(t)
            display.getCurrentStage():setFocus(t)
            t.x0=event.x-t.x
            t.y0=event.y-t.y
        elseif “moved” == phase then
            t.x = event.x-t.x0
            t.y = event.y-t.y0
        elseif “ended” == phase or “cancelled” == phase then
            display.getCurrentStage():setFocus (ni)
            t.isFocus =false
       end

    return true
end

i will start drag the object when i created.

Thanks all!

Hi,

You can dispatch the event. but make sure you do {name=“touch”} not {name=touch}

But you will still have issues setting the focus.

If I understood right what you are trying to achieve, you should add the listener to the stage not the object.

[lua]

local stage=display.getCurrentStage()

local function onTouch(event)

        local t=obj

        local phase =event.phase

       if “began” == phase then

            local parent =t.parent

            parent:insert(t)

            --display.getCurrentStage():setFocus(t)

            t.x0=event.x-t.x

            t.y0=event.y-t.y

        elseif “moved” == phase then

            t.x = event.x-t.x0

            t.y = event.y-t.y0

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

            --display.getCurrentStage():setFocus (ni)

            --t.isFocus =false

       end

    return true

end

stage:addEventListener(“touch”, onTouch)

[/lua]

Let me know if this is what you wanted

Hi @igenapps

your code inspired me and i found a solution.

I made:

[lua]

obj= display.newImage(“levels/”…name…".png")
obj:setReferencePoint( display.CenterReferencePoint )
obj.x = positionX
obj.y = positionY
menuGroup:insert( obj )

f(x) touch to drag obj{}

obj:addEventListener(“touch”,onTouch)

objetoCreadoActual=obj --global variable

local stage=display.getCurrentStage()
local function onTouch(event)
local t=obj
local phase =event.phase
if(objetoCreadoActual~=nil)then
if “began” == phase then
objetoCreadoActual:dispatchEvent({name=“touch”,phase=‘began’, target=objetoCreadoActual ,x=objetoCreadoActual.x , y=objetoCreadoActual.y })
elseif “moved” == phase then
objetoCreadoActual:dispatchEvent({name=“touch”,phase=‘moved’, target=objetoCreadoActual ,x=objetoCreadoActual.x , y=objetoCreadoActual.y })
elseif “ended” == phase or “cancelled” == phase then
objetoCreadoActual:dispatchEvent({name=“touch”,phase=‘ended’, target=objetoCreadoActual ,x=objetoCreadoActual.x , y=objetoCreadoActual.y })
objetoCreadoActual=nil
stage:removeEventListener(“touch”, onTouch)
end
end

return true
end
stage:addEventListener(“touch”, onTouch)[/lua]

Basically,

when i created a object i add an EventListener to the stage to “dispatch” the object touch event, when i remove the finger this even dissapear and i can interact with the touch object event.

Thanks all!!!

Hi,

You can dispatch the event. but make sure you do {name=“touch”} not {name=touch}

But you will still have issues setting the focus.

If I understood right what you are trying to achieve, you should add the listener to the stage not the object.

[lua]

local stage=display.getCurrentStage()

local function onTouch(event)

        local t=obj

        local phase =event.phase

       if “began” == phase then

            local parent =t.parent

            parent:insert(t)

            --display.getCurrentStage():setFocus(t)

            t.x0=event.x-t.x

            t.y0=event.y-t.y

        elseif “moved” == phase then

            t.x = event.x-t.x0

            t.y = event.y-t.y0

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

            --display.getCurrentStage():setFocus (ni)

            --t.isFocus =false

       end

    return true

end

stage:addEventListener(“touch”, onTouch)

[/lua]

Let me know if this is what you wanted

Hi @igenapps

your code inspired me and i found a solution.

I made:

[lua]

obj= display.newImage(“levels/”…name…".png")
obj:setReferencePoint( display.CenterReferencePoint )
obj.x = positionX
obj.y = positionY
menuGroup:insert( obj )

f(x) touch to drag obj{}

obj:addEventListener(“touch”,onTouch)

objetoCreadoActual=obj --global variable

local stage=display.getCurrentStage()
local function onTouch(event)
local t=obj
local phase =event.phase
if(objetoCreadoActual~=nil)then
if “began” == phase then
objetoCreadoActual:dispatchEvent({name=“touch”,phase=‘began’, target=objetoCreadoActual ,x=objetoCreadoActual.x , y=objetoCreadoActual.y })
elseif “moved” == phase then
objetoCreadoActual:dispatchEvent({name=“touch”,phase=‘moved’, target=objetoCreadoActual ,x=objetoCreadoActual.x , y=objetoCreadoActual.y })
elseif “ended” == phase or “cancelled” == phase then
objetoCreadoActual:dispatchEvent({name=“touch”,phase=‘ended’, target=objetoCreadoActual ,x=objetoCreadoActual.x , y=objetoCreadoActual.y })
objetoCreadoActual=nil
stage:removeEventListener(“touch”, onTouch)
end
end

return true
end
stage:addEventListener(“touch”, onTouch)[/lua]

Basically,

when i created a object i add an EventListener to the stage to “dispatch” the object touch event, when i remove the finger this even dissapear and i can interact with the touch object event.

Thanks all!!!