K. Hanoy
K. Hanoy

Reputation: 13

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
    }

Upvotes: 1

Views: 101

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

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?) {
}

Upvotes: 1

Related Questions