我需要再次打电话给viewDidLoadBy()
以在我的游戏中创建一个新形状。但我无法掌握如何做到这一点。在继续之前是否需要删除当前实例?这是我主要的TetrisViewController中的内容
其中包含TetrisModel和TetrisView文件。我创建了一个实例,但现在我想创建另一个实例,任何建议或说明都会很棒。
以下是我在TetrisViewControler.swift中所做的事情,它在我的游戏中创建了一个形状。
override func viewDidLoad() {
super.viewDidLoad()
//let centerX = Int(UIScreen.main.bounds.size.width) / blockSize * blockSize / 2
let randoNumber = randomShapeGenerator()
if randoNumber == 0{
let grid = QTetrisGrid()
block = TetrisBlockView(color: UIColor.orange, grid: grid, blockSize: blockSize,
startY: 120.0, boardCenterX: CGFloat(135.0))
}
if randoNumber == 1{
let grid = ZTetrisGrid()
block = TetrisBlockView(color: UIColor.orange, grid: grid, blockSize: blockSize,
startY: 120.0, boardCenterX: CGFloat(135.0))
}
if randoNumber == 3{
let grid = JTetrisGrid()
block = TetrisBlockView(color: UIColor.orange, grid: grid, blockSize: blockSize,
startY: 120.0, boardCenterX: CGFloat(135.0))
}
if randoNumber == 4{
let grid = ITetrisGrid()
block = TetrisBlockView(color: UIColor.orange, grid: grid, blockSize: blockSize,
startY: 120.0, boardCenterX: CGFloat(135.0))
}
if randoNumber == 5{
let grid = STetrisGrid()
block = TetrisBlockView(color: UIColor.orange, grid: grid, blockSize: blockSize,
startY: 120.0, boardCenterX: CGFloat(135.0))
}
tetrisBoard = TetrisBoardView(withFrame: UIScreen.main.bounds, blockSize: blockSize, circleRadius: 1 )
view.addSubview(tetrisBoard)
print("Center of block before animation: \(block.center)")
print("Bounds of main screen is \(UIScreen.main.bounds)")
view.addSubview(block)
答案 0 :(得分:1)
适用于viewDidLoad的唯一代码是用于设置视图的初始状态,在本例中是游戏板本身。绘图形状应该出现在主循环中,这在游戏编程中很常见。
但是,要回答您的具体问题,您需要将代码分解为单独的函数。伪代码......
func addRandomShapeToBoard() {
// insert the code you have above
}
然后你可以从其他函数调用它,而不只是在viewDidLoad
里面