我怎么称呼func movePlayer
func movePlayer( touches: Set<UITouch>, with event: UIEvent?){
guard currentGameState == gameState.inGame else { return }
for touch: AnyObject in touches{
pointOfTouch = touch.location(in: self)
if pointOfTouch.x > gameArea.maxX - player.size.width/2 {
pointOfTouch.x = gameArea.maxX - player.size.width/2
}
if pointOfTouch.x < gameArea.minX + player.size.width/2 {
pointOfTouch.x = gameArea.minX + player.size.width/2
}
if pointOfTouch.y > gameArea.maxY - player.size.height/2 {
pointOfTouch.y = gameArea.maxY - player.size.height/2
}
if pointOfTouch.y < gameArea.minY + player.size.height/2 {
pointOfTouch.y = gameArea.minY + player.size.height/2
}
}
}
正确地放在
内override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard currentGameState == gameState.preGame else { return }
startGame()
}
我已经试过通常这样称呼:
movePlayer()
但是它并没有真正起作用。
答案 0 :(得分:1)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard currentGameState == gameState.preGame else { return }
startGame()
self.movePlayer(touches: touches, with: event)
}
func movePlayer( touches: Set<UITouch>, with event: UIEvent?){
guard currentGameState == gameState.inGame else { return }
for touch: AnyObject in touches{
pointOfTouch = touch.location(in: self)
if pointOfTouch.x > gameArea.maxX - player.size.width/2 {
pointOfTouch.x = gameArea.maxX - player.size.width/2
}
if pointOfTouch.x < gameArea.minX + player.size.width/2 {
pointOfTouch.x = gameArea.minX + player.size.width/2
}
if pointOfTouch.y > gameArea.maxY - player.size.height/2 {
pointOfTouch.y = gameArea.maxY - player.size.height/2
}
if pointOfTouch.y < gameArea.minY + player.size.height/2 {
pointOfTouch.y = gameArea.minY + player.size.height/2
}
}
}