Search code examples
swifttouchskspritenode

Move 2 different Spritenodes in the touchesMoved function


i'm trying to make a 2 player game in Swift, so far I can detect multiple touches and move each node individually, however when both players try to move their node at the same time one always takes control and moves both nodes at the same time, the code I have for when both nodes are being touched is the following:

if Touching && Touching2 {
        for touch: AnyObject in touches {
            let Loc = (touch as! UITouch).location(in: self)
            let PrevLoc = (touch as! UITouch).previousLocation(in: self)
            let Shell = self.childNode(withName: ShellCategory) as! SKSpriteNode
            var NewPos = Shell.position.x + (Loc.x - PrevLoc.x)
            NewPos = max(NewPos, Shell.size.width / 2)
            NewPos = min(NewPos, self.size.width - Shell.size.width / 2)
            Shell.position = CGPoint(x:NewPos, y: Shell.position.y)

            let Loc2 = (touch as! UITouch).location(in: self)
            let PrevLoc2 = (touch as! UITouch).previousLocation(in: self)
            let Shell2 = self.childNode(withName: ShellCategory2) as! SKSpriteNode
            var NewPos2 = Shell.position.x + (Loc2.x - PrevLoc2.x)
            NewPos2 = max(NewPos2, Shell2.size.width / 2)
            NewPos2 = min(NewPos2, self.size.width - Shell2.size.width / 2)
            Shell2.position = CGPoint(x:NewPos2, y: Shell2.position.y)
        }
    }

Thanks beforehand.


Solution

  • Well, after some investigation I finally found what I needed, I'm leaving this here for future developers who might find themselves in similar situations.

    Multi-touch gesture in Sprite Kit