我正在用swift和spritekit制作游戏,并且我有一个函数,其中有多个精灵从屏幕顶部掉落。我也做到了,这样我就可以使用nodeAtPoint来检测精灵上的触摸,并可以轻拂精灵。我的问题是,由于nodeAtPoint拖动树中最深的节点,因此当我单击并拖动一个精灵时,场景中最新的精灵将被拉向我的触摸,而不是我最初触摸的那个。如果有人对如何仅影响我触摸的节点有任何建议,我将非常感激。

    class GameScene: SKScene {

    var ball = SKSpriteNode(imagedNamed: "blueBlue")

    var touchedNode: SKNode? = SKNode()

     override func didMoveToView(view: SKView) {

       var create = SKAction.runBlock({() in self.createTargets()})
            var wait = SKAction.waitForDuration(2)
            var waitAndCreateForever = SKAction.repeatActionForever(SKAction.sequence([create, wait]))
            self.runAction(waitAndCreateForever)
    }


        func createTargets() {
             ball = SKSpriteNode(imageNamed: "blueBlue")
             let randomx = Int(arc4random_uniform(170) + 290)
             ball.zPosition = 0
            ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 11)

            ball.physicsBody?.dynamic = true
            ball.size = CGSize(width: ball.size.width / 1.5, height: ball.size.height / 1.5)
            let random = Int(arc4random_uniform(35))

            let textures = [texture1, texture2, texture3, texture4, texture5, texture6, texture7, texture8, texture9, texture10, texture11, texture12, texture13, texture14, texture15, texture16, texture17, texture18, texture19, texture20, texture21, texture22, texture23, texture24, texture25, texture1, texture7, texture18, texture24, texture25, texture1, texture7, texture18, texture24, texture25]

            ball.texture = textures[random]
            ball.position = CGPoint(x: randomx, y: 1400)
            addChild(ball)
    }
        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
            let touch = touches.first as! UITouch
            let touchLocation = touch.locationInNode(self)
           touchedNode = self.nodeAtPoint(touchLocation)
            if touchedNode.frame.contains(touchLocation) {
            touching = true
            touchPoint = touchLocation
            }
       }

        override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
            let touch = touches.first as! UITouch
            let touchLocation = touch.locationInNode(self)
            touching = true
            touchPoint = touchLocation

        }
     override func update(currentTime: NSTimeInterval) {

            if touching {

                if touchPoint != touchedNode.position {

                    let dt: CGFloat = 0.1

                    let distance = CGVector(dx: touchPoint.x - touchedNode.position.x, dy: touchPoint.y - touchedNode.position.y)

                    let vel = CGVector(dx: distance.dx / dt, dy: distance.dy / dt)

                    if touchedNode.parent != nil {

                    touchedNode.physicsBody?.velocity = vel

                }
             }
            }
        }
    }

最佳答案

我建议您对代码进行以下更改:


添加检查以查看精灵是否已被移动(如果是,则不执行任何操作)
关闭要移动的精灵的affectedByGravity属性
触摸结束时重置touchedNodetouching变量


这是上述更改的实现...

touchesBegantouchesMoved替换为以下内容

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    if (!touching) {
        let node = self.nodeAtPoint(touchLocation)
        if node is SKSpriteNode {
            touchedNode = node
            touching = true
            touchPoint = touchLocation
            touchedNode!.physicsBody?.affectedByGravity = false
        }
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    if (touching) {
        let touch = touches.first as! UITouch
        let touchLocation = touch.locationInNode(self)
        touchPoint = touchLocation
    }
}


并添加此方法

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    if (touchedNode != nil) {
        touchedNode!.physicsBody!.affectedByGravity = true
    }
    touchedNode = nil
    touching = false
}

10-07 16:18