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

时间:2015-02-14 17:51:35

标签: ios xcode swift crash touchesbegan

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

请帮助!!

错误=原因:'尝试添加已有父级的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)

    }

}
//

1 个答案:

答案 0 :(得分:0)

您的问题出在addTargetaddBall方法中。

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

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

addChild(ball)
addChild(target)

并且只在方法中更新位置等,而不是在每次调用方法时再次添加它。