SKSpriteNode - create a round corner node?

前端 未结 8 1330
猫巷女王i
猫巷女王i 2020-12-01 05:10

Is there a way to make a SKSpriteNode round cornered? I am trying to create a Tile likesqaure blocks with color filled SKSpriteNode:

SKSpriteNode *tile = [SK         


        
相关标签:
8条回答
  • 2020-12-01 05:44

    from the class reference:

    "An SKSpriteNode is a node that draws a textured image, a colored square, or a textured image blended with a color."

    It seems the easiest way is to draw a block with rounded corners and then use one of these class methods:

    • spriteNodeWithImageNamed:
    • spriteNodeWithTexture:
    • spriteNodeWithTexture:size:
    0 讨论(0)
  • 2020-12-01 05:51

    Swift 4:

    A good solution could be created before you should add your sprite to his parent, I've maded a little extension that could be corrected as you wish:

    extension SKSpriteNode {
        func addTo(parent:SKNode?, withRadius:CGFloat) {
            guard parent != nil else { return }
            guard  withRadius>0.0 else {
                parent!.addChild(self)
                return
            }
            let radiusShape = SKShapeNode.init(rect: CGRect.init(origin: CGPoint.zero, size: size), cornerRadius: withRadius)
            radiusShape.position = CGPoint.zero
            radiusShape.lineWidth = 2.0
            radiusShape.fillColor = UIColor.red
            radiusShape.strokeColor = UIColor.red
            radiusShape.zPosition = 2
            radiusShape.position = CGPoint.zero
            let cropNode = SKCropNode()
            cropNode.position = self.position
            cropNode.zPosition = 3
            cropNode.addChild(self)
            cropNode.maskNode = radiusShape
            parent!.addChild(cropNode)
        }
    }
    

    Usage:

    let rootNode = SKSpriteNode(color: .white, size: self.size)
    rootNode.alpha = 1.0
    rootNode.anchorPoint = CGPoint.zero
    rootNode.position = CGPoint.zero
    rootNode.zPosition = 1
    rootNode.name = "rootNode"
    rootNode.addTo(parent: self, withRadius: 40.0)
    
    0 讨论(0)
  • 2020-12-01 05:51

    Here's a Swift 3 snippet based on the second solution of the accepted answer.

    func createPlayerRoundedNode(){
        let tile = SKSpriteNode(color: .white, size: CGSize(width: 30, height: 30))
        tile.zPosition = 3
        tile.name = "tile node"
        let cropNode = SKCropNode()
        cropNode.zPosition = 2
        cropNode.name = "crop node"
        let mask = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 30, height: 30), cornerRadius: 10)
        mask.fillColor = SKColor.darkGray
        mask.zPosition = 2
        mask.name = "mask node"
        cropNode.maskNode = mask
        self.addChild(cropNode)
        cropNode.addChild(tile)
    }
    
    0 讨论(0)
  • 2020-12-01 05:52

    In Swift 3 you can create with:

    let tile = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 30, height: 30), cornerRadius: 10)
    
    0 讨论(0)
  • 2020-12-01 05:57

    To get a rounded corner node you can use 2 approaches, each of them requires use of SKShapeNode.

    First way is to use SKShapeNode and set its path to be a rounded rectangle like this:

    SKShapeNode* tile = [SKShapeNode node];
    [tile setPath:CGPathCreateWithRoundedRect(CGRectMake(-15, -15, 30, 30), 4, 4, nil)];
    tile.strokeColor = tile.fillColor = [UIColor colorWithRed:0.0/255.0
                                                        green:128.0/255.0
                                                         blue:255.0/255.0
                                                        alpha:1.0];
    

    The other one uses sprite node,crop node and SKShapeNode with rounded rectangle as crop nodes mask:

    SKSpriteNode *tile = [SKSpriteNode spriteNodeWithColor:[UIColor   colorWithRed:0.0/255.0
                                                                               green:128.0/255.0
                                                                                blue:255.0/255.0
                                                                               alpha:1.0] size:CGSizeMake(30, 30)];
    SKCropNode* cropNode = [SKCropNode node];
    SKShapeNode* mask = [SKShapeNode node];
    [mask setPath:CGPathCreateWithRoundedRect(CGRectMake(-15, -15, 30, 30), 4, 4, nil)];
    [mask setFillColor:[SKColor whiteColor]];
    [cropNode setMaskNode:mask];
    [cropNode addChild:tile];
    

    If your tiles are one solid colour, i suggest you go with the first approach.

    0 讨论(0)
  • 2020-12-01 06:02

    This is heavily inspired by the accepted answer, yet it uses a more readable way to create the SKShapeNode and also fixes the annoying pixel line around the crop. Seems like a minor detail, but it may cost someone a few minutes.

    CGFloat cornerRadius = 15;
    SKCropNode *cropNode = [SKCropNode node];
    SKShapeNode *maskNode = [SKShapeNode shapeNodeWithRectOfSize:scoreNode.size cornerRadius:cornerRadius];
    [maskNode setLineWidth:0.0];
    [maskNode setFillColor:[UIColor whiteColor]];
    [cropNode setMaskNode:maskNode];
    [cropNode addChild:scoreNode];
    
    0 讨论(0)
提交回复
热议问题