試圖編碼snake.head.frame相交snake.body.frame,但我在gameOver()中得到一個錯誤func - 蛇體是一個UIImage不能讓蛇頭相交它的身體gameOver - Swift 3
實際上試圖插入snake.body.bodys []。框架和我沒有得到任何錯誤,但相交snake.head.frame到snake.body.bodys []。幀之後,在它沒有觸發遊戲
func gameOver() {
if snake.head.frame.intersects(snake.body.frame) // ERROR - {
print("game over")
timer.invalidate()
}
}
// Value of type 'SnakeBody' has no member 'frame'
類Snake:
class Snake {
let head = SnakeHead()
let body = SnakeBody()
weak var mainView: UIView!
var direction = Direction.right
init(view: UIView) {
mainView = view
head.nextItem = body.bodys[0]
view.addSubview(head)
view.addSubview(body.bodys[0])
}
}
類SnakeBody:
class SnakeBody {
var startRadius: CGFloat = 20.0
var bodys = [ListBodyItem]()
init() {
bodys.append(conBody(CGRect(x: 60, y: 90, width: 30, height: 30)))
}
func addOne(_ view: UIView) {
let curLast = bodys.last!
bodys.append(conBody(curLast.frame))
curLast.nextItem = bodys.last
view.addSubview(bodys.last!)
}
func conBody(_ frame: CGRect) -> ListBodyItem {
let body = ListBodyItem(frame: frame)
body.setImage(UIImage(named: "snakeBody"), for: UIControlState())
body.clipsToBounds = true
return body
}
}
在此先感謝
的GAMEOVER()方法上面沒有觸發時,蛇頭框架與其身體相交。我在哪裏以及如何稱此方法? – 7rs7
請參閱更新 – Malik