Swift touchesBegan 导致 Xcode 崩溃,同时仍在 touchesEnded 中执行功能

Swift touchesBegan causes Xcode crash while still performing function in touchesEnded

我是编程新手,所以我确信它是一个简单的修复程序。但是,我拥有它,因此当我触摸特定的精灵节点时,它会添加一个球和目标。一旦我松开手指,球就会飞向目标。但是,如果我在第一个球完成之前尝试发送另一个球,Xcode 会崩溃。

请帮忙!!

ERROR = 原因:'试图添加一个已经有父节点的 SKNode:

代码:

//
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

            let touch = touches.anyObject() as UITouch
            let touchLocation = touch.locationInNode(self)

            if self.nodeAtPoint(touchLocation) == self.slingshot {

            addBall()
            addTarget()

            isFingerTouching = true

            }
    }




 override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        /* Update to new touch location */

    if isFingerTouching {

        var touch = touches.anyObject() as UITouch!
        var touchLocation = touch.locationInNode(self)
        var previousLocation = touch.previousLocationInNode(self)


                var slingholderY = slingholder.position.y + (touchLocation.y - previousLocation.y)
                var slingholderX = slingholder.position.x + (touchLocation.x - previousLocation.x)

        slingholderY = min(slingholderY, 155)
        slingholderY = max(slingholderY, 89)

        slingholderX = min(slingholderX, CGRectGetMidX(self.frame) + 40)
        slingholderX = max(slingholderX, CGRectGetMidX(self.frame) - 40)

            slingholder.position = CGPointMake(slingholderX, slingholderY)

        leftsling.yScale = (155 / slingholder.position.y)*0.6
        leftsling.xScale = (slingholder.position.x / 383)*0.6

        rightsling.yScale = (155 / slingholder.position.y)*0.6
        rightsling.xScale = (383 / slingholder.position.x)*0.6

        slingholder.yScale = (89 / slingholder.position.y)
        slingholder.xScale = (89 / slingholder.position.y)

        ball.yScale = 2*(89 / slingholder.position.y)
        ball.xScale = 2*(89 / slingholder.position.y)
        ball.position = slingholder.position

        slingholder.zRotation = 0.3*((slingholder.position.x - 383)/40)

        target.position.x = 768 - (9.57 * (slingholder.position.x - 343))
        target.position.y = max(10, 300)
        target.position.y = (self.frame.size.height - self.scoreboard.size.height - 300) + (9.85 * (89 - slingholder.position.y) + 150)

            }
    }


    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

      sendBall()
      isFingerTouching = false

    }

    //Check if bird reaches right or left side
    func checkIfBirdReachesOtherSide () {
        for child in self.children {
            if(child.position.x > self.frame.width+20) {
                self.removeChildrenInArray([child])
            }
            if(child.position.x < -20) {
                self.removeChildrenInArray([child])
            }
        }

}

    func addBall () {

                ball.position = CGPointMake(CGRectGetMidX(self.frame), 155)
                ball.xScale = 1.0
                ball.yScale = 1.0
                ball.zPosition = 4
                self.addChild(ball)

            }

    func addTarget ()  {


        target.position = CGPoint(x: CGRectGetMidX(self.frame), y: slingholder.position.y + 200)
        target.zPosition = 4
        self.addChild(target)
    }

   func sendBall () {

        let moveToTarget = SKAction.moveTo(CGPointMake(target.position.x, target.position.y), duration: 0.5)
        let scaleBallUp = SKAction.scaleBy(1.0, duration: 0.2)
        let scaleBallDown = SKAction.scaleBy(0.3, duration: 0.3)

        let delay = SKAction.waitForDuration(2)

        let remove = SKAction.removeFromParent()

        let sequence = SKAction.sequence([scaleBallUp, scaleBallDown, remove])

        let ballGroupAction = SKAction.group([moveToTarget, sequence])
        ball.runAction(ballGroupAction)

        target.runAction(remove)

        let moveSlingholder = SKAction.moveTo(CGPointMake(CGRectGetMidX(self.frame), 155), duration:1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

        let scaleSling = SKAction.scaleXTo(0.6, y: 0.6, duration: 1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

        let scaleSlingHolder = SKAction.scaleXTo(0.6, y: 0.6, duration: 1, delay: 0.2, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

        let rotateSlingholder = SKAction.rotateToAngle(0, duration: 1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

        let slingHolderBundledMovement = SKAction.group([moveSlingholder, rotateSlingholder, scaleSlingHolder])


        slingholder.runAction(slingHolderBundledMovement)
        leftsling.runAction(scaleSling)
        rightsling.runAction(scaleSling)

    }

}
//

您的问题出在 addTargetaddBall 方法中。

您只能将一个 SKNode 添加到一个父节点。每次触摸屏幕时,您都会在其中添加相同的目标和相同的球。所以你应该只添加一次球和目标并且每次都更新位置。

因此您可以在 didMoveToView 方法中添加两个节点:

addChild(ball)
addChild(target)

并且只更新方法中的位置等,而无需在每次调用方法时再次添加。