Вопрос о самодельных прикосновениях функции Swift - PullRequest
0 голосов
/ 02 мая 2019

Как можно позвонить на 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()

но на самом деле это не сработало.

1 Ответ

1 голос
/ 02 мая 2019
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
        }
    }
}
...