Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a border to an SKSpriteNode, similar to UIView?

I'm interested if an SKSpriteNode can be made to imitate the behavior of a UIView where I can specify border and corner radius?

self.view.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.view.layer.borderWidth = 2;
self.view.layer.cornerRadius = 2;
self.view.layer.masksToBounds = YES;
like image 513
Alex Stone Avatar asked Jan 02 '14 18:01

Alex Stone


1 Answers

The following is a simple, dropin extension for SKSpriteNode that will allow you to draw a border with a given color around your node.

import SpriteKit

extension SKSpriteNode {
    func drawBorder(color: UIColor, width: CGFloat) {
        let shapeNode = SKShapeNode(rect: frame)
        shapeNode.fillColor = .clear
        shapeNode.strokeColor = color
        shapeNode.lineWidth = width
        addChild(shapeNode)
    }
}
like image 77
CodeBender Avatar answered Oct 15 '22 21:10

CodeBender