I want create an array with information of game level objects for my game. But I'm stuck in creating an appropriate array with this information to run this one later in a loop to place all game objects while loading this level.
我想为我的游戏创建一个包含游戏级对象信息的数组。但是我一直在用这些信息创建一个适当的数组,以便稍后在循环中运行这个数组,以便在加载这个级别时放置所有的游戏对象。
This is my array:
这是我的数组:
let level1 =
[
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
]
let level2 =
[
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
),
(
id: 2,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
]
And this is my load level func:
这是我的负载级别func:
func loadLevel(levelNumber: Int = 1) {
var level = [[String:AnyObject]]()
switch(levelNumber) {
case 1:
level = level1 //Cannot assign a value of type '[(id: Int, position: CGPoint, impulse: CGVector)]' to a value of type '[([String: AnyObject])]'
case 2:
level = level2 //Cannot assign a value of type '[(id: Int, position: CGPoint, impulse: CGVector)]' to a value of type '[([String: AnyObject])]'
case 3:
level = level3 //Cannot assign a value of type '[(id: Int, position: CGPoint, impulse: CGVector)]' to a value of type '[([String: AnyObject])]'
default:
level = level1 //Cannot assign a value of type '[(id: Int, position: CGPoint, impulse: CGVector)]' to a value of type '[([String: AnyObject])]'
}
for s in level {
var newShip = Ship(ship: ships[s.id! - 1], position: s.position)
self.addChild(newShip)
ship.physicsBody?.applyImpulse(s.impulse)
}
}
I even do not know if this is a good approach to save/edit level object information.
我甚至不知道这是否是保存/编辑级别对象信息的好方法。
And when I use:
当我使用:
var level = [id:Int, position:CGPoint, impulse:CGVector]() //Use of unresolved identifier 'id'
1 个解决方案
#1
4
You have the wrong type for your level
variable. It is not an array of dictionaries. It is an array of named tuples. Try declaring level
like this:
级别变量的类型是错误的。它不是字典数组。它是一个名为元组的数组。尝试如下声明级别:
var level:[(id: Int, position: CGPoint, impulse: CGVector)]
If instead you put all of your levels in 1 big array, you can access each level with an array index and you won't need a big switch statement. I recommend telling Swift the type of the array when you declare it:
如果你把所有的层放在一个大数组中,你可以用一个数组索引访问每个层,你不需要一个大的switch语句。我建议在声明时告诉Swift数组的类型:
let levels: [[(id: Int, position: CGPoint, impulse: CGVector)]] = [
// level 1
[
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
],
// level 2
[
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
),
(
id: 2,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
]
]
func loadLevel(levelNumber: Int = 1) {
// Subtract 1 since array indices start at 0. This will crash with Array
// index out of range if the level doesn't exist. Take proper precautions.
let level = levels[levelNumber - 1]
for s in level {
var newShip = Ship(ship: ships[s.id - 1], position: s.position)
self.addChild(newShip)
ship.physicsBody?.applyImpulse(s.impulse)
}
}
Or you can make levels
be a dictionary with the level number being the key
:
或者你可以把水平数作为一个字典,把这个数字作为关键字:
let levels: [Int:[(id: Int, position: CGPoint, impulse: CGVector)]] = [
// level 1
1: [
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
],
// level 2
2: [
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
),
(
id: 2,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
]
]
func loadLevel(levelNumber: Int = 1) {
// Unwrap the level information. This will crash if it isn't a valid
// level number. Take proper precautions.
let level = levels[levelNumber]!
for s in level {
var newShip = Ship(ship: ships[s.id - 1], position: s.position)
self.addChild(newShip)
ship.physicsBody?.applyImpulse(s.impulse)
}
}
#1
4
You have the wrong type for your level
variable. It is not an array of dictionaries. It is an array of named tuples. Try declaring level
like this:
级别变量的类型是错误的。它不是字典数组。它是一个名为元组的数组。尝试如下声明级别:
var level:[(id: Int, position: CGPoint, impulse: CGVector)]
If instead you put all of your levels in 1 big array, you can access each level with an array index and you won't need a big switch statement. I recommend telling Swift the type of the array when you declare it:
如果你把所有的层放在一个大数组中,你可以用一个数组索引访问每个层,你不需要一个大的switch语句。我建议在声明时告诉Swift数组的类型:
let levels: [[(id: Int, position: CGPoint, impulse: CGVector)]] = [
// level 1
[
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
],
// level 2
[
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
),
(
id: 2,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
]
]
func loadLevel(levelNumber: Int = 1) {
// Subtract 1 since array indices start at 0. This will crash with Array
// index out of range if the level doesn't exist. Take proper precautions.
let level = levels[levelNumber - 1]
for s in level {
var newShip = Ship(ship: ships[s.id - 1], position: s.position)
self.addChild(newShip)
ship.physicsBody?.applyImpulse(s.impulse)
}
}
Or you can make levels
be a dictionary with the level number being the key
:
或者你可以把水平数作为一个字典,把这个数字作为关键字:
let levels: [Int:[(id: Int, position: CGPoint, impulse: CGVector)]] = [
// level 1
1: [
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
],
// level 2
2: [
(
id: 1,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
),
(
id: 2,
position: CGPointMake(0.5, 0.8),
impulse: CGVectorMake(-40, 0)
)
]
]
func loadLevel(levelNumber: Int = 1) {
// Unwrap the level information. This will crash if it isn't a valid
// level number. Take proper precautions.
let level = levels[levelNumber]!
for s in level {
var newShip = Ship(ship: ships[s.id - 1], position: s.position)
self.addChild(newShip)
ship.physicsBody?.applyImpulse(s.impulse)
}
}