我正在将二进制空间分区上的this ActionScript tutorial转换为Swift,以便可以在类似流氓的游戏中使用它。我遇到了麻烦。
在文章中,作者按如下方式初始化其类:
public function Leaf(X:int, Y:int, Width:int, Height:int)
{
// initialize our leaf
x = X;
y = Y;
width = Width;
height = Height;
}
当我将其翻译成Swift时,我遇到了一个错误。上面的代码并未初始化其所有声明的值。这导致我陷入无法解决的错误。不知何故,本文的作者使用此函数在初始化范围之外初始化了leftChild
和rightChild
变量。
public function split():Boolean
{
// begin splitting the leaf into two children
if (leftChild != null || rightChild != null)
return false; // we're already split! Abort!
// determine direction of split
// if the width is >25% larger than height, we split vertically
// if the height is >25% larger than the width, we split horizontally
// otherwise we split randomly
var splitH:Boolean = FlxG.random() > 0.5;
if (width > height && width / height >= 1.25)
splitH = false;
else if (height > width && height / width >= 1.25)
splitH = true;
var max:int = (splitH ? height : width) - MIN_LEAF_SIZE; // determine the maximum height or width
if (max <= MIN_LEAF_SIZE)
return false; // the area is too small to split any more...
var split:int = Registry.randomNumber(MIN_LEAF_SIZE, max); // determine where we're going to split
// create our left and right children based on the direction of the split
if (splitH)
{
leftChild = new Leaf(x, y, width, split);
rightChild = new Leaf(x, y + split, width, height - split);
}
else
{
leftChild = new Leaf(x, y, split, height);
rightChild = new Leaf(x + split, y, width - split, height);
}
return true; // split successful!
}
在ActionScript中可以使用哪种方式,但是在Swift中,这会导致我遇到问题。
这是我的翻译代码(Swift):
private let mapWidth:Int = 50
private let mapHeight:Int = 50
class Leaf {
var leftLeaf = [Leaf]()
var rightLeaf = [Leaf]()
var minLeafSize:Int = 6
var x, y, width, height: Int
var leftChild:Leaf
var rightChild:Leaf
init (X:Int, Y:Int, W:Int, H:Int) {
x = Y
y = Y
width = W
height = H
let maxLeafSize:UInt = 20
var leaves = [Leaf]()
// first, create a Leaf to be the 'root' of all Leafs.
let root = Leaf(X: 0, Y: 0, W: mapWidth, H: mapHeight)
leaves.append(root)
var didSplit:Bool = true
// we loop through every Leaf in our Vector over and over again, until no more Leafs can be split.
while (didSplit) {
didSplit = false
for l in leaves {
if l.leftLeaf.isEmpty == true && l.rightLeaf.isEmpty == true {
// if this Leaf is too big, or 75% chance...
if l.width > maxLeafSize || l.height > maxLeafSize || Int(arc4random_uniform(100)) > 25 {
if (l.split()) {
// if we did split, push the child leafs to the Vector so we can loop into them next
leaves.append(l.leftChild)
leaves.append(l.rightChild)
didSplit = true
}
}
}
}
}
}
func split() -> Bool {
if leftLeaf.isEmpty == true || rightLeaf.isEmpty == true {
return false
}
var splitH = arc4random_uniform(100) > 50 ? true : false
if width > height && Double(width / height) >= 1.25 {
splitH = false
}
if height > width && Double(height / width) >= 1.25 {
splitH = true
}
let max:Int = (splitH ? height : width) - minLeafSize // determine the maximum height or width
if max <= minLeafSize { return false }
let split:Int = Int(arc4random_uniform(UInt32(minLeafSize - max) + UInt32(max)))
if (splitH) {
leftChild = Leaf(X: x, Y: y, W: width, H: split)
rightChild = Leaf(X: x, Y: y + split, W: width, H: height - split)
leftLeaf.append(leftChild)
rightLeaf.append(rightChild)
} else {
leftChild = Leaf(X: x, Y: y, W: split, H: height)
rightChild = Leaf(X: x + split, Y: y, W: width - split, H: height);
leftLeaf.append(leftChild)
rightLeaf.append(rightChild)
}
return true
}
}
(据我所知)它与本文中的ActionScript代码相同。但这给我一个错误。 leftChild
和rightChild
变量未在我的init
方法中初始化。当我将split() -> Bool
函数移到init
方法中时,它不会让我使用该函数,给我一个错误“ Leaf类型的值没有成员split()”。从l
行中删除if (l.spit())
给了我第二个错误“声明前使用局部变量'split'”。 split()
函数必须在初始化范围之外。
如果我尝试像这样初始化leftChild
和rightChild
:
init (X:Int, Y:Int, W:Int, H:Int) {
x = Y
y = Y
width = W
height = H
leftChild = Leaf(X: x, Y: y, W: width, H: height)
rightChild = Leaf(X: x, Y: y, W: width, H: height)
}
它创建一个无限循环,最终导致崩溃。
代码应该在leftChild
函数中初始化rightChild
和split() -> Bool
,但是我不认为这就是Swift中的工作方式。您应该能够将其复制/粘贴到Swift文件中,并得到相同的错误。
为什么会这样?我的代码写得不好吗?我该如何解决?
答案 0 :(得分:2)
在ActionScript中,将使用特殊值undefined
自动评估未初始化的变量;此外,在ActionScript中,undefined == null
也是如此,这就是if (leftChild != null || rightChild != null)
起作用的原因。
在Swift中,您需要显式allow变量为零。您担心的变量需要以nil
开始(如果允许,它们会自动将它们的类型设置为Optional
-注意问号):
var leftChild:Leaf?
var rightChild:Leaf?