Skspritenode 纹理动画不起作用
Skspritenode texture animation doesn't work
我只是想在我的场景中制作一些硬币的动画。在我的其他项目中,动画有效。不过最近不在了
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)
}
}
试着重构一下你的代码,从文件名中删除 .png
(应该是修复)并在硬币循环外提取纹理数组(优化),所以你的代码可能是:
func addCoins() {
let textures = ["coin1", "coin2", "coin3", "coin4"].flatMap { SKTexture(imageNamed: [=10=]) }
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)
}
}