简体   繁体   中英

How do I make an SKSpriteNode change dependent on a score?

I'm trying to code a cooler version of Flappy Bird where the bird changes to something cooler which is contingent on the score. Your help would really be appreciated. Here is the full code declaring the bird. How could I make a function that updates the bird dependant on the score?

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)

You can create this behavior in your score variable's didSet function.

This runs whenever you increment (or decrement) the player's score.

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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM