簡體   English   中英

更改 SpriteKit 節點的紋理

[英]Changing texture of a SpriteKit node

我有一個包含 SpriteKit 節點的對象:

class PauseButton{
    var playTexture: SKTexture = SKTexture.init(imageNamed: "play")
    var pauseTexture: SKTexture = SKTexture.init(imageNamed: "pause")
    var node: SKSpriteNode

    init(){
        self.node = SKSpriteNode(texture: pauseTexture)
    }
}

我正在嘗試通過 SKAction 更改其紋理,但沒有任何反應:

pauseImage.node.runAction(SKAction.setTexture(pauseImage.playTexture))

我也嘗試運行self.scene!.view!.setNeedsDisplay()無濟於事。

嘗試:

    var playTexture: SKTexture = SKTexture.init(imageNamed: "play")
    var pauseTexture: SKTexture = SKTexture.init(imageNamed: "pause")
    var node: SKSpriteNode
    let changeTextures = SKAction.animateWithTextures([pauseTexture], timePerFrame: 0.1)
    node.runAction(changeTextures)

不要SKAction使用SKAction 請改用texture屬性。 它與使用SKAction但更易於使用和理解:

pauseImage.node.texture = pauseImage.playTexture

此外,您不必使用SKTexture.init只需使用:

SKTexture(imageNamed: "play")

在用於表示節點的類中,我使用了self.run(SKAction.setTexture(ballTexture))

如果您使用的用例與我不同,那么您只需使用nodeName.run(SKAction.setTexture(ballTexture))

干杯。 🍻

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM