[Resolved] Touch event problem

Hello, I have scene with background rect and one phys object
[lua]function scene:createScene( event )
local group = self.view

Rect = display.newRect(W,H,-W,-H);
Rect.id = “rect”;
group:insert(Rect)

end

function scene:enterScene( event )
local group = self.view

ball = Ball.createBall( some params); --my phys object
function OnTouch(e)
do something
end

Rect:addEventListener(“touch”, OnTouch);

end[/lua]
why function OnTouch(e) triggered when I touch ball?

How to make that “function OnTouch(e)” triggered only if I touch Rect background, excluding all objects on it?

Sorry for my bad English, best regards, Vladimir [import]uid: 126133 topic_id: 31774 reply_id: 331774[/import]

have you ensured that the other objects are in front use ball:toFront() to force them to be on top and see if the touch event is still being triggered on the background.

Larry Meadows
[import]uid: 11860 topic_id: 31774 reply_id: 126855[/import]

Hi

give this a try in your function?

function OnTouch(e)
if e.target == rect then
do something
end
return true
end

if you return the touch at each object it should stop the touch event leaking through… off top of my head.

good luck [import]uid: 131622 topic_id: 31774 reply_id: 126876[/import]

[code]
local group = display.newGroup();

function createScene()
local Rect = display.newRect(0,0,display.contentWidth,display.contentHeight);
Rect.id = “rect”;
Rect:setFillColor(255,255,255)
Rect:addEventListener(“tap”, OnTouch);
group:insert(Rect)
end

function enterScene()
local ball = display.newImage(“ball.png”)
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2
ball:addEventListener(“tap”, function(e) return true end)
group:insert(ball)
end

function OnTouch(e)
print(“touch is calling…”)
end

createScene()
enterScene()
[/code] [import]uid: 163563 topic_id: 31774 reply_id: 126878[/import]

thanks to all) all working) [import]uid: 126133 topic_id: 31774 reply_id: 127004[/import]

thanks to all) all working) [import]uid: 126133 topic_id: 31774 reply_id: 127005[/import]

have you ensured that the other objects are in front use ball:toFront() to force them to be on top and see if the touch event is still being triggered on the background.

Larry Meadows
[import]uid: 11860 topic_id: 31774 reply_id: 126855[/import]

Hi

give this a try in your function?

function OnTouch(e)
if e.target == rect then
do something
end
return true
end

if you return the touch at each object it should stop the touch event leaking through… off top of my head.

good luck [import]uid: 131622 topic_id: 31774 reply_id: 126876[/import]

[code]
local group = display.newGroup();

function createScene()
local Rect = display.newRect(0,0,display.contentWidth,display.contentHeight);
Rect.id = “rect”;
Rect:setFillColor(255,255,255)
Rect:addEventListener(“tap”, OnTouch);
group:insert(Rect)
end

function enterScene()
local ball = display.newImage(“ball.png”)
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2
ball:addEventListener(“tap”, function(e) return true end)
group:insert(ball)
end

function OnTouch(e)
print(“touch is calling…”)
end

createScene()
enterScene()
[/code] [import]uid: 163563 topic_id: 31774 reply_id: 126878[/import]

thanks to all) all working) [import]uid: 126133 topic_id: 31774 reply_id: 127004[/import]

thanks to all) all working) [import]uid: 126133 topic_id: 31774 reply_id: 127005[/import]