代码之家  ›  专栏  ›  技术社区  ›  E. Huckabee

这个ActionScript函数的Swift等价物

  •  -1
  • E. Huckabee  · 技术社区  · 6 年前

    tutorial on BSP 变成斯威夫特。在教程中有一个ActionScript函数。

    public function getRoom():Rectangle
    {
        // iterate all the way through these leafs to find a room, if one exists.
        if (room != null)
            return room;
        else
        {
            var lRoom:Rectangle;
            var rRoom:Rectangle;
            if (leftChild != null)
            {
                lRoom = leftChild.getRoom();
            }
            if (rightChild != null)
            {
                rRoom = rightChild.getRoom();
            }
            if (lRoom == null && rRoom == null)
                return null;
            else if (rRoom == null)
                return lRoom;
            else if (lRoom == null)
                return rRoom;
            else if (FlxG.random() > .5)
                return lRoom;
            else
                return rRoom;
        }
    }
    

    我把这个函数翻译成了Swift(尽我所能),我一定是写错了,因为这个函数返回了一个nil值,而它本不应该返回。

    我的Swift版本:

    // left/right child are initialized as follows:
    // leftChild:Room?
    // rightChild:Room?
    
    public func getRoom() -> Room? {
        if room != nil {
            return room
        } else {
            var lRoom:Room?
            var rRoom:Room?
    
            if leftChild != nil {
                lRoom = leftChild!.getRoom()!
            }
            if rightChild != nil {
                rRoom = rightChild!.getRoom()!
            }
            if lRoom == nil && rRoom == nil {
                return nil
            } else if rRoom == nil {
                return lRoom
            } else if lRoom == nil {
                return rRoom
            } else if  Double.random(in: 0..<1.0) > 0.5 {
                return lRoom
            } else {
                return rRoom
    
            }
        }
    }
    

    在哪里? Room

    class Room {
        var x1:Int
        var x2:Int
        var y1:Int
        var y2:Int
        var center:CGPoint
    
        init(X: Int, Y: Int, W: Int, H: Int) {
            x1 = X
            x2 = X + W
            y1 = Y
            y2 = Y + H
            center = CGPoint(x: (x1 + x2) / 2, y: (y1 + y2) / 2)
        }
    }
    

    我得到了一个零值,而我不应该。我想我把函数翻译错了。 Rectangle 会是 CGRect 但我用我的 房间 房间

    如何用Swift编写这个函数?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Paulw11    6 年前

    你的问题是,你是强制展开的结果 getRoom nil 当遍历到达叶节点时。强制展开 results in a crash

    通过正确使用条件展开,不仅可以使代码更具可读性,还可以消除崩溃。

    public func getRoom() -> Room? {
    
        if let _ = room {
            return room
        }
    
        let lRoom = leftChild?.getRoom()
        let rRoom = rightChild?.getRoom()
    
        switch (lRoom != nil, rRoom != nil) {
        case (false,false):
            return nil
        case (true,false):
            return lRoom
        case (false,true):
            return rRoom
        case (true,true):
            return arc4random_uniform(2) == 1 ? lRoom: rRoom
        }
    }