Swift 如何访问枚举';来自另一个函数的状态
我有一个随机生成迷宫的代码,基本上迷宫是由一个网格(数组)组成的,每个网格单元有4个bool类型的变量(上、下、左、右),它们都指向细胞壁。墙壁的真假是随机选择的。如果一堵墙是真的,那么就有一堵墙被放置,然而如果一堵墙是假的,那么那里就没有墙 然而,所有这些都发生在Swift 如何访问枚举';来自另一个函数的状态,swift,function,enums,state,Swift,Function,Enums,State,我有一个随机生成迷宫的代码,基本上迷宫是由一个网格(数组)组成的,每个网格单元有4个bool类型的变量(上、下、左、右),它们都指向细胞壁。墙壁的真假是随机选择的。如果一堵墙是真的,那么就有一堵墙被放置,然而如果一堵墙是假的,那么那里就没有墙 然而,所有这些都发生在迷宫类和游戏场景类中。在gamesence.swift中,我创建了一个enum wallSides,状态为“上、下、左侧、右侧、诺西德”(基本上指用户点击单元格的哪一面墙): 我在func-ConvertLocationPointto
迷宫
类和游戏场景
类中。在gamesence.swift
中,我创建了一个enum wallSides
,状态为“上、下、左侧、右侧、诺西德”(基本上指用户点击单元格的哪一面墙):
我在func-ConvertLocationPointtoGridPosition
中使用这些状态,它接受CGPoint
位置,并将其转换为x,y整数返回(以查找它在网格中的单元格)。该函数还使用它接收到的位置,通过上部、下部、左侧、右侧、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)
}
请帮助我如何做到这一点。如果我理解正确,问题是访问多个返回值。试试这个
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
}
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
}
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)")