Detecting finger dragged off of moving object

thats the way i started learning years ago. maybe if you show me some code i can help. the code i posted above works as is for me. dispatchEvent is a way of creating your own event. like enterFrame, userInput, touch, … in the code above theres an eventListener for a touch event on the circle. when i dispatch my event what im doing is changing the event.phase and im resending it back to the circle as a touch event. You can completely make your own event and dispatch it to an object or to the Runtime. all you need to do is create a variable for a table and inside the table you have to at lease have a name variable.

function myFunct(event)   --this does something   print(event.name,event.phase,.....) end   local myEvent = { name = "doSomething", phase = "start", .... } myObject:addEventListener( "doSomething", myFunct )   function iDidSomethingThatSentMeHere()   myObject:dispatchEvent(myEvent) end

whats happening is i hane an object that was created called myObject. when the user does whatever that causes the functin iDidSome… to run it dispatches the event i created called myEvent and myObject has an eventListener listening for an event called doSomething. so it send you to the function called myFunct and myFunct can read all the variables setup inside of myEvent. hope that helps it took me a few times playing around with it to completely grasp it

So…the way I understand it- it runs an event from within a function?

Anyways I’ll try a few things out, write some simple function and use dispatchEvent.

I just don’t understand what’s the point of dispatchEvent…what exactly does it do in this function:

local function myTouchFunction(event)
  if event.phase == “began” then
 
  elseif event.phase == “moved” then
    if e.x < e.target.contentBounds.xMin or
       e.x > e.target.contentBounds.xMax or
       e.y < e.target.contentBounds.yMin or
       e.y > e.target.contentBounds.yMax then
          e.phase = “offTarget”
          e.target:dispatchEvent(e)
    end
    – add your moved code here
 
  elseif event.phase == “offTarget” then
 
  elseif event.phase == “ended” then
 
  elseif event.phase == “cancelled” then
 
  end
end

I don’t get it…what does it do? What role does dispatchEvent have in this function? :confused:

Which part of this code tracks the touch when it moves over the content bounds?

I’m just passing by and haven’t got in details reading each post, so forgive me if this was gone over already, but if the problem is detecting when the touch is off an object, can’t something like this be done?

 

function obj:touch(event) if event.phase == "began" then display.getCurrentStage():setFocus(self); self.isFocus = true; elseif event.phase == "moved" and self.isFocus then if event.x \> self.contentBounds.xMax or event.x \< self.contentBounds.xMin or event.y \> self.contentBounds.yMax or event.y \< self.contentBounds.yMin then print("The touch is outside the object"); else print("Touch is moving"); end elseif self.isFocus then -- only ended and cancel should have remained as phases display.getCurrentStage():setFocus(nil); self.isFocus = nil; end end obj:addEventListener("touch", obj);

The problem was that if the touch has moved over the object’s content bounds, you can’t track it anymore, because it’s not on the object.

As soon as event.x is more than object’s content bounds, it’s not on the object, and therefore won’t be detected.

I’ve solved the problem in my case, by adding a listener to Runtime as well, so that I can still detect touch when it moves over the object’s content bounds.

I have 2 listeners now, one added to the object and the other one added to runtime.

I’m sorry but that’s what setFocus is for (:
Try out my code, you’ll see what I mean (;
When setFocus is set on an object, it won’t let go that object even if the touch is outside of it.

There’s nothing to be sorry about :slight_smile:

I’ve just tried it out, and yeah. Results are the same as my code, only the way you did it is more simple.

I wasn’t familiar with setFocus.

I’ll see which one I’ll use, right now I’m having no problems with either. Maybe I’ll stick with my current code, or maybe I’ll take a step forward and start using setFocus.

So yeah, thanks for this.

I’m glad it helped (; 
Regarding the initial issue of the thread (the ball moving) take into account that if the object moves by itself, the touch won’t be detected anymore if the user also doesn’t move the finger (like if the finger is still, and the object moves away from it).
In that case, you’ll need to do the check inside of an enterFrame event. Store the event.x and y from the touch, and do the contentBounds comparison inside the enterFrame event (even better if those x and y are taken from an object as big as the area the ball will bounce in).

May I just ask something else about the above function that you’ve written?

If display.getCurrentStage():setFocus(self) sets the focus on the current touch object, then what is  self.isFocus = true used for?

self.isFocus is used to avoid going into the “moved” or “ended” phase without having first done a “began” phase (:
Say that you first tap outside of the object, then drag your finger on it. Without it, the moved phase would start (but not a began), and we want to make sure that the object is on :setFocus first (which we do only in the began phase).

As for the “ended”, say your finger is on the screen, the object moves beneath your finger, and you lift your finger. “Ended” would trigger, since the touch has actually ended while on the object, but “moved” and “began” would have never happened (:

So it’s something that you can use or not depending on your necessity. For example you could put the setFocus on began AND on moved, so that no matter if it has just began or if it’s moving on top of the object, it will gain the focus. For example I would do that in the case I have some buttons, and I want them to become inactive if the user drags the finger outside of them, but active if the user drags it inside of them (even from button to button, and not just always with the same button).

Ok, thanks for explaining that. I’ve been using some made up variables for that, and making them true on began phase, to detect if the touch didn’t start on the button.

self.isFocus seems better tho’ so I’ll try to use that in the future.

I still have to do some changes to these functions. I have a little multitouch problem atm. If one finger is touching the jump button, and another finger moves on the D-pad or anywhere else on the screen, the jump button becomes inactive.

I’ve been reading Corona docs…something about “focus on per-object-basis” (setFocus).

after activating multitouch, you need to keep the various touch ids and use them with setFocus.
This will make sure that each object will retain only one touch focus.

 

function obj:touch(event) if event.phase == "began" then display.getCurrentStage():setFocus(self, event.id); self.isFocus = true; elseif event.phase == "moved" and self.isFocus then if event.x \> self.contentBounds.xMax or event.x \< self.contentBounds.xMin or event.y \> self.contentBounds.yMax or event.y \< self.contentBounds.yMin then print("The touch is outside the object"); else print("Touch is moving"); end elseif self.isFocus then -- only ended and cancel should have remained as phases display.getCurrentStage():setFocus(self, nil); self.isFocus = nil; end end obj:addEventListener("touch", obj);

This is the updated code to work with multitouch (:

Lemme try that, to see how it works. Then I gotta dive into it, understand and learn to write such functions myself.

Ha! Works perfectly.

So if I understand this correctly…by adding event.id you can specify which object the touch is happening on. Otherwise it could be any object in the current stage that the touch is currently happening on- not sure if I’m right about this…

“To turn off focus you must specify the object and pass nil for touchID.” ~Corona docs

That’s what you did here, right?:

display.getCurrentStage():setFocus(self, nil);

yep, basically by passing the event.id you tell setFocus that you want focus of that specific touch (referenced by the id), to that specific objects. Afterwards, you tell setFocus that any previous focus of any previous touch specified for the object, must be cancelled.

Ok, thanks!

You’ve helped me speed up the development of my game and helped me learn something that’s gonna be useful when I’ll be programming the rest of the controls :slight_smile:

I’m glad (; 
Good luck with your game!

I had a new issue trying to get this working with Multi-touch within the same display object - I posted it here…

http://forums.coronalabs.com/topic/49421-setfocus-preventing-multi-touch-events-bug-sample-code-attached/?p=255678

I had a new issue trying to get this working with Multi-touch within the same display object - I posted it here…

http://forums.coronalabs.com/topic/49421-setfocus-preventing-multi-touch-events-bug-sample-code-attached/?p=255678