我正在SpriteKit
中构建一个游戏,其中包含一个从7个选项中选择一个角色的玩家。场景1将包括7个图像。每个图像代表一个字符。玩家将通过按下所选图像来选择角色,并将呈现新的场景2。我希望选定的角色图像出现在场景2中。我在场景1中保存了所选角色的数据,但是当场景2出现时,它总是显示角色1而不是另一个。请告知从场景1访问var并将其带到场景2的正确代码行。
import SpriteKit
class Scene1: SKScene {
var Player1 = SKSpriteNode()
var Player2 = SKSpriteNode()
var Player3 = SKSpriteNode()
var Player4 = SKSpriteNode()
var Player5 = SKSpriteNode()
var Player6 = SKSpriteNode()
var Player7 = SKSpriteNode()
var playerSelect1 = false
var playerSelect2 = false
var playerSelect3 = false
var playerSelect4 = false
var playerSelect5 = false
var playerSelect6 = false
var playerSelect7 = false
// in touches began..
if self.nodeAtPoint(location) == self.player1 {
playerSelect1 = true
NSUserDefaults.standardUserDefaults().setBool(true, forKey:"playerSelect1")
// then i present scene2
} else if self.nodeAtPoint(location) == self.player2 {
playerSelect2 = true
NSUserDefaults.standardUserDefaults().setBool(true, forKey:"playerSelect2")
//then i present scene2
} else if self.nodeAtPoint(location) == self.player3 {
playerSelect3 = true
NSUserDefaults.standardUserDefaults().setBool(true, forKey:"playerSelect3")
//then i present scene2
} else if self.nodeAtPoint(location) == self.player4 {
playerSelect4 = true
NSUserDefaults.standardUserDefaults().setBool(true, forKey:"playerSelect4")
//then i present scene2
// i didn't list all 7 characters to make code shorter
class Scene2: SKScene {
var playerSelected: Scene1()
// did move to view
var character1 = SKSpriteNode()
var character2 = SKSpriteNode()
var character3 = SKSpriteNode()
var character4 = SKSpriteNode()
//grab the selection value
let playerSelect1 = NSUserDefaults.standardUserDefaults().boolForKey("playerSelect1")
let playerSelect2 = NSUserDefaults.standardUserDefaults().boolForKey("playerSelect2")
let playerSelect3 = NSUserDefaults.standardUserDefaults().boolForKey("playerSelect3")
let playerSelect4 = NSUserDefaults.standardUserDefaults().boolForKey("playerSelect4")
if playerSelect1 == true {
self.addChild(character1)
} else if playerSelect2 == true {
self.addChild(character2)
} else if playerSelect3 == true {
self.addChild(character3)
} else if playerSelect4 == true {
self.addChild(character4)
}
}
答案 0 :(得分:1)
关于您的问题,您可以通过多种方式来实现您的目标。现在我个人仅在需要与例如今天的扩展进行通信时使用NSUserDefaults
。您可能还想稍后保存您的数据文件,所以我想到了:
class Settings: NSObject { // NSCoding in case you want to save your data
var playerGender: [Bool]! = [Bool]()
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(playerGender, forKey: "playerGender")
}
init(coder aDecoder: NSCoder!) {
playerGender = aDecoder.decodeObjectForKey("playerGender") as! [Bool]
}
override init() {
super.init()
self.playerGender = Array(count:7, repeatedValue:false)
}
}
class GameManager: NSObject {
static let sharedInstance = GameManager()
var settings : Settings! = Settings()
}
class Scene1: SKScene {
let gameManager = GameManager.sharedInstance
var player : [SKSpriteNode]?
override func didMoveToView(view: SKView) {
// in touches began..
if self.nodeAtPoint(location) == self.player1 {
gameManager.settings.playerGender[0] = true
}
}
}
class Scene2: SKScene {
let gameManager = GameManager.sharedInstance
var character : [SKSpriteNode]?
override func didMoveToView(view: SKView) {
//grab the selection value
let playerSelect1 = gameManager.settings.playerGender[0] //it's true
}
}
<强>释强>:
Settings
是一个可以编写游戏设置的类,将来还可以将属性值保存到文件中。
GameManager
是一个共享实例,您可以在任何地方调用它。
答案 1 :(得分:0)
因此,让我们考虑用户第一次选择播放器1.您将所选播放器存储为Bool
中的NSUserDefaults
。此值将保留在本地存储中,直到您更改它为止,即使在应用程序关闭后也是如此。因此,下次用户选择播放器2时,您为Bool
设置playerSelect2
。问题是,在Scene2
中,您开始if
语句检查是否playerSelect1 == true
,这是因为您将其设置为true
一次并且从未将其更改回来。使用您当前的方法,您必须将所需的playerSelect
设置为true
,同时将所有其他设置重置为false
。我建议您更改策略,以便在String
中存储与所选播放器相对应的NSUserDefaults
。这样的事情应该让你开始。
如果要设置所选播放器:
if self.nodeAtPoint(location) == self.player1 {
NSUserDefaults.standardUserDefaults().setObject("playerSelect1", forKey: "selectedPlayer")
else if self.nodeAtPoint(location) == self.player2 {
NSUserDefaults.standardUserDefaults().setObject("playerSelect2", forKey: "selectedPlayer")
else if ...
如果您想确定在Scene2
中选择了哪个玩家:
//grab the value from local storage
let selectedPlayer = NSUserDefaults.standardUserDefaults().objectForKey("selectedPlayer") as! String
if selectedPlayer == "playerSelect1" {
//handle player 1 case
}
else if selectedPlayer == "playerSelect2" {
//handle player 2 case
}