Created
June 13, 2013 22:16
-
-
Save figengungor/5777852 to your computer and use it in GitHub Desktop.
Dragging a display object in Corona
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- create object | |
| local myObject = display.newRect( 0, 0, 100, 100 ) | |
| myObject:setFillColor( 255 ) | |
| -- touch listener function | |
| function myObject:touch( event ) | |
| if event.phase == "began" then | |
| self.markX = self.x -- store x location of object | |
| self.markY = self.y -- store y location of object | |
| elseif event.phase == "moved" then | |
| local x = (event.x - event.xStart) + self.markX --event.xStart gives the point where your finger touch on the object at first | |
| local y = (event.y - event.yStart) + self.markY --event.y gives where your finger is now | |
| self.x, self.y = x, y -- move object based on calculations above | |
| end | |
| return true | |
| end | |
| -- make 'myObject' listen for touch events | |
| myObject:addEventListener( "touch", myObject ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment