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

理解这些Haxe函数有困难

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

    我一直在学模仿 this article 为了我的比赛。这篇文章是关于程序生成地下城地图。我已经成功地翻译了本文中的大部分Haxe代码。我翻译的大部分代码,主要是猜测和试错。我有一部分不太明白:

    现在我们的目标是把每个房间连接起来,这样我们就可以穿过我们的地牢,最终到达一个通向下一层的出口。我们可以通过在房间之间开辟走廊来实现这一点。

    我们需要在代码中添加一个点变量来跟踪创建的每个房间的中心。无论何时创建和放置房间,我们都会确定其中心并将其连接到上一个房间的中心。

    首先,我们将实施走廊:

    private function hCorridor(x1:Int, x2:Int, y) {
        for (x in Std.int(Math.min(x1, x2))...Std.int(Math.max(x1, x2)) + 1) {
            // destory the tiles to "carve" out corridor
            map[x][y].parent.removeChild(map[x][y]);
    
            // place a new unblocked tile
            map[x][y] = new Tile(Tile.DARK_GROUND, false, false);
    
            // add tile as a new game object
            addChild(map[x][y]);
    
            // set the location of the tile appropriately
            map[x][y].setLoc(x, y);
        }
    }
    
    // create vertical corridor to connect rooms
    private function vCorridor(y1:Int, y2:Int, x) {
        for (y in Std.int(Math.min(y1, y2))...Std.int(Math.max(y1, y2)) + 1) {
            // destroy the tiles to "carve" out corridor
            map[x][y].parent.removeChild(map[x][y]);
    
            // place a new unblocked tile
            map[x][y] = new Tile(Tile.DARK_GROUND, false, false);
    
            // add tile as a new game object
            addChild(map[x][y]);
    
            // set the location of the tile appropriately
            map[x][y].setLoc(x, y);
        }
    }
    

    这些功能的作用方式几乎相同,但一个是水平的,另一个是垂直的。

    Image 1

    直接引自文章。

    我正试图找出如何用Swift编写这些for循环。我理解函数参数和函数应该做什么。然而,我不明白这条线路的swift等价物是什么:

    for (x in Std.int(Math.min(x1, x2))...Std.int(Math.max(x1, x2)) + 1)
    

    以下是我目前的尝试:

    func hCorridor(x1: Int, x2:Int, y: Int) {
        for x in x1...x2 {
            
        }
    }
    func vCorridor(y1: Int, y2:Int, x: Int) {
        for y in y1...y2 {
            
        }
    }
    

    (顺便说一句,它不起作用。)

    3 回复  |  直到 4 年前
        1
  •  2
  •   dfd    6 年前

    根据@JeffWard的答案(意思是我假设他理解Haxe), 快速翻译为:

    let min_x = min(x1, x2)
    let max_x = max(x1, x2)
    for _ in min_x...max_x {
    }
    

    还有几件事:

    • 如果你真的需要 在循环迭代器中,将下划线改为变量。
    • x1 == x2 . (但在后一种情况下,应该只通过循环两次。
    • var , ; ,或C样式循环。

    编辑:

    我立刻想到许多人会说什么 更好或“更快”!:-)

     for _ in min(x1,x2)...(max(x1,x2) {
    
     }
    
        2
  •  3
  •   Jeff Ward    6 年前

    然而,我不明白这条线路的swift等价物是什么:

    for (x in Std.int(Math.min(x1, x2))...Std.int(Math.max(x1, x2)) + 1)
    

    range iteration 在Haxe中:

    for (x in a...b) { }
    

    它相当于以下C样式for循环:

    for (x=a; x<b; x++) { }
    

    a b . 虽然我不能告诉你等价的Swift代码,但我可以告诉你这个循环的伪代码:

    var min_x = smaller_of(x1, x2);
    var max_x = larger_of(x1, x2);
    for (x = min_x; x<max_x + 1; x++) { }
    

    Std.int()

    你可能会发现 Haxe manual 对于你可能有的任何其他问题都很方便。

        3
  •  1
  •   tokiop    6 年前

    引用的haxefor循环从最低的x值迭代到最高的x值。数学.min(x1,x2)将返回最低值,因此它可以根据值从x1迭代到x2,或从x2迭代到x1。

    斯威夫特好像有 such comparison functions too ,您当前的尝试似乎没有使用它或复制此逻辑。