正如标题所说,我的 SKTexture 并没有像我期望的那样改变。
这就是我在玩家类中声明单例的方式:
static let sharedInstance = NHPlayer()
我有这个功能:
func setSkin(name: String) {
print("Before : \(body.texture)") // Print default SKTexture
body.texture = atlas.textureNamed("\(name)-body")
print("After : \(body.texture)") // print the SKTexture I choose in game
}
我这样称呼 body :
func loadAppearance() {
let name = getCurrentSkin()
print(name)
body = SKSpriteNode(texture: atlas.textureNamed("\(name)-body"))
addChild(body)
}
我使用这个函数得到了我想要的纹理的名称:
func getCurrentSkin() -> String {
let defaults = NSUserDefaults.standardUserDefaults()
var name = defaults.valueForKey("currentSkin")
if name == nil {
name = "default"
}
return "\(name!)"
}
在这里调用setSkin:
func play() {
let defaults = NSUserDefaults.standardUserDefaults()
let current = charactersList.filter{ $0.name == nameLabel.text?.lowercaseString }
removeFromParent()
defaults.setObject(current.first?.name, forKey: "currentSkin")
NHPlayer.sharedInstance.setSkin((current.first?.name)!)
GameScene.sharedInstance.restart()
}
它打印了正确的纹理名称,但纹理未加载,我必须关闭游戏并重新打开它才能让 SKTexture 发生变化。
关于如何修复的任何想法?
如果您需要更多信息,请随时询问。
最佳答案
您确定您没有在播放器类以外的其他地方调用 NHPlayer() 吗?
看起来当您分配新纹理时,它确实在分配,但分配给另一个实例而不是单例
关于xcode - SKTexture 没有正确改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755530/