I have two SKSpriteNode
, the cat and the black wall, each one of them has the class associated physicsBody
with it, that's why the cat is on top of the black wall.
I move the cat with my finger with the function touchesMoved
as follows:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node == miaum {
gato.position = location
gato.physicsBody?.affectedByGravity = false
}
}
}
The problem is that since the cat follows the finger, when the finger goes back and forth across the black wall, the cat follows the finger, and that shouldn't happen. Is there a quick and easy way to program so that doesn't happen?
Image that shows the problem that happens, I move the cat with my finger, according to the code explained above:
You must use the functions proposed by Sprite Kit for this type of cases. Collisions should be handled by the included physics engine and collision masks.
I leave you a reference link: https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html
As additional information, I suggest you not to modify the position of the "Cat" directly, rather change the direction and speed when you move it, so the collision detector of the same Sprite Kit engine will do the job.