简体   繁体   English

更改 SpriteKit 节点的纹理

[英]Changing texture of a SpriteKit node

I have an object that includes 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)
    }
}

And I'm trying to change its texture via SKAction, but nothing happens:我正在尝试通过 SKAction 更改其纹理,但没有任何反应:

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

I also tried to run self.scene!.view!.setNeedsDisplay() to no avail.我也尝试运行self.scene!.view!.setNeedsDisplay()无济于事。

Try:尝试:

    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)

Don't use an SKAction for that.不要SKAction使用SKAction Use the texture property instead.请改用texture属性。 It's the exact same as if you use the SKAction but much easier to use and to understand:它与使用SKAction但更易于使用和理解:

pauseImage.node.texture = pauseImage.playTexture

Also you don't have to use SKTexture.init just use:此外,您不必使用SKTexture.init只需使用:

SKTexture(imageNamed: "play")

In a class that is used to represent a node, I used self.run(SKAction.setTexture(ballTexture)) .在用于表示节点的类中,我使用了self.run(SKAction.setTexture(ballTexture))

If you aren't using the same usecase as I am, then you'd just use nodeName.run(SKAction.setTexture(ballTexture))如果您使用的用例与我不同,那么您只需使用nodeName.run(SKAction.setTexture(ballTexture))

Cheers.干杯。 🍻 🍻

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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