event.phase never shows "ended" or "cancelled" when...

I’ve been using Solar2D since 2014. One thing that makes me crazy is that, although I can check event.phase = “began”, “moved”, “ended” or “cancelled”, why isn’t there an “enter” or “leave”?

I set my custom button color using “began” and I reset my custom button color using “ended”.

But what am I to do when someone touches with “began” and then drags their finger (or pointer) out of the button region and then let’s go? Right now, this results in leaving my custom hilighted button without resetting it - shouldn’t it at least send a “cancelled”?

Could it be there is already a solution for this that I don’t know?

If I really want to do this, I suppose I could create my own code to create an object the size of the entire screen and track every phase=“ended” and then compare the coordinates to the size and position of my custom buttons to do this. Isn’t there any easier solution?

I apply a slight scaling on “began” and then remove this on “ended”. This works great. Changing the colour of a button is so 90s and should be avoided.

@anon63346430 do you have a game or app I can see this in action? Also, if you scale at began, what do you do when the user exits while dragging so that ended never happens?

What I do is checking if the “finger” moved away from the starting point, on every “moved” phase.
I like to think that if it moved more than 5px then it’s not a “click” but a “swipe/cancel”.

1 Like

“cancelled” is a system event where the OS tells Solar2D that it cancelled the touch event.

I do have a custom library that tracks when the touch event leaves the target bounds, but I’m just toggling target.inBounds = true/false.

You don’t need to do anything as fancy as you’re recommending. All you need to do is 1) set focus on the event target, 2) track the event coordinates in “moved” phase and see if they are within the touch target’s contentBounds. In “began” phase, you set “inBounds” to true and then you just check if the event coordinates go out of bounds. Once out of bounds, then you set “inBounds” to false, etc.

This behaviour is programmed into widget.newButton, if I’m not mistaken. It probably has a similar state that its toggling true/false.

1 Like

@XeduR are you saying that inBounds is already a property of display objects? Or are we creating this property as a new one, which would beg the question, how is it then set to false? Am I to write any code to check if it’s inBounds or is that handled already by Soalr2D display objects using the inBounds property? Hopefully my questions were not confusing. Thanks for your help.

The specific object.inBounds property is just in my library.

I took a quick look at the widget source and it seems that they’re just checking the state every time the event is fired and they don’t store a similar property anywhere.

You could use the function from above as a starting point and create your own method for tracking when a touch event leaves object bounds and when it re-enters.

1 Like

@XeduR thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.