当touchesMoved停止时,弹弓总是返回锚点

时间:2015-04-27 20:08:44

标签: ios swift sprite-kit

所以我有一个非常基本的弹弓,它有两个物理体使用SKPhysicsSpringJoint相互连接。接触开始时,我创造了一个弹丸和弹簧。当移动触摸时,我相应地将射弹移动到用户的触摸位置。当接触结束时,发射弹丸。

问题是当用户停止移动射弹但不放手时,射弹移向其锚点并围绕它摆动直到用户松开它们的触摸。我已经工作了好几个小时试图弄清楚为什么会这样,有什么想法吗?

以下是主要功能:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let touchLocation = touch.locationInNode(self)

        //Add Cannon Ball
        var uniqueID:Int = nodeArray.count
        var uniqueString = "cannonBall\(uniqueID)"




        var cannonBall = CannonBallNode(location: CGPoint(x: size.width/2, y: 200))
        cannonBall.name = uniqueString

        // 3 - Determine offset of location to projectile
        let offset = touchLocation - cannonBall.position

        // 4 - Bail out if you click above cannon
        if (offset.y > 50) { return }

        self.addChild(cannonBall)
        println("\(nodeArray.count)")


        if var cannon = childNodeWithName("cannon") {
            if var heldCannonBall = childNodeWithName(uniqueString) {
                heldCannonBall.physicsBody?.affectedByGravity = false
                var spring = SKPhysicsJointSpring.jointWithBodyA(cannon.physicsBody, bodyB: cannonBall.physicsBody, anchorA: cannon.position, anchorB: cannonBall.position)

                spring.damping = 0.4;
                spring.frequency = 1.0;

                // Add Physics
                physicsWorld.addJoint(spring)
            }
        }
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let touchLocation = touch.locationInNode(self)

        var uniqueID:Int = nodeArray.count
        var uniqueString = "cannonBall\(uniqueID)"

        if var heldCannonBall = childNodeWithName(uniqueString) {
            // 3 - Determine offset of location to projectile
            let offset = touchLocation - heldCannonBall.position

            // 4 - Bail out if you are shooting down or backwards
            if (offset.y > 0) { return }

            heldCannonBall.position = touchLocation
        }
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let touchLocation = touch.locationInNode(self)

        var uniqueID:Int = nodeArray.count
        var uniqueString = "cannonBall\(uniqueID)"

        if var releasedCannonBall = childNodeWithName(uniqueString) {
            var angle = CGFloat(M_PI_4)
            var magnitude:CGFloat = 1;

            releasedCannonBall.physicsBody?.applyImpulse(CGVectorMake(magnitude*cos(angle), magnitude*sin(angle)))
            releasedCannonBall.physicsBody?.affectedByGravity = true
        }




        runAction(SKAction.sequence([
                SKAction.runBlock{[self.physicsWorld.removeAllJoints()]},
                SKAction.waitForDuration(0.1)]
        ))

        nodeArray.append("\(uniqueString)")
    }
}

1 个答案:

答案 0 :(得分:0)

终于遇到了解决方案!我需要产生一个更新功能,以便在通过touchesMoved函数移动时更新射弹的位置。我创建了一个全局变量,并在触摸开始和移动时存储触摸的位置。我把它扔进更新功能,瞧,没有更多的问题。

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    //#1
    var uniqueID:Int = nodeArray.count
    var uniqueString = "cannonBall\(uniqueID)"
    println("\(cannonBallLocation)")
    if var releasedCannonBall = childNodeWithName(uniqueString) {
       releasedCannonBall.position = cannonBallLocation
    }
} //end func