Reputation: 968
I'm building my first game in Swift and I want to execute a code block that places sprites at my fingers touch location- only if the touch started outside of a certain area of the display. This touch may or may not be continuous, and therefore it may or may not be continuously generating new sprites, so the my logic is in touchesMoved
. Right now I'm using
for touch : AnyObject in touches {
let location = touch.locationInNode(self)
...
to grab each touch location. However, this seems to be continuously updated and I'm not sure how to reference one specific touch and its starting point (assuming multiple fingers are down at a time). I looked into UITouch phase
, specifically touchEnded
, to see if I could work out some other way of going about my problem, but I can't even figure out how to access the phase
of a touch using the Apple Docs. (This doesn't seem to work: touch.phase == UITouchPhase.Ended
I think that I'm also fundamentally misunderstanding how to handle multiple touch events and how to pass information between them (touchesBegan
,touchesMoved
,touchesEnded
).
I may be missing something obvious, but I'm surprised that the docs did a seemingly-poor job explaining and that there don't seem to be many good answers online regarding this question. Any help would be awesome.
Upvotes: 0
Views: 612
Reputation: 21
To answer the question on how to pass a value that belongs to the first touch: Create a variable in your original class
var initialPosition = CGPoint()
In the touchesBegan assign the touch position to initialPosition
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
originalPosition = location
}
}
In the touchesMoved you now can use the position of the first touch.
Upvotes: 1