Dragging Object Explanation

I am a self learning newbie in corona. I was going through google search on how to drag a object and I found following codes in thatssopanda

Can anyone explain me the following 

  1. why do we need to use display.getCurrentStage():setFocus(self, event.id)

  2. What is the use of self and when should I use self in my codes and what does self.markx means

  3. what does self.isFocus=true means

  4. What does event.xStart and event.y gives us

  5. hide the status bar, shouldnt this just be on by default?

  6. display.setStatusBar(display.HiddenStatusBar)

  7.  

  8. – create a circle and put it in the center of the screen

  9. local circle = display.newCircle( display.contentWidth*0.5,display.contentHeight*0.5, 75)

  10. circle:setFillColor( 255 )

  11.  

  12. – touch listener function

  13. function circle:touch( event )

  14. if event.phase == “began” then

  15. – first we set the focus on the object

  16. display.getCurrentStage():setFocus( self, event.id )

  17. self.isFocus = true

  18.  

  19. – then we store the original x and y position

  20. self.markX = self.x

  21. self.markY = self.y

  22.  

  23. elseif self.isFocus then

  24.  

  25. if event.phase == “moved” then

  26. – then drag our object

  27. self.x = event.x - event.xStart + self.markX

  28. self.y = event.y - event.yStart + self.markY

  29. elseif event.phase == “ended” or event.phase == “cancelled” then

  30. – we end the movement by removing the focus from the object

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

  32. self.isFocus = false

  33. end

  34.  

  35. end

  36.  

  37. – return true so Corona knows that the touch event was handled propertly

  38. return true

  39. end

  40.  

  41. – finally, add an event listener to our circle to allow it to be dragged

  42. circle:addEventListener( “touch”, circle )

@all - Please read and correct if I goof any details.
 
 
its4krishna - Part 1
Before I answer, I hope that you’ve looked at the API docs (https://docs.coronalabs.com/daily/api/) and specifically read these entries:

1. Why do we need to use display.getCurrentStage():setFocus(self, event.id
 
When you touch the screen, the device starts tracking that touch.  Until your finger is lifted, the device can differentiate this touch from another finger touching the screen before or after.
 
To be clear, the device knows how to group began, moved, and ended phases for a specific touch.
 
setFocus() tells the system to start sending all other touch events related to the current touch event being processed to the current object doing the processing:
[background=#fafafa]-- This code is in the current listener.[/size][/background]

-- 'self' is the object currently receiving the event details -- event.id is required if you are allowing multi-touch. Each touch (in multitouch) has a -- unique ID display.getCurrentStage():setFocus( self, event.id ) -- Now, all future events related to this touch will be sent to 'self' for processing.

 
Current Stage & setFocus() Relationship
This is a fancy name for ‘top group’.  The top group contains all other groups and gets all touch events sent to it first.  So, we must setFocus() via ‘current stage’ to ensure that the event gets routed to the right child of that stage.
 
 

2. What is the use of self and when should I use self in my codes and what does self.markx means

Self is the object which the listener/function is ‘attached’ to.  This is not something that can be explained in a few lines, so let me share this link with you (and how the community):
https://docs.google.com/document/d/1Y84OufJB_eGZFkUknX28IBZFRV2_-gw1OOTsozN-IK4/edit?usp=sharing
 
I wrote a quick and dirty guide for a friend.
 
I suggest reading the entire document, but pay close attention to pages 7 … 8 for an answer to your question about self.
 
self.markX = something
This is creating a field named ‘markX’ on the object (self) that is calling this listener.  It then asigns a value (something).
 
This is ‘field’ is now forever available on that object till it is set to ‘nil’.  So, in later calls to the touch listener, we can check for the last value in that field if we need it (which is what he is doing).
 
 

3. what does self.isFocus=true means

As with self.markX, this is a field we are creating and setting to true (or false) to use as a flag in later calls to the listener.
 
When it is set to true, this listener will proceed to the correct if-the-else branch for a ‘currently focused’ object.
 
 

4. What does event.xStart and event.y gives us
 
This is all in the API docs.  Please read/follow each link on this page:
https://docs.coronalabs.com/daily/api/event/touch/index.html
 
 
its4krishna - Part 2
Be sure you have spent time doing plenty of Lua programming exercises before you try to understand Corona code.  Without a solid foundation you will quickly become lost and start forming bad habits based on partial knowledge.  
 
I’m not chastising you here, but hoping to help you.
 
I also strongly suggest following some formal guides or training like Dr. Brian Burton’s Corona Books, especially if you don’t already know at least one programming language and/or do not have a formal education (taught via classes) in programming.

Note: As much as I want to help you get started, please take my advice about the guides, docs, and book based training.

The forums are not a place to get instruction/training.  They are meant to answer well defined ’ small’ single-topic specific questions.

It is very hard to know what you will understand without knowing your background and level of skill/knowledge.  Thus is is hard to answer questions with 100% certainty that you will understand and benefit from my (and other folks’) effort.

PS - Please post back if you can’t find suitable training materials as I and others will be happy to point you in the right direction.

Hi again.

While my doc was OK, you should definitely be sure to work your way through Corona’s own training:

as well as external books:

@all - Please read and correct if I goof any details.
 
 
its4krishna - Part 1
Before I answer, I hope that you’ve looked at the API docs (https://docs.coronalabs.com/daily/api/) and specifically read these entries:

1. Why do we need to use display.getCurrentStage():setFocus(self, event.id
 
When you touch the screen, the device starts tracking that touch.  Until your finger is lifted, the device can differentiate this touch from another finger touching the screen before or after.
 
To be clear, the device knows how to group began, moved, and ended phases for a specific touch.
 
setFocus() tells the system to start sending all other touch events related to the current touch event being processed to the current object doing the processing:
[background=#fafafa]-- This code is in the current listener.[/size][/background]

-- 'self' is the object currently receiving the event details -- event.id is required if you are allowing multi-touch. Each touch (in multitouch) has a -- unique ID display.getCurrentStage():setFocus( self, event.id ) -- Now, all future events related to this touch will be sent to 'self' for processing.

 
Current Stage & setFocus() Relationship
This is a fancy name for ‘top group’.  The top group contains all other groups and gets all touch events sent to it first.  So, we must setFocus() via ‘current stage’ to ensure that the event gets routed to the right child of that stage.
 
 

2. What is the use of self and when should I use self in my codes and what does self.markx means

Self is the object which the listener/function is ‘attached’ to.  This is not something that can be explained in a few lines, so let me share this link with you (and how the community):
https://docs.google.com/document/d/1Y84OufJB_eGZFkUknX28IBZFRV2_-gw1OOTsozN-IK4/edit?usp=sharing
 
I wrote a quick and dirty guide for a friend.
 
I suggest reading the entire document, but pay close attention to pages 7 … 8 for an answer to your question about self.
 
self.markX = something
This is creating a field named ‘markX’ on the object (self) that is calling this listener.  It then asigns a value (something).
 
This is ‘field’ is now forever available on that object till it is set to ‘nil’.  So, in later calls to the touch listener, we can check for the last value in that field if we need it (which is what he is doing).
 
 

3. what does self.isFocus=true means

As with self.markX, this is a field we are creating and setting to true (or false) to use as a flag in later calls to the listener.
 
When it is set to true, this listener will proceed to the correct if-the-else branch for a ‘currently focused’ object.
 
 

4. What does event.xStart and event.y gives us
 
This is all in the API docs.  Please read/follow each link on this page:
https://docs.coronalabs.com/daily/api/event/touch/index.html
 
 
its4krishna - Part 2
Be sure you have spent time doing plenty of Lua programming exercises before you try to understand Corona code.  Without a solid foundation you will quickly become lost and start forming bad habits based on partial knowledge.  
 
I’m not chastising you here, but hoping to help you.
 
I also strongly suggest following some formal guides or training like Dr. Brian Burton’s Corona Books, especially if you don’t already know at least one programming language and/or do not have a formal education (taught via classes) in programming.

Note: As much as I want to help you get started, please take my advice about the guides, docs, and book based training.

The forums are not a place to get instruction/training.  They are meant to answer well defined ’ small’ single-topic specific questions.

It is very hard to know what you will understand without knowing your background and level of skill/knowledge.  Thus is is hard to answer questions with 100% certainty that you will understand and benefit from my (and other folks’) effort.

PS - Please post back if you can’t find suitable training materials as I and others will be happy to point you in the right direction.

Hi again.

While my doc was OK, you should definitely be sure to work your way through Corona’s own training:

as well as external books: