SpriteKit(9) - 其他节点内容

修剪节点(遮盖效果)

有点类似于CALayer

import SpriteKit
import GameplayKit

class GameShapeNode: SKScene {
    override func didMove(to view: SKView) {
        self.size = UIScreen.main.bounds.size
        backgroundColor = UIColor.white
        //修剪节点
        let  pictureNode = SKSpriteNode(imageNamed: "star.jpg")
        pictureNode.position = CGPoint.zero
        pictureNode.setScale(0.9)

        let maskNode = SKSpriteNode(imageNamed: "starBg.png")
        let cropNode = SKCropNode()
        cropNode.position = self.view!.center
        cropNode.addChild(pictureNode)        //添加内容
        cropNode.maskNode = maskNode          //超过maskNode范围的内容都被裁剪
        
        self.addChild(cropNode)
    }
}
SpriteKit(9) - 其他节点内容_第1张图片
debug view
SpriteKit(9) - 其他节点内容_第2张图片
效果图

形状节点

  • 矩形

let  shapeNode = SKShapeNode(rect: CGRect(x: self.frame.midX - 100, y: self.frame.midY - 100, width: 200, height: 200))
shapeNode.strokeColor = UIColor.white
shapeNode.fillColor = UIColor.red
self.addChild(shapeNode)
SpriteKit(9) - 其他节点内容_第3张图片
矩形
  • 圆角矩形

方式一

let rect = CGRect(x: self.frame.midX - 100, y: self.frame.midY - 100, width: 200, height: 200)
let shapeNode = SKShapeNode(rect: rect, cornerRadius: 50)
shapeNode.strokeColor = UIColor.white
shapeNode.fillColor = UIColor.red
self.addChild(shapeNode)

方式二

let rect = CGRect(x: self.frame.midX - 100, y: self.frame.midY - 100, width: 200, height: 200)
let shapeNode = SKShapeNode(rectOf: rect.size, cornerRadius: 20)
shapeNode.position = self.view!.center
shapeNode.strokeColor = UIColor.white
shapeNode.fillColor = UIColor.red
self.addChild(shapeNode)
SpriteKit(9) - 其他节点内容_第4张图片
圆角矩形
  • 圆形

let shapeNode = SKShapeNode(circleOfRadius: 100)
SpriteKit(9) - 其他节点内容_第5张图片
圆形.png
  • 椭圆

方式一

 let rect = CGRect(x: self.frame.midX - 100, y: self.frame.midY - 50, width: 200, height: 100)
let shapeNode = SKShapeNode(ellipseIn: rect)
shapeNode.strokeColor = UIColor.white
shapeNode.fillColor = UIColor.red
self.addChild(shapeNode)

方式二

let shapeNode = SKShapeNode(ellipseOf: rect.size)
shapeNode.position = self.view!.center
SpriteKit(9) - 其他节点内容_第6张图片
椭圆

个性化属性

import SpriteKit
import GameplayKit

class GameShape: SKScene {
    override func didMove(to view: SKView) {
        self.size = UIScreen.main.bounds.size
        let rect = CGRect(x: self.frame.midX - 100, y: self.frame.midY - 50, width: 100, height: 200)
        let shapeNode = SKShapeNode(ellipseOf: rect.size)
        shapeNode.position = self.view!.center
        shapeNode.strokeColor = UIColor.white       //填充颜色
        shapeNode.fillColor = UIColor.red           //线颜色
        shapeNode.lineWidth = 10                    //线宽
        shapeNode.glowWidth = 4                     //光晕
        self.addChild(shapeNode)
    }
}
SpriteKit(9) - 其他节点内容_第7张图片
其他属性.png
  • 形状路径

import SpriteKit
import GameplayKit

class GameShape: SKScene {
    override func didMove(to view: SKView) {
        self.size = UIScreen.main.bounds.size
        let shapeNode = SKShapeNode()
        shapeNode.position = self.view!.center
        let path = CGMutablePath()
        let point_1 = CGPoint.zero
        let point_2 = CGPoint(x: point_1.x + 100, y: point_1.y + 100)
        let point_3 = CGPoint(x: point_1.x + 200, y: point_1.y )
        let point_4 = CGPoint(x: point_1.x + 100, y: point_1.y - 100)
        let point_5 = point_1
        path.move(to: point_1)
        path.addLine(to: point_2)
        path.addLine(to: point_3)
        path.addLine(to: point_4)
        path.addLine(to: point_5)
        shapeNode.path = path
        shapeNode.strokeColor = UIColor.white       //填充颜色
        shapeNode.fillColor = UIColor.red           //线颜色
        shapeNode.lineWidth = 25                    //线宽
        shapeNode.glowWidth = 4                     //光晕
        shapeNode.lineJoin = .round                 //线连接方式
        self.addChild(shapeNode)
    }
}
SpriteKit(9) - 其他节点内容_第8张图片
形状路径

效果节点

  • 高斯模糊

import SpriteKit
import GameplayKit

class GameBlur: SKScene {
    override func didMove(to view: SKView) {
        self.size = UIScreen.main.bounds.size

        //添加滤镜
        let effectNode = SKEffectNode()
        effectNode.position = self.view!.center
        self.addChild( effectNode)
        
        let bgNode = SKSpriteNode(imageNamed: "background1.png")
        bgNode.size = self.view!.bounds.size
        effectNode.addChild(bgNode)
        
        //设置滤镜
        let filter = CIFilter(name: "CIGaussianBlur")!
        filter.setDefaults()
        filter.setValue(NSNumber(value: 10.0), forKey: "inputRadius")
        effectNode.filter = filter

    }
}
SpriteKit(9) - 其他节点内容_第9张图片
高斯模糊

总结

  • 不管是SKEffectNode还是SKCropNode, 都是以该节点为底,然后再用该节点,addChild其他节点内容.
  • 最好测试都用真机测试,模拟器可能有内存消耗不准确的问题.

你可能感兴趣的:(SpriteKit(9) - 其他节点内容)