美文网首页ARKit&SceneKit入门
SpriteKit(8) - 粒子系统

SpriteKit(8) - 粒子系统

作者: li_礼光 | 来源:发表于2017-08-01 17:55 被阅读207次

    粒子系统

    • 加载粒子文件

    import SpriteKit
    import GameplayKit
    
    class GamePartical: SKScene {
        var snow :SKEmitterNode!
        override func didFinishUpdate() {
            self.size = UIScreen.main.bounds.size
            snow = SKEmitterNode(fileNamed: "snow.sks")
            snow.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
            self.addChild(snow!)
        }
    }
    
    创建粒子.png 粒子编辑器 模拟器效果
    • 代码创建

    import SpriteKit
    import GameplayKit
    
    class GamePartical: SKScene {
        var emitter : SKEmitterNode!
        override func didMove(to view: SKView) {
            let rainTexture = SKTexture(imageNamed: "zombie1")
            emitter = SKEmitterNode()
            emitter.particleTexture = rainTexture
            emitter.particleBirthRate = 10
            emitter.particleColor = UIColor.white
            emitter.particleSpeed = -450
            emitter.particleScaleRange = 150
            emitter.particleLifetime = 2
            
            emitter.particleScale = 0.2
            emitter.particleScaleRange = 0.5
            emitter.particleAlpha = 0.7
            emitter.particleAlphaRange = 0.5
            
            emitter.emissionAngle = 0
            emitter.emissionAngleRange = CGFloat.pi * 2
            
            emitter.particleColorBlendFactor = 1
            emitter.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
            self.addChild(emitter)
        }
    }
    
    粒子效果

    PS : 太多的粒子会大量占用系统资源,让游戏变卡,所以使用时要注意


    使用关键帧序列配置粒子属性

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            let scaleSequence = SKKeyframeSequence(keyframeValues: [0.2,0.9,0.1], times: [0.0, 0.5, 0.75])
            emitter.particleScaleSequence = scaleSequence
        }
    
    关键帧序列配置粒子属性

    PS : 也就是改变粒子的运动内容.

    为粒子添加动作

    • 使用SKAction方式

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            let moveAction = SKAction.moveBy(x: 300, y: 0, duration: 1)
            emitter.run(moveAction)
        }
    
    移动效果
    • 使用partcleAction属性添加动作

    let color = SKAction.colorize(with: SKColor.green, colorBlendFactor: 0.5, duration: 1)
    emitter.particleAction = color
    

    PS : 测试了N遍,貌似用这个动作添加没有效果. 有可能是我的操作方式不对.原教材也是一样,没有效果,不会变颜色.玛德,坑爹.这种方式暂时不考虑,可能有坑

    清屏操作

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            emitter.resetSimulation()
        }
    
    清屏操作

    相关文章

      网友评论

        本文标题:SpriteKit(8) - 粒子系统

        本文链接:https://www.haomeiwen.com/subject/cmmjlxtx.html