nodeAtPoint错误的节点

时间:2015-07-07 07:26:37

标签: ios swift sprite-kit

我用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

                }    
             }
            }  
        }
    }

1 个答案:

答案 0 :(得分:0)

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

  1. 添加一项检查以查看精灵是否已被移动(如果是,不执行任何操作)
  2. 关闭正在移动的精灵的affectedByGravity属性
  3. 触摸结束时重置touchedNodetouching变量
  4. 这是一个具有上述变化的实现......

    使用以下

    替换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
    }