如何根据分数进行SKSpriteNode更改?

时间:2017-08-05 22:59:24

标签: ios swift skspritenode sktexture

我试图编写一个更酷的版本Flappy Bird,其中鸟变为更酷的东西,这取决于分数。真的很感激你的帮助。这是宣布这只鸟的完整代码。我怎样才能根据分数制作更新鸟类的功能?

let birdTexture1 = SKTexture(imageNamed: "bird-01")
birdTexture1.filteringMode = .nearest
let birdTexture2 = SKTexture(imageNamed: "bird-02")
birdTexture2.filteringMode = .nearest

let anim = SKAction.animate(with: [birdTexture1, birdTexture2],     timePerFrame: 0.2)
let flap = SKAction.repeatForever(anim)

bird = SKSpriteNode(texture: birdTexture1)
bird.setScale(2.0)
bird.position = CGPoint(x: self.frame.size.width * 0.35, y:self.frame.size.height * 0.6)
bird.run(flap)

bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
bird.physicsBody?.isDynamic = true
bird.physicsBody?.allowsRotation = false

bird.physicsBody?.categoryBitMask = birdCategory
bird.physicsBody?.collisionBitMask = worldCategory | pipeCategory
bird.physicsBody?.contactTestBitMask = worldCategory | pipeCategory

self.addChild(bird)

1 个答案:

答案 0 :(得分:0)

您可以在得分变量的didSet函数中创建此行为。

每当你增加(或减少)玩家的分数时,它就会运行。

var score: Int {
    didSet {
        if score > 69 {
            bird.removeAllActions()

            let coolBirdTexture1 = SKTexture(imageNamed: "coolbird-01")
            coolBirdTexture1.filteringMode = .nearest
            let coolBirdTexture2 = SKTexture(imageNamed: "coolbird-02")
            coolBirdTexture2.filteringMode = .nearest

            let anim = SKAction.animate(with: [coolBirdTexture1, coolBirdTexture2], timePerFrame: 0.2)
            let flap = SKAction.repeatForever(anim)

            bird.texture = coolBirdTexture1
            bird.run(flap)
        }
    }
}