2017-07-24 275 views
0

我有一个随机生成迷宫的代码,基本上迷宫是由一个网格(数组)组成的,每个网格单元有4个变量(上,下,左,右)所有这些都是指细胞壁。墙的真假是随机选择的。如果墙壁是真的,那么有一面墙放置,但是是假的,那里没有墙。如何从另一个函数访问枚举的状态

但是,所有这些都发生在Maze类和GameScene类中。内的GameScene.swift,我已经创建了一个enum wallSides与状态`上攻,下行,莱夫特赛德,rightSide,noSide”(基本上指的是电池的哪一侧壁上的用户轻敲):

enum wallSides { 
    case upSide, downSide, leftSide, rightSide, noSide 
} 

我使用这些状态在func ConvertLocationPointtoGridPosition它需要一个CGPoint位置并将其转换并返回为x,y整数(以查找它在网格中的哪个单元格)。该功能还使用它发生在找出细胞中的位置点在使用upSide, downSide, leftSide, rightSide, noSide状态的该壁的位置,并返回wallSide

func ConvertLocationPointToGridPosition(location: CGPoint) -> (x: Int, y: Int, side: wallSides) { 
    var xPoint: CGFloat 
    var yPoint: CGFloat 
    xPoint = location.x/CGFloat(tileSize) 
    yPoint = location.y/CGFloat(tileSize) 
    let yPointRemainder = yPoint.truncatingRemainder(dividingBy: 1) 
    let xPointRemainder = xPoint.truncatingRemainder(dividingBy: 1) 
    var wallSide: wallSides 

    if yPointRemainder < 0.2 && xPointRemainder < 0.8 && xPointRemainder > 0.2 { 
     wallSide = .downSide 
    } else if yPointRemainder > 0.8 && xPointRemainder < 0.8 && xPointRemainder > 0.2 { 
     wallSide = .upSide 
    } else if xPointRemainder < 0.2 && yPointRemainder < 0.8 && yPointRemainder > 0.2 { 
     wallSide = .leftSide 
    } else if xPointRemainder > 0.8 && yPointRemainder < 0.8 && yPointRemainder > 0.2 { 
     wallSide = .rightSide 
    } else { 
     wallSide = .noSide 
    }//end of if-then 

    return(Int(xPoint), Int(yPoint), wallSide) 
} 

我所试图做的是func TouchesBegan其中,若所述内该用户点击节点是墙,“如果墙面状态rightSide,再做事”,但我不知道如何从fConvertLocationPointToGridPosition访问枚举的状态和变量:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    //actually user tapping a wall 
    //send a message that the wall has been tapped 
    let touch = touches.first!    // Get the first touch 
    let location = touch.location(in: self) //find the location of the touch in the view 
    let nodeAtPoint = atPoint(location) //find the node at that location 
    if nodeAtPoint.name == "mazeWall" { 
     ConvertLocationPointToGridPosition(location: location) 
     //self.removeChildren(in: [self.atPoint(location)]) 

     //if side == wallSides.downSide { 
      //code 
     //} 
    }//end of nodeAtPoint = mazeWall if-then statement 
} 

请帮我我如何做到这一点。

回答

1

如果我理解正确,则问题是访问多个返回值。试试这个

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    //actually user tapping a wall 
    //send a message that the wall has been tapped 
    let touch = touches.first!    // Get the first touch 
    let location = touch.location(in: self) //find the location of the touch in the view 
    let nodeAtPoint = atPoint(location) //find the node at that location 
    if nodeAtPoint.name == "mazeWall" { 

     let (x, y, side) = ConvertLocationPointToGridPosition(location: location) 
     //self.removeChildren(in: [self.atPoint(location)]) 

     if side == wallSides.downSide { 
     //code 
     } 
    }//end of nodeAtPoint = mazeWall if-then statement 
} 

另一个版本。

let position = ConvertLocationPointToGridPosition(location: location) 
print("side: \(position.2)")