我只是尝试在我的场景中制作一些硬币。在我的其他项目中,动画有效。但最近不在这里。
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)
}
}
答案 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)
}
}