我已经制作了一个等轴网格作为游戏的世界/关卡,并且希望在网格周围设置边界,以便玩家不能超出它们。由于它是倾斜的,并且我编写的函数会自动放置网格瓦片,所以我不能试错 CGPoints 直到我弄对为止。这是我用于网格的代码:
func newTile(size:CGSize) -> SKShapeNode {
let shape = SKShapeNode()
let path = UIBezierPath()
path.move(to: CGPoint(x:0, y:size.height / 2.0))
path.addLine(to: CGPoint(x:size.width / 2.0, y:0))
path.addLine(to: CGPoint(x:0, y:-size.height / 2.0))
path.addLine(to: CGPoint(x:-size.width / 2.0, y:0))
path.close()
shape.path = path.cgPath
shape.lineWidth = 1.0
shape.fillColor = SKColor.gray
return shape
}
func tilePosition(col:Int, row:Int) -> CGPoint {
let x = (CGFloat(row) * tileWidth / 2.0) + (CGFloat(col) * tileWidth / 2.0)
let y = (CGFloat(col) * tileHeight / 2.0) - (CGFloat(row) * tileHeight / 2.0)
return CGPoint(x: x-2000, y: y-100)
}
let tileHeight:CGFloat = 45.0
let tileWidth:CGFloat = 90.0
let numRows = 40
let numCols = 40
let size = CGSize(width: tileWidth, height: tileHeight)
for row in 1...numRows {
for col in 1...numCols {
let tile = newTile(size: size)
tile.position = tilePosition(col: col, row: row)
self.addChild(tile)
}
}
我该如何设置一个边界来限制最后一个创建的瓷砖?