ios - nodeAtPoint 错误节点

标签 ios swift sprite-kit

我正在使用 swift 和 spritekit 制作一个游戏,并且我有一个功能,可以让多个 Sprite 从屏幕顶部掉落。我还做到了这一点,以便我能够使用 nodeAtPoint 检测 Sprite 上的触摸,并使得轻弹 Sprite 成为可能。我的问题是,由于 nodeAtPoint 拖动树中最深的节点,当我单击并拖动 Sprite 时,场景中最新的 Sprite 会被拉向我的触摸位置,而不是我最初触摸的位置。如果有人对如何仅影响我接触的节点有一些建议,我将非常感激。

    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. 添加检查以查看 Sprite 是否已被移动(如果是,则不执行任何操作)
  2. 关闭正在移动的 Sprite 的 affectedByGravity 属性
  3. 触摸结束时重置 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
}

关于ios - nodeAtPoint 错误节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262315/

相关文章:

ios - 有没有办法在发生某些事情后停止 touches 方法?

ios - 固定位置在 contentEditable ios webkit safari 上损坏

iOS 4.3 Simulator Localization.strings 以法语工作,但不是英语

ios - 选择某些共享类型后如何获取UIActivityType

ios - 如何将日期合并到字典中?

ios - swift 2.2 和 xcode 7.3 中的模拟和文档

ios - 同时使用 AudioKit 和 SpriteKit 音频

objective-c - exportAsynchronouslyWithCompletionHandler : is not working

iPhone iOS 如何通过长按将图像粘贴到 UIImageView 中

ios - 对 SKShapeNode 施加脉冲