Swift - Nodes pass from one side to another with touchesMoved if the physicsBody body is very thin


I have two SKSpriteNode the cat and the black wall, each of them have the class physicsBody associated so the cat is on top of the black wall.

The cat moves it with the finger with the function touchesMoved of the following form:

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 as the cat follows the finger, when the finger passes from one side to the other through the black wall, the cat passes too and that should not happen. Is there a quick and easy way to program so that does not happen?

Image that shows the problem that is happening, I move the cat with my finger, according to the code explained above:

asked by albertcito 02.02.2016 в 17:27

1 answer


You must use the functions proposed by Sprite Kit for this type of case. Collisions must be handled by the included physics engine and collision masks.

I leave a reference link: link

As an additional piece of information, I suggest you do not change the position directly from the "Cat", rather change the direction and speed when you move it, so the collision detector of the same Sprite Kit engine will do the job.

answered by 13.10.2016 / 15:44