我尝试允许用户使用硬币购买节点,然后在游戏中使用此节点。
我试图设置一些代码来检查节点是否已被触摸/购买过,如果没有,则显示该节点的灰色图像。
此外,如果用户的硬币金额大于整数,则在节点上启用触摸,允许他们购买。
这就是我以前可以用来看看节点是否已经购买/触摸过,如果没有,如果有足够的硬币就可以触摸/购买:
if UserDefaults.standard.bool(forKey: "ship2") == true{
let ship2Texture = SKTexture(imageNamed: "ship2.png")
ship2 = SKSpriteNode(texture: ship2Texture)
ship2.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY)
self.addChild(ship2)
}else{
coinImage1 = SKSpriteNode(texture: coinImageTexture)
coinImage1.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY + 105)
coinImage1.zPosition = 1
self.addChild(coinImage1)
coinLabel1.fontName = "MarkerFelt-Thin"
coinLabel1.fontSize = 25
coinLabel1.fontColor = .black
coinLabel1.text = "20"
coinLabel1.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY + 105)
coinLabel1.zPosition = 1.1
self.addChild(coinLabel1)
let greyship2Texture = SKTexture(imageNamed: "greyship2.png")
greyship2 = SKSpriteNode(texture: greyship2Texture)
greyship2.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY)
self.addChild(greyship2)
}
现在这就是我的触摸开始:
if(atPoint(location) == greyship2) {
if coinScore > 3 {
let oldValue = UserDefaults.standard.integer(forKey: "COINSCORE")
let newValue = oldValue - 20
UserDefaults.standard.set(newValue, forKey: "COINSCORE")
UserDefaults.standard.set(true, forKey: "ship2")
} else {
}
}
if(atPoint(location) == ship2){
if let scene = GameScene2(fileNamed: "GameScene2") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view!.presentScene(scene, transition: SKTransition.doorway(withDuration: 1.2));
}
}
唯一的问题是,当用户有足够的硬币并接触到greyship2时,它不会检查初始代码。我必须重新启动coinScore的应用程序,减少20,并使彩色船实际出现。
我已尝试将第一部分代码放入更新功能但崩溃了。
答案 0 :(得分:1)
看起来好像您在购买后没有更新UI。您正在更改UserDefaults值,因此下次运行时它将显示正确的精灵。但是你需要做的是在购买后更新用户界面,因为根据你的UserDefaults值选择精灵的代码不会再次运行。如果你的第一段代码在一个函数中,那么你可以在购买之后调用该函数。
最好在更改值后调用UserDefaults.standard.synchronize()
,以便立即将其写入磁盘。
试试这个
if(greyship2.contains(location)) {
if coinScore > 3 {
let oldValue = UserDefaults.standard.integer(forKey: "COINSCORE")
let newValue = oldValue - 20
UserDefaults.standard.set(newValue, forKey: "COINSCORE")
UserDefaults.standard.set(true, forKey: "ship2")
UserDefaults.standard.synchronize()
//Now it has been brought update the UI and other necessary things
//If the code is in a function call it
//updateUI()
//else change the sprites
//Remove the grey ship
greyship2.setScale(0)
greyship2.removeFromParent()
//create the new ship and add it to the scene
let ship2Texture = SKTexture(imageNamed: "ship2.png")
ship2 = SKSpriteNode(texture: ship2Texture)
ship2.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY)
ship2.setScale(1)
self.addChild(ship2)
break //finish the touchesbegan call
}
}
if(ship2.contains(location)) {
if let scene = GameScene2(fileNamed: "GameScene2") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view!.presentScene(scene, transition: SKTransition.doorway(withDuration: 1.2));
}
}
希望这有帮助