深度背景动画在SpriteKit

Sam*_*Sam 6 xcode background sprite-kit swift

我正在使用Swift和SpriteKit构建我的第一个游戏并想要添加背景.游戏发生在太空中,所以我想让背景中的星星以不同的速度移动.目前,我正在通过使较大的恒星比较小的恒星更快地在屏幕上移动来进行3D外观.有没有一种有效的方法来做到这一点,而不是像这样制作一个SKNode子类,并在一开始就将它作为一个孩子添加DidMoveToView?看起来这种方法非常密集,但我想我会一遍又一遍地重复使用相同的图像.

class BackGroundAnimation:SKNode{

let theView:SKView
init(aView:SKView){

    theView = aView

    super.init()

    animate()
}


func animate(){


    for _ in 1...200{

        let randomSize = random(1, max: 3)
        var randomPosx = random(1,max: 1000)
        randomPosx = randomPosx/1000.0
        var randomPosy = random(1,max: 1000)
        randomPosy = randomPosy/1000.0

        let star:SKSpriteNode = SKSpriteNode(texture:starTexture)
        star.setScale(randomSize/60.0)



        star.position = CGPoint(x:(theView.scene?.size.width)! * randomPosx,y:(theView.scene?.size.width)! * randomPosy)//    (self.scene.size.width)*randomPosx, y:(self.scene.size.height) * randomPosy)

        //star.position = CGPoint(x: 200,y: 200)

        star.physicsBody = SKPhysicsBody(circleOfRadius: star.size.width/2 )
        star.physicsBody?.collisionBitMask = 0
        star.physicsBody?.categoryBitMask = 0
        star.physicsBody?.contactTestBitMask = 0

        star.physicsBody?.linearDamping = 0
        star.physicsBody?.velocity = CGVector(dx:1 * randomSize, dy:0)
        star.name = "star"

        //addChild(star)
        self.addChild(star)
        self.moveToParent(self.scene!)


    }


}




required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
Run Code Online (Sandbox Code Playgroud)

}

任何帮助都会很棒.

Jos*_*rez 8

正如我在评论中提到的,您可以使用粒子创建一个美丽的视差背景.

在班级的任何地方添加此功能.

//Creates a new star field
func starfieldEmitterNode(speed speed: CGFloat, lifetime: CGFloat, scale: CGFloat, birthRate: CGFloat, color: SKColor) -> SKEmitterNode {
    let star = SKLabelNode(fontNamed: "Helvetica")
    star.fontSize = 80.0
    star.text = "?"
    let textureView = SKView()
    let texture = textureView.textureFromNode(star)
    texture!.filteringMode = .Nearest

    let emitterNode = SKEmitterNode()
    emitterNode.particleTexture = texture
    emitterNode.particleBirthRate = birthRate
    emitterNode.particleColor = color
    emitterNode.particleLifetime = lifetime
    emitterNode.particleSpeed = speed
    emitterNode.particleScale = scale
    emitterNode.particleColorBlendFactor = 1
    emitterNode.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMaxY(frame))
    emitterNode.particlePositionRange = CGVector(dx: CGRectGetMaxX(frame), dy: 0)
    emitterNode.particleSpeedRange = 16.0

    //Rotates the stars
    emitterNode.particleAction = SKAction.repeatActionForever(SKAction.sequence([
        SKAction.rotateByAngle(CGFloat(-M_PI_4), duration: 1),
        SKAction.rotateByAngle(CGFloat(M_PI_4), duration: 1)]))

    //Causes the stars to twinkle
    let twinkles = 20
    let colorSequence = SKKeyframeSequence(capacity: twinkles*2)
    let twinkleTime = 1.0 / CGFloat(twinkles)
    for i in 0..<twinkles {
        colorSequence.addKeyframeValue(SKColor.whiteColor(),time: CGFloat(i) * 2 * twinkleTime / 2)
        colorSequence.addKeyframeValue(SKColor.yellowColor(), time: (CGFloat(i) * 2 + 1) * twinkleTime / 2)
    }
    emitterNode.particleColorSequence = colorSequence

    emitterNode.advanceSimulationTime(NSTimeInterval(lifetime))
    return emitterNode
}
Run Code Online (Sandbox Code Playgroud)

然后添加此功能.这是创建星星层的功能.只需调用此函数,例如在didMoveToView中.

func createStarLayers() {
    //A layer of a star field
    let starfieldNode = SKNode()
    starfieldNode.name = "starfieldNode"
    starfieldNode.addChild(starfieldEmitterNode(speed: -48, lifetime: size.height / 23, scale: 0.2, birthRate: 1, color: SKColor.lightGrayColor()))
    addChild(starfieldNode)

    //A second layer of stars
    var emitterNode = starfieldEmitterNode(speed: -32, lifetime: size.height / 10, scale: 0.14, birthRate: 2, color: SKColor.grayColor())
    emitterNode.zPosition = -10
    starfieldNode.addChild(emitterNode)

    //A third layer
    emitterNode = starfieldEmitterNode(speed: -20, lifetime: size.height / 5, scale: 0.1, birthRate: 5, color: SKColor.darkGrayColor())
    starfieldNode.addChild(emitterNode)
}
Run Code Online (Sandbox Code Playgroud)

这就是它的样子.

在此输入图像描述