1

I have a widget.newScrollView component and a widget.newButton in front of it. Unfortunately when i click my button it also calls my ScrollView "tap" handler. How do i stop my ScrollView from getting this event? Here is some of the code I'm using:

local function handleButtonEvent( event )
    if ( "ended" == event.phase ) then
        print( "Button was pressed and released" )
    end
    return true; **I tried this - but it had no effect**
end

added

local button1 = widget.newButton(
{
    label = "button",
    onEvent = handleButtonEvent,
    emboss = false,
    shape = "roundedRect",
    width = 400,
    height = 100,
    cornerRadius = 32,
    fillColor = { default={1,0,0,1}, over={1,0.1,0.7,1} },
    strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} },
    strokeWidth = 4,
    fontSize=100;
}

I've got an array (planets) of display.NewImages and my handler - like this:

local planets = {};
planets[1] = display.newImage( "planetHexs/001.png", _topLeft_x, _topLeft_y);
planets[2] = display.newImage( "planetHexs/002.png", _topLeft_x, _topLeft_y + _planet_height2 );
....

local scrollView = widget.newScrollView(
{
    top = 0,
    left = 0,
    width = display.actualContentWidth,
    height = display.actualContentHeight,
    scrollWidth = 0,
    scrollHeight = 0,
    backgroundColor = { 0, 0, 0, 0.5},
    verticalScrollDisabled=true;
}

for i = 1, #planets do
    local k = planets[i];
    scrollView:insert( k )
end

function PlanetTapped( num )
    print( "You touched the object!"..num );
end

for i = 1, #planets do
    local k = planets[i];
    k:addEventListener( "tap", function() PlanetTapped(i) end )
end

I get this print log:

Button was pressed and released

You touched the object2
Jason Bullen
  • 155
  • 1
  • 2
  • 10
  • Possible duplicate of [Stop event propagation in Corona SDK](http://stackoverflow.com/questions/20140217/stop-event-propagation-in-corona-sdk) – Piglet Oct 01 '16 at 18:13
  • please use google befor putting up a new question. if you want to stop event propagation the last event handler that shall handle the event has to return true. just put return true as last statement into your handleButtonEvent function... – Piglet Oct 01 '16 at 18:14
  • many thanks Piglet :) I tried that but it didn't work :( I've added some more of my code which might help :) Please help a little more :) – Jason Bullen Oct 02 '16 at 10:20
  • have you tried using onPress or onRelease instead of onEvent for the button event handler? https://docs.coronalabs.com/api/library/widget/newButton.html tap seems to be a different event so return true in the button's event handler won't have any effect. – Piglet Oct 02 '16 at 14:58
  • I have updated the answer. You must add a tap listener to the button which only returns true, essentially only to block tap events not to propagate to objects behind the button. – Basilio German Oct 02 '16 at 21:06

1 Answers1

0

You must return true on your event functions to prevent propagation. This essentially tells Corona that the event was handled correctly and that no more event listeners for that event should be fired. You can read more about event propagation here in the docs.

"tap" and "touch" events are handled with different listeners, so if you wish to stop taps when you touch the button, you will have add a "tap" listener to your button as well, that essentially just returns true to prevent, or block tap events to anything behind it.

button1:addEventListener("tap", function() return true end)

Since the button does not have a tap event, tap events simply go through the button, to any objects behind it which do have a "tap" event.

Basilio German
  • 1,801
  • 1
  • 13
  • 22