Issues with setFocus

Hi,

I’m just starting with Corona so it probably is me not understanding something about touch handling.

What I want is to register touches and assign them to one of [possible] many objects which will handle the touch.
The objects are Rects decorated with :touch(event) functions.

So what I do is:
[lua]local grassBackground = display.newImage(“images/background_grass.png”, 0, 0)
obj:insert(grassBackground)
grassBackground:addEventListener(“touch”, obj)

function obj:touch(event)
print(event.phase)
for i = 1, self.objectsGroup.numChildren do
local object = self.objectsGroup[i]
if object.isReady then
display:getCurrentStage():setFocus(object)
break;
end
return true
end
end[/lua]

And for the object which should handle the touch:

[lua] function obj:touch(event)
print(event.phase)
if event.phase == “began” then
self.isReady = false
elseif event.phase == “moved” then
self.target = { x = event.x, y = event.y }
else
display.getCurrentStage():setFocus(nil)
self.isReady = true
end
return true;
end[/lua]

The only thing I get in terminal is:
began
which comes from the original touch handling function [first code snippet].
It doesn’t matter where I go with the touch the only thing that is registered is the touch began.

I’m not sure if setFocus will automatically call the :touch method on the object to be focused, so I’ve added
[lua]object:touch(event)[/lua]
after the setFocus. Function gets called fine, unfortunately that’s it. no other event phases are sent to the object.

Any tips about how to handle it?

Thanks [import]uid: 109453 topic_id: 19227 reply_id: 319227[/import]

I forgot to add “touch” event listener to my object to which I wanted to set focus to.
I’ve only added touch function.

Simple yet took a while to notice. [import]uid: 109453 topic_id: 19227 reply_id: 74285[/import]

Well done on figuring it out, sorry no one was able to provide assistance earlier.

Nicely done :slight_smile:

Peach [import]uid: 52491 topic_id: 19227 reply_id: 74352[/import]