gpt4 book ai didi

ios - 使用 sprite 工具包的级别菜单的持久性

转载 作者:行者123 更新时间:2023-11-29 01:05:30 25 4
gpt4 key购买 nike

我正在尝试制作一个具有不同级别的菜单,而游戏只有三个开放级别。

问题在于节省铅含量。

我已经设法让关卡在打开时显示各自的编号,如果没有打开则显示挂锁。完成上一关后,将打开一个新关卡。

我希望保存它,这样当您关闭应用程序时,关卡进度不会消失。

我尝试使用 .plist 但他们不允许我使用数字作为 plist 中的变量名称,因此很难获取每个级别的信息。我告诉你。

我的.plist是一个字典矩阵,里面有其他字典,由三个组成部分(完成级别,级别,开闭)组成:

<dict>
<key>Diez5</key>
<dict>
<key>finishEnd</key>
<integer>0</integer>
<key>level</key>
<integer>10</integer>
<key>openClosed</key>
<integer>0</integer>
</dict>
<key>Once5</key>
<dict>
<key>finishEnd</key>
<integer>0</integer>
<key>level</key>
<integer>11</integer>
<key>openClosed</key>
<integer>0</integer>
</dict>
<key>Uno</key>
<dict>
<key>finishEnd</key>
<integer>0</integer>
<key>level</key>
<integer>1</integer>
<key>openClosed</key>
<integer>1</integer>
</dict>
...
...</dict>

接下来我有类(class)使用这个:

 class MenuList {

var matriz4 = [["Ocho","Nueve","Diez","Once"], ["Cuatro","Cinco","Seis","Siete"], ["Matriz","Uno","Dos","Tres"]]

var matriz5 = [["Ocho5","Nueve5","Diez5","Once5"], ["Cuatro5","Cinco5","Seis5","Siete5"], ["Matriz5","Uno5","Dos5","Tres5"]]

let levelKey = "level"
let openClosedKey = "openClosed"
let finishKey = "finishEnd"

let columnNum: Int!
let rowNum: Int!

let plist = Plist(name: "MatrizMenu")

var levelID: Int!
var openCID: Int!
var finishID: Int!

init() {

self.columnNum = matriz4.count + 1
self.rowNum = matriz4.count
}

func getTile(column: Int, row: Int, matrixNum: Int) -> (SKSpriteNode,SKLabelNode) {

let textLabel = SKLabelNode(fontNamed: "HelveticaRoundedLTStd-BdCn")
textLabel.fontColor = UIColor.whiteColor()
textLabel.text = " "

switch(matrixNum){
case 4:

let dict = plist?.getMutablePlistFile()!
if let myDict = dict![matriz4[row][column]] {

openCID = myDict.valueForKey(openClosedKey) as! Int!
finishID = myDict.valueForKey(finishKey) as! Int!

if matriz4[row][column] == "Matriz" {

textLabel.text = "4x4"
return (SKSpriteNode(imageNamed: "rectangle-play"),textLabel)

} else if openCID != 0 && finishID == 0 {
levelID = myDict.valueForKey(levelKey) as! Int!
textLabel.text = "\(levelID)"
return (SKSpriteNode(imageNamed: "UnfinishTile"), textLabel)

} else if openCID != 0 && finishID != 0 {
levelID = myDict.valueForKey(levelKey) as! Int!
textLabel.text = "\(levelID)"
return (SKSpriteNode(imageNamed: "FinishTile"), textLabel)

}else {
textLabel.text = "lock"
return (SKSpriteNode(imageNamed: "rectangle-play-normal"), textLabel)
}
}

case 5:

let dict = plist?.getMutablePlistFile()!
if let myDict = dict![matriz5[row][column]] {

openCID = myDict.valueForKey(openClosedKey) as! Int!

if matriz5[row][column] == "Matriz5" {

let sprite = SKSpriteNode(color: UIColor(red: 1, green: 0.541, blue: 0.298, alpha: 1), size: CGSize(width: 50, height: 50))
textLabel.text = "5x5"
return (sprite,textLabel)

} else if openCID != 0 && finishID == 0 {
levelID = myDict.valueForKey(levelKey) as! Int!
textLabel.text = "\(levelID)"
return (SKSpriteNode(imageNamed: "UnfinishTile"), textLabel)


} else if openCID != 0 && finishID != 0 {
levelID = myDict.valueForKey(levelKey) as! Int!
textLabel.text = "\(levelID)"
return (SKSpriteNode(imageNamed: "FinishTile"), textLabel)

}else {
textLabel.text = "lock"
return (SKSpriteNode(imageNamed: "rectangle-play-normal"), textLabel)
}

}

default:
return (SKSpriteNode(), textLabel)

}

return (SKSpriteNode(), textLabel)
}

func get_level(column: Int, row: Int, matrixNum: Int) -> (Int,Bool) {

switch(matrixNum){
case 4:

let dict = plist?.getMutablePlistFile()!
if let myDict = dict!["\(matriz4[row][column])"] {

openCID = myDict.valueForKey(openClosedKey) as! Int!
levelID = myDict.valueForKey(levelKey) as! Int!

if matriz4[row][column] != "Matriz" && openCID != 0 {
return (levelID,true)
}
}
break

case 5:

let dict = plist?.getMutablePlistFile()!
if let myDict = dict!["\(matriz5[row][column])"] {

openCID = myDict.valueForKey(openClosedKey) as! Int!
levelID = myDict.valueForKey(levelKey) as! Int

if matriz5[row][column] != "Matriz5" && openCID != 0 {
return (levelID,true)
}
}
break

default:
return (0,false)
}
return (0,false)
}

func getBool(column: Int, row: Int) -> Bool {

if matriz4[row][column] == "Matriz" {
return true
} else { return false }
}

}

这是我发现可用的 plist 类 ( Help for plist ):

import CoreData

struct Plist {
//1
enum PlistError: ErrorType {
case FileNotWritten
case FileDoesNotExist
}
//2
let name:String
//3
var sourcePath:String? {
guard

let path = NSBundle.mainBundle().pathForResource(name, ofType: "plist")

else { return .None }
return path
}
//4
var destPath:String? {
guard sourcePath != .None else { return .None }
let dir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
return (dir as NSString).stringByAppendingPathComponent("\(name).plist")
}

init?(name:String) {
//1
self.name = name
//2
let fileManager = NSFileManager.defaultManager()
//3
guard let source = sourcePath else { return nil }
guard let destination = destPath else { return nil }
guard fileManager.fileExistsAtPath(source) else { return nil }
//4
if !fileManager.fileExistsAtPath(destination) {
//5
do {
try fileManager.copyItemAtPath(source, toPath: destination)
} catch let error as NSError {
print("Unable to copy file. ERROR: \(error.localizedDescription)")
return nil
}
}
}

//1
func getValuesInPlistFile() -> NSDictionary?{
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(destPath!) {
guard let dict = NSDictionary(contentsOfFile: destPath!) else { return .None }
return dict
} else {
return .None
}
}
//2
func getMutablePlistFile() -> NSMutableDictionary?{
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(destPath!) {
guard let dict = NSMutableDictionary(contentsOfFile: destPath!) else { return .None }
return dict
} else {
return .None
}
}
//3
func addValuesToPlistFile(dictionary:NSDictionary) throws {
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(destPath!) {
if !dictionary.writeToFile(destPath!, atomically: false) {
print("File not written successfully")
throw PlistError.FileNotWritten
}
} else {
throw PlistError.FileDoesNotExist
}
}

那么,如何保存关卡进度呢?感谢您的帮助!

最佳答案

有几种方法可以做到这一点。我目前正在完成我的游戏,我使用 UICollectionView 有类似的菜单。为了存储数据,我在处理游戏保存的 Singleton 游戏数据类中为此创建了一个字典数组。

例如

var unlockedLevels: [[String: Bool]] = [
// Empty, so array starts at index 1 and not 0
[:],
// World 1
["1": true, "2": false, "3": false, "4": false], // Level 1-4
// World 2
...
]

然后我在我的集​​合 View 中使用 indexPath 来检查哪个单元格被按下并将其与数组进行比较以查看它是否已解锁并加载关卡。

    let world = indexPath.section + 1 // + 1 because indexPath starts at 0
let level = indexPath.row + 1 // If collection view is horizontal need to convert this because indexPath goes up/down not left/right.

// Load level only if unlocked, otherwise exit method
guard GameData.sharedInstance.unlockedLevels[world]["\(level)"] == true else { return }

// code to load level

您不仅要确保根据关卡是否解锁来更改单元格。我在我的集​​合 View 的 CellForRowAtIndexPath 方法中这样做,它有 2 个单元格,1 个正常,1 个锁定。由于单元格出队和重用,我无法让它可靠地使用 1 个单元格。

    let world = indexPath.section + 1
let level = indexPath.row + 1

if GameData.sharedInstance.unlockedLevels[world]["\(level)"] == true {
return cell // returns normal cell to play level
} else {
return cellLocked // returns cell with padlock.
}

完成一个级别后,将相应级别的 bool 设置为 true 并保存进度。

要保存数组,您可以使用 NSCoding、NSUserDefaults 或最佳解决方案 Keychain。

https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding

how to don't get a nil value from NSUserDefaults in ViewDidLoad

https://github.com/jrendel/SwiftKeychainWrapper

https://www.raywenderlich.com/63235/how-to-save-your-game-data-tutorial-part-1-of-2 (Obj C 但我认为还是有用的)

不能 100% 确定这就是您要找的东西,因为我不使用 Plists。让我知道进展如何。

关于ios - 使用 sprite 工具包的级别菜单的持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36430861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com