简体   繁体   中英

Changing texture of a SpriteKit node

I have an object that includes a SpriteKit node:

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:

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

I also tried to run self.scene!.view!.setNeedsDisplay() to no avail.

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. Use the texture property instead. It's the exact same as if you use the SKAction but much easier to use and to understand:

pauseImage.node.texture = pauseImage.playTexture

Also you don't have to use SKTexture.init just use:

SKTexture(imageNamed: "play")

In a class that is used to represent a node, I used 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))

Cheers. 🍻

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