swift - Sprite 子类检测触摸,尽管实际上触摸了空白空间

标签 swift sprite-kit

我正在对节点进行子类化以用于触摸检测。我有一个盒子 parent ,它旁边有一个线子,灰色空间只是空白:

enter image description here

问题是当我点击灰色区域时,它会记录为对距离很远的框的触摸。


这是我展示问题的代码,以及我糟糕的解决方法……我做了两组盒子,第一组是显示问题的,第二组是解决方法的:

import SpriteKit

class GameScene: SKScene {
  
  enum sizez {
    static  let
    box  = CGSize(width: 50, height: 35),
    line = CGSize(width: 200, height: 10)
  }
  
  class Box: SKSpriteNode {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      print("touched box")
    }
  }
  class Line: SKSpriteNode {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      print("touched line")
    }
  }
  
  override func didMove(to view: SKView) {
            
    // The problem sprites that register touch despite empty space:
    let box = Box(color: .red, size: sizez.box)
    box.isUserInteractionEnabled = true
    addChild(box)
    
    let line =  Line(color: .purple, size: sizez.line)
    line.isUserInteractionEnabled = true
    line.anchorPoint = CGPoint.zero
    line.position = CGPoint(x: box.frame.maxX + 10, y: box.frame.minY)
    ///////////////////
    box.addChild(line)
    ///////////////////
    
    
    // These sprites detect touches properly (no detection in empty space)
    let box2 = Box(color: .red, size: sizez.box)
    box2.isUserInteractionEnabled = true
    box2.position.y -= 100
    addChild(box2)
    
    let line2 =  Line(color: .purple, size: sizez.line)
    line2.isUserInteractionEnabled = true
    line2.anchorPoint = CGPoint.zero
    line2.position = CGPoint(x: box2.frame.maxX + 10, y: box2.frame.minY)
    ////////////////
    addChild(line2)
    ////////////////
  }
}

当您直接点击顶行上方(或更远的地方)时,您会看到:

enter image description here

当你对底线做同样的事情时,你会得到这个:

enter image description here

放弃 SK 的内置父/子系统,然后让我自己手动跟踪它们将是一个巨大的麻烦……而且这对我的应用程序来说是一个很大的性能打击。

任何合理的解决方法或解决方案都可以让我在使用类似于第一个框的代码时单击灰色区域,我们将不胜感激。

更新:

通过制作一个不可见的背景节点并将其 zPositon 设置得更少,我现在可以在灰色区域中单击并将它注册为背景节点,而不是框。

let  bkgSize  = CGSize(width: 1000, height: 1000)

let bkg = Bkg(color: .gray, size: bkgSize)
bkg.isUserInteractionEnabled = true
bkg.zPosition -= 1
addChild(bkg)

但是,为什么在没有背景节点的情况下,这个空白空间触摸被注册为框触摸?

最佳答案

你是绝对正确的背景节点它按预期工作,没有背景 Sprite 它给出@Confused描述的结果。通过像这样微调 TouchesBegan 函数,我能够在没有背景的情况下按预期工作......

class Box: SKSpriteNode {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first as UITouch! {

            let touchLocation = touch.location(in: parent!)

            if frame.contains(touchLocation) {
                print("touched box")
            }
        }
    }
}

class Line: SKSpriteNode {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch! {

            let touchLocation = touch.location(in: parent!)

            if frame.contains(touchLocation) {
                print("touched line")
            }
        }
    }
}

关于swift - Sprite 子类检测触摸,尽管实际上触摸了空白空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42532624/

相关文章:

swift - Xcode SpriteKit SKAction 未更改 SKSpriteNode 的属性?

swift - 无法在 tvOS 中获得透明的标签栏

ios - 如何配置 podsec 文件 swift 项目

ios - 在关闭中找不到成员

ios - 需要帮助,Swift 2.0 SKSpriteNode

swift - SCNScene : Calculate projected size of an object

ios - 添加一个 SKSpriteNode 到 UIView

swift - 如何在 Swift 中使用 openURL 打电话?

Swift 4 从后退按钮中删除文本

sprite-kit - SKRenderer——神秘类