Dragging on game screen and releasing on a button

Hi guys,

I have a little glitch I don’t know how to get around. I have attached a touch event listener to my main game, and I also have a button there that opens up the menu. I need the “began”, “moved” and “ended” phase in my events in my game for my gameplay. However when I drag my finger over the button and release my finger, my game will never receive a “ended” event so my move only resolves when I touch the screen again. Is there a way that the button does not consume the touch events, so that my game can know that I have released my finger? Does my button have to manually inform my touch listener by creating my own event with phase = "ended"or is there a Corona way to do this?

Thanks in advance,

Alex

Is your button getting a began phase?  If not, you could set a flag and if you get an ended without a began, then simply return false and that event will fall thru to the next object that can handle touch events.

Of course! Return false will not consume the event and you’re right. I can avoid processing my button with the flag you suggest! Thanks!

You can also use set focus so the event that started on one object will trigger touch events only on that object.

https://docs.coronalabs.com/api/type/StageObject/setFocus.html

Ok, so the problem is still there, since there is no event on the button that gets generated whatsoever when moving off the game. I think I’ll have to use setFocus. Thanks, primoz for the suggestion.

Here’s the code sample to show my problem:

local Widget =  require(“widget”)

 function touchListener(event)

     print("game touch listener "…event.phase)

     return false

end

 Runtime:addEventListener(“touch”, touchListener)

    

    function onEventButtonFunction( event )

        print("Button pressed: ",event.phase)

        return false    

    end

    

   local button = Widget.newButton

{

    left = 100,

    top = 100,

    width = 100,

    height = 50,

    defaultFile = nil,

    overFile = nil,

    onPress =  onEventButtonFunction,

    onRelease = onEventButtonFunction,

    id = “idButton”,

    label = “testButton”,

    font = native.systemFont,

    fontSize = 12

}

The button widget always returns true internally on any touch event so you won’t be able to get it to pass through. You will have to use setFocus.

So this is the solution. Thanks primoz.cerar

local Widget =  require(“widget”)

local displayGroup = display.newGroup()

local rectangle = display.newRect(displayGroup, display.contentWidth / 2, display.contentHeight / 2, display.contentWidth,  display.contentHeight)

        rectangle:setFillColor(1,1,1,1)

  displayGroup:insert(rectangle)

  

  

 function rectangle:touch(event)

     if event.phase == “began” then

     display.getCurrentStage():setFocus(self)

     elseif event.phase == “ended” then

      display.getCurrentStage():setFocus(nil)

    end

    

     print("game touch listener "…event.phase)

end

 Runtime:addEventListener(“touch”, rectangle)

    

    function onEventButtonFunction( event )

        print("Button pressed: ",event.phase)

        return false    

    end

    

   local button = Widget.newButton

{

    left = 100,

    top = 100,

    width = 100,

    height = 50,

    defaultFile = nil,

    overFile = nil,

    onPress =  onEventButtonFunction,

    onRelease = onEventButtonFunction,

    id = “idButton”,

    label = “testButton”,

    font = native.systemFont,

    fontSize = 12

}

Welcome.

Just a note: You don’t have to insert the rectangle in to displayGroup as you already specified the parent in display.newRect

Ok, gotcha! Thanks

Is your button getting a began phase?  If not, you could set a flag and if you get an ended without a began, then simply return false and that event will fall thru to the next object that can handle touch events.

Of course! Return false will not consume the event and you’re right. I can avoid processing my button with the flag you suggest! Thanks!

You can also use set focus so the event that started on one object will trigger touch events only on that object.

https://docs.coronalabs.com/api/type/StageObject/setFocus.html

Ok, so the problem is still there, since there is no event on the button that gets generated whatsoever when moving off the game. I think I’ll have to use setFocus. Thanks, primoz for the suggestion.

Here’s the code sample to show my problem:

local Widget =  require(“widget”)

 function touchListener(event)

     print("game touch listener "…event.phase)

     return false

end

 Runtime:addEventListener(“touch”, touchListener)

    

    function onEventButtonFunction( event )

        print("Button pressed: ",event.phase)

        return false    

    end

    

   local button = Widget.newButton

{

    left = 100,

    top = 100,

    width = 100,

    height = 50,

    defaultFile = nil,

    overFile = nil,

    onPress =  onEventButtonFunction,

    onRelease = onEventButtonFunction,

    id = “idButton”,

    label = “testButton”,

    font = native.systemFont,

    fontSize = 12

}

The button widget always returns true internally on any touch event so you won’t be able to get it to pass through. You will have to use setFocus.

So this is the solution. Thanks primoz.cerar

local Widget =  require(“widget”)

local displayGroup = display.newGroup()

local rectangle = display.newRect(displayGroup, display.contentWidth / 2, display.contentHeight / 2, display.contentWidth,  display.contentHeight)

        rectangle:setFillColor(1,1,1,1)

  displayGroup:insert(rectangle)

  

  

 function rectangle:touch(event)

     if event.phase == “began” then

     display.getCurrentStage():setFocus(self)

     elseif event.phase == “ended” then

      display.getCurrentStage():setFocus(nil)

    end

    

     print("game touch listener "…event.phase)

end

 Runtime:addEventListener(“touch”, rectangle)

    

    function onEventButtonFunction( event )

        print("Button pressed: ",event.phase)

        return false    

    end

    

   local button = Widget.newButton

{

    left = 100,

    top = 100,

    width = 100,

    height = 50,

    defaultFile = nil,

    overFile = nil,

    onPress =  onEventButtonFunction,

    onRelease = onEventButtonFunction,

    id = “idButton”,

    label = “testButton”,

    font = native.systemFont,

    fontSize = 12

}

Welcome.

Just a note: You don’t have to insert the rectangle in to displayGroup as you already specified the parent in display.newRect

Ok, gotcha! Thanks