Detecting finger dragged off of moving object

Hello everyone, I am making a game where when your finger moves off the object (in my case a circle) even if it is still touching the screen something happens (background image turns red).
 
Note that the circle is randomly moving
 
Here is my code

math.randomseed( os.time() ) bg = display.newRect(-100,0,840,1640) bubble = display.newCircle(330,500, 200) bubble:setFillColor(230,230,230) local playing = false local function gameStart (event) if event.phase == "began" then help:removeSelf() help2:removeSelf() playing = true --in the future add a count down timer local function move (evnet) local xV = math.random( 0, 500 ) local yV = math.random( 0, 1136 ) transition.to( bubble, { time=1500, x=xV, y=yV, onComplete=move} ) end move() end if event.phase == "ended" then playing = false bg:setFillColor(255,0,0) end end bubble:addEventListener("touch",gameStart)

 
As always, thanks for the high quality support!

So did you have a question or were you just showing us your code

Hahaha sorry

Yes, IS there a way to detect if a finger has moved off of a moving object, even if the finger is still down?

There is a canceled phase

Or you can compare touch location to object location in the moved phase

So did you have a question or were you just showing us your code

Hahaha sorry

Yes, IS there a way to detect if a finger has moved off of a moving object, even if the finger is still down?

There is a canceled phase

Or you can compare touch location to object location in the moved phase

I have had no luck so far. The “canceled” phase is not responding. When event.phase == “canceled” the bubble should disappear but it doesn’t. However with event.phase == “ended” it works, but only if the finger is lifted from the screen. And I do not think I can compare locations because the bubble will keep moving.

Any help is greatly appreciated. 

Try setting stage focus on object during touch event

Ok so I tried doing that but if I dragged my finger off of the bubble nothing would happen (canceled), but if I removed my finger, it would disappear (ended). 

Here is the code.

function gameStart(event) if event.phase == "began" then if start == false then -- get stage and call 'setFocus()' method local stage = display.getCurrentStage() stage:setFocus( bubble ) bubble.isFocus = true bubble:applyLinearImpulse( -200, -500 , bubble.x+150, bubble.y+150 ) title:removeSelf() instructions:removeSelf() start = true local stage = display.getCurrentStage() stage:setFocus( bubble ) bubble.isFocus = true end elseif bubble.isFocus then if event.phase == "ended" or event.phase == "cancelled" then local stage = display.getCurrentStage() stage:setFocus( nil ) bubble:removeSelf() end end end

Hi @SummitTech,

Just a few quick comments on phases:

  1. “cancelled” is not really an event that gets triggered by the user. It generally happens only if the OS itself cancels the process, i.e. something interrupts the process.

  2. you won’t get an “ended” phase by moving a touch off the object (as in, sliding your touch outside the object’s bounds). “ended” is triggered when you lift your touch off an object, but that touch point is still within the object bounds.

  3. to sense a touch sliding off an object, you might try setting up some kind of background object (behind the other object) or stage listener, and then set up a flag like “onObject” as true when the touch is on the object. Then, if the background element receives a “moved” phase while that flag is true, you can assume the touch came from the object and then moved onto the background (thus, what you need).

Hope this helps,

Brent

Thanks for the help Brent! I’ll let you know if it works!

Ok I don’t know if I am doing something wrong (or forgetting to do something), or if the program just doesn’t work.

Here is a basic overview of what I am trying to accomplish.

User touches object and it starts to move around the screen.

The player must keep their finger on the object at all times or they lose.

Here is part of my code

function loseGame(event) bubble:removeSelf() start = false end function bgF(event) if start == true then if event.phase == "moved" and event.phase == "began" then loseGame() end end end function gameStart(event) if event.phase == "began" then if start == false then -- get stage and call 'setFocus()' method local stage = display.getCurrentStage() stage:setFocus( bubble ) bubble.isFocus = true bubble:applyLinearImpulse( -200, -500 , bubble.x+150, bubble.y+150 ) title:removeSelf() instructions:removeSelf() start = true local stage = display.getCurrentStage() stage:setFocus( bubble ) bubble.isFocus = true --Game functions function hitWall(event) bubble:setLinearVelocity( 0, 0 ) chooseLocation() end function chooseLocation(event) m = (math.random(24)) move() end function move(event) end end elseif bubble.isFocus then if event.phase == "ended" or event.phase == "cancelled" then local stage = display.getCurrentStage() stage:setFocus( nil ) loseGame() end end end bubble:addEventListener("touch", gameStart) bg:addEventListener("touch",bgF)

Thanks a ton for everyone’s help and support!

Does anyone have any clue on how to do this? Not to rush anyone but its kind of important.

Thanks!

Bump :slight_smile:

I could use some help about now

Your code is really hard to read.  Maybe you could indent things so we can see where functions really begin and end.  

I have had no luck so far. The “canceled” phase is not responding. When event.phase == “canceled” the bubble should disappear but it doesn’t. However with event.phase == “ended” it works, but only if the finger is lifted from the screen. And I do not think I can compare locations because the bubble will keep moving.

Any help is greatly appreciated. 

Try setting stage focus on object during touch event