Touch Does Not End When Finger is Dragged Off Screen

Hi, this happens when I create a circle that I have added an event listener to it. If I touch the circle and release it prints both commands but when I touch the circle, drag my finger off screen and release, it does not print the second command. Please see as follow:

local function onTouch(event) if(event.phase=="began")then print("began detected") elseif(event.phase=="ended" or event.phase=="cancelled")then print("ended or cancelled detected") end end circle=display.newCircle(100, 100, 30) circle:addEventListener("touch", onTouch)

How do I make it so the event ends when I drag my finger off the screen? Please help!

Geez, should’ve literally waited another minute before posting.

adding: display.getCurrentStage():setFocus(circle) solved the problem!!

But here comes a second question. I am using storyboard and have “local group=self.view” in the createScene function. If I set “group” as global and not local, is that going to cause problems?

Hi @firdausm,

Without seeing all of your code, I will comment that global variables should be avoided whenever possible. Only if you have a very specific reason for using them, and can’t find a way to localize the same process, should globals be considered. :slight_smile:

Brent

Yup.  If you do the same thing in multiple story scenes, your scene’s views will trample on each other.

What you might want to do is at the top of the scene after the:

local scene = storyboard.newScene()

local group = scene.view

Then you can use the group variable anywhere with in that scene, though it’s a best practice to add methods to the scene and access it like createScene does, i.e.

Instead of:

local function doSomething(someParameters)

      group:insert(someobject) – the way I did it abofe

end

do this instead:

function scene:doSomething(someParameters)

      local group = self.view

      group:insert(someobject)

end

This is a much more object oriented way to manage this issue and leads to cleaner, easier to understand code.e

Rob

Thanks guys!

Geez, should’ve literally waited another minute before posting.

adding: display.getCurrentStage():setFocus(circle) solved the problem!!

But here comes a second question. I am using storyboard and have “local group=self.view” in the createScene function. If I set “group” as global and not local, is that going to cause problems?

Hi @firdausm,

Without seeing all of your code, I will comment that global variables should be avoided whenever possible. Only if you have a very specific reason for using them, and can’t find a way to localize the same process, should globals be considered. :slight_smile:

Brent

Yup.  If you do the same thing in multiple story scenes, your scene’s views will trample on each other.

What you might want to do is at the top of the scene after the:

local scene = storyboard.newScene()

local group = scene.view

Then you can use the group variable anywhere with in that scene, though it’s a best practice to add methods to the scene and access it like createScene does, i.e.

Instead of:

local function doSomething(someParameters)

      group:insert(someobject) – the way I did it abofe

end

do this instead:

function scene:doSomething(someParameters)

      local group = self.view

      group:insert(someobject)

end

This is a much more object oriented way to manage this issue and leads to cleaner, easier to understand code.e

Rob

Thanks guys!