Touch events outside

Hi guys!

I am wondering… how do I know if a user has dragged his finger outside a touch area? It doesnt give me an event like: ‘ended’ or ‘draggedOutside’. 

And is there a way to find out if an area is touched, without listening to events from it? That would also suffice.

Thanks!

you need to set focus on the touch object to continue receiving events once you drag beyond the bounds of the object.

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

You will have to detect that yourself. For instance, if your object/area has a centre pivot, and event.x is greater than event.target.x + event.target.width/2, then the user moved their finger off the right side of the object. 

And as RG says above, the object will require focus…

Note: While the examples at that link are perfectly suitable, you may find this easier to use as it is compatiable with multi-touch on or off:

local function onTouch( self, event ) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( event.target, event.id ) -- YOUR CODE HERE elseif( self.isFocus ) then if( phase == "moved" ) then -- YOUR CODE HERE elseif( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( event.target, nil ) -- YOUR CODE HERE end end return false end

Then:

local obj = newRect( 100, 100, 100, 100 ) obj.touch = onTouch obj:addEventListener("touch")

Tip: One question per post gets better answers I totally missed the second question and will let nick answer that.

Alternately, if you want to include a few related questions try prefacing it with, "these are my question"s and then use a numbered list of questions.

OK, I lied I’ll answer the second question too.

First, SSK2 has this feature in it, but the concept  you’re describing is what I think of as ‘in bounds’.  Specifically, in bounds of an objec’ts axis-aligned bounding box:

local function isInBounds( obj, obj2 ) if(not obj2) then return false end local bounds = obj2.contentBounds if( obj.x \> bounds.xMax ) then return false end if( obj.x \< bounds.xMin ) then return false end if( obj.y \> bounds.yMax ) then return false end if( obj.y \< bounds.yMin ) then return false end return true end

With the above code, you can extend my original example as follows:

local function onTouch( self, event ) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( event.target, event.id ) -- SET OBJECT TO GREEN FILL self:setFillColor(0,1,0) elseif( self.isFocus ) then -- Touch in bounds? local inBounds = isInBounds( event, self ) if( phase == "moved" ) then -- Highlight as green it touch in bounds else clear to white if(inBounds) then self:setFillColor(0,1,0) else self:setFillColor(1,1,1) end elseif( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( event.target, nil ) -- Clear to white, touch is done self:setFillColor(1,1,1) end end return false end

Thank you very much!

you need to set focus on the touch object to continue receiving events once you drag beyond the bounds of the object.

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

You will have to detect that yourself. For instance, if your object/area has a centre pivot, and event.x is greater than event.target.x + event.target.width/2, then the user moved their finger off the right side of the object. 

And as RG says above, the object will require focus…

Note: While the examples at that link are perfectly suitable, you may find this easier to use as it is compatiable with multi-touch on or off:

local function onTouch( self, event ) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( event.target, event.id ) -- YOUR CODE HERE elseif( self.isFocus ) then if( phase == "moved" ) then -- YOUR CODE HERE elseif( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( event.target, nil ) -- YOUR CODE HERE end end return false end

Then:

local obj = newRect( 100, 100, 100, 100 ) obj.touch = onTouch obj:addEventListener("touch")

Tip: One question per post gets better answers I totally missed the second question and will let nick answer that.

Alternately, if you want to include a few related questions try prefacing it with, "these are my question"s and then use a numbered list of questions.

OK, I lied I’ll answer the second question too.

First, SSK2 has this feature in it, but the concept  you’re describing is what I think of as ‘in bounds’.  Specifically, in bounds of an objec’ts axis-aligned bounding box:

local function isInBounds( obj, obj2 ) if(not obj2) then return false end local bounds = obj2.contentBounds if( obj.x \> bounds.xMax ) then return false end if( obj.x \< bounds.xMin ) then return false end if( obj.y \> bounds.yMax ) then return false end if( obj.y \< bounds.yMin ) then return false end return true end

With the above code, you can extend my original example as follows:

local function onTouch( self, event ) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( event.target, event.id ) -- SET OBJECT TO GREEN FILL self:setFillColor(0,1,0) elseif( self.isFocus ) then -- Touch in bounds? local inBounds = isInBounds( event, self ) if( phase == "moved" ) then -- Highlight as green it touch in bounds else clear to white if(inBounds) then self:setFillColor(0,1,0) else self:setFillColor(1,1,1) end elseif( phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( event.target, nil ) -- Clear to white, touch is done self:setFillColor(1,1,1) end end return false end

Thank you very much!