Skspritenode纹理动画不起作用

时间:2018-01-25 23:15:26

标签: swift swift3 skspritenode skaction

我只是尝试在我的场景中制作一些硬币。在我的其他项目中,动画有效。但最近不在这里。

func addCoins() {

    for coins in map.coinSpawns {

        var coin = SKSpriteNode(imageNamed: "coin1")
        coin.position = coins
        coin.size = CGSize(width:map.tileSize - 10,height: map.tileSize - 10)
        let action = SKAction.repeatForever(SKAction.animate(withNormalTextures: [SKTexture(imageNamed: "coin1.png"),SKTexture(imageNamed: "coin2.png"),SKTexture(imageNamed: "coin3.png"),SKTexture(imageNamed: "coin4.png")], timePerFrame: 0.5, resize: false, restore: true))

        self.addChild(coin)
        coin.run(action)
        self.coins.append(coin)
    }
}

1 个答案:

答案 0 :(得分:1)

尝试重构一下你的代码,从文件名中删除.png(应该是修复)并在coin循环之外提取纹理数组(优化),所以你的代码可能是:

func addCoins() {
    let textures = ["coin1", "coin2", "coin3", "coin4"].flatMap { SKTexture(imageNamed: $0) }

    for coins in map.coinSpawns {
        var coin = SKSpriteNode(imageNamed: "coin1")
        coin.position = coins
        coin.size = CGSize(width:map.tileSize - 10,height: map.tileSize - 10)
        self.addChild(coin)
        self.coins.append(coin)

        let action = SKAction.repeatForever(SKAction.animate(with: textures, timePerFrame: 0.5, resize: true, restore: false))
        coin.run(action)
    }
}