Search code examples
sprite-kitswift2skspritenodeskcropnode

Override function with sprite kit game


I am migrating my game from swift 1 and I can't figure out how to fix this function. I am trying to detect if no touches are taking place, which should keep the ball still. this is the code i have and keep getting error Invalid redeclaration of touchesBegan. Im not sure how to work around this, I've been stuck for a while and can't get the ball to stop moving when no touches present. Please help, thanks

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // do nothing if ad is showing
    if Ad.share().showing {
        return
    }

    let touch = touches.first! as UITouch
    let touchedLocation = touch.locationInNode(self)

    // change the speedXFirst of the ball if it starts
    if start {

        // start move
        let moveRight = touchedLocation.x > CGRectGetMidX(self.frame)
        let speedX = moveRight ? ballSpeedX : -ballSpeedX
        ball.speedXFirst = speedX

    }

    // start game if it stops
    else {
        startGame()
    }

}



    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { //error RIGHT HERE

    if start {

        // stop move
        ball.speedXFirst = 0
    }

Solution

  • If you ball must be stopped when the user detaches the finger from the screen, probably you want to use:

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    }
    

    or this if you need to know when the touches were interrupted:

        override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    }