Sebastian
Sebastian

Reputation: 6384

Which is the best way to move o collection of nodes?

In my game I need a Label with one character with a background and a border. To Achieve this I create this code with a SKLabelNode, SKShapeNode and SKSpriteNode:

    override func didMoveToView(view: SKView) {
           
       let label = SKLabelNode(fontNamed:"AmericanTypewriter-CondensedBold")
       label.text = "H";
       label.fontColor = UIColor.blackColor()
       label.fontSize = 120;
       label.userInteractionEnabled = false
        
        let background = SKSpriteNode(color: UIColor(red: 255, green: 255, blue: 255, alpha: 0.4), size: CGSizeMake(label.frame.size.width*1.2, label.frame.size.height*1.2))
        background.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
        background.name = "bkg"
        background.addChild(label)
        label.position = CGPoint(x:0, y:-label.frame.size.height/2)
        
        let border = SKShapeNode(path: CGPathCreateWithRect(CGRectMake(-(background.frame.size.width/2), -(background.frame.size.height/2), background.frame.size.width, background.frame.size.height), nil))
        border.fillColor = UIColor.clearColor()
        border.strokeColor = UIColor.blackColor()
        border.lineWidth = 5.0
        border.userInteractionEnabled = false
        background.addChild(border)
        
        
        self.addChild(background)
        
        
        
    }

I looks like this:

enter image description here

Then I need to move this collection of nodes. I tried to achieve this that way:

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        for touch in touches {
            let location = touch.locationInNode(self)
            let touchedNode = nodeAtPoint(location)
            
            if touchedNode.name == "bkg" {
                touchedNode.position = location
            }
        }
    }

But it doesn't move smoothly and It's hard to tap on this.

I don't know if I change the way I build the label (with 3 nodes) or I change the way I tried to move it.

Is there a best way to move a collection of nodes?

Upvotes: 1

Views: 41

Answers (1)

Cal Stephens
Cal Stephens

Reputation: 793

If you add all of the nodes to a singular parent node, you could move that parent node and then all three of your existing nodes would move too.

Upvotes: 2

Related Questions