Created
June 14, 2013 20:59
-
-
Save figengungor/5785233 to your computer and use it in GitHub Desktop.
Add focus to drag code so the objects won't interfere with each other while dragging on the screen.
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 objects | |
| local rect1 = display.newRect( 0, 0, 100, 100 ) | |
| local rect2 = display.newRect(300,300, 50,50) | |
| -- touch listener function | |
| local function move(self, event ) | |
| if event.phase == "began" then | |
| --set focus on touched object so it won't interfere with other display objects. | |
| display.getCurrentStage():setFocus( self, event.id ) | |
| self.isFocus = true | |
| --store the first location of the object | |
| self.x0 = self.x -- store x location of object | |
| self.y0 = self.y -- store y location of object | |
| elseif event.phase == "moved" then | |
| --add the amount of distance that you move the object, to first location of the object | |
| local x = (event.x - event.xStart) + self.x0 | |
| local y = (event.y - event.yStart) + self.y0 | |
| self.x, self.y = x, y -- move object based on calculations above | |
| elseif event.phase=="ended" or event.phase=="canceled" then | |
| --remove the focus on the object | |
| display.getCurrentStage():setFocus( self, nil ) | |
| self.isFocus = false | |
| end | |
| return true | |
| end | |
| rect1.touch=move | |
| rect2.touch=move | |
| rect1:addEventListener( "touch", rect1) | |
| rect2:addEventListener("touch", rect2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment