如何根据分数进行 SKSpriteNode 更改?
Posted
技术标签:
【中文标题】如何根据分数进行 SKSpriteNode 更改?【英文标题】:How do I make an SKSpriteNode change dependent on a score? 【发布时间】:2017-08-05 22:59:24 【问题描述】:我正在尝试编写一个更酷的 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】:您可以在 score 变量的 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)
【讨论】:
以上是关于如何根据分数进行 SKSpriteNode 更改?的主要内容,如果未能解决你的问题,请参考以下文章
如何在没有 SKTexture 的情况下更改 didBeginContact 中的 SKSpriteNode 大小和颜色?