关于Swift的自制触摸功能的问题

时间:2019-05-02 10:17:07

标签: swift function sprite-kit

我怎么称呼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 个答案:

答案 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
        }
    }
}