uka*_*szm 5 hittest ios sprite-kit skspritenode
这是我的问题:
我使用SpriteKit,我想在某个矩形中发生忽略触摸事件(阻止调用touchesBegan).我想如何做到这一点的方式是一样的东西"重写hitTestWithEvent的UIView".但我找不到任何类似的方法SKSpriteNode可以忽略事件并阻止调用touchesBegan.
当然
isUserInteractionEnabled为false但它会禁用整个精灵而不仅仅是一部分.touchesBegan方法中的触摸位置,但这是迟到的 - 在同一位置下面的其他精灵将不再接收此事件.SKCropNode,但它只是阻止显示精灵,甚至在不可见区域处理事件.有没有人知道如何防止部分精灵处理事件?
这open func nodes(at p: CGPoint) -> [SKNode]对于检测您已触摸到某个点的节点很有用。之后,您可以排除CGRect所需的每个节点的区域。
import SpriteKit
class GameScene: SKScene {
var warningZone = CGRect(x: -80, y: -60, width: 100, height: 100)
override func didMove(to view: SKView) {
let nodeRed = SKSpriteNode.init(color: .red, size: CGSize(width:300,height:200))
nodeRed.name = "nodeRed"
addChild(nodeRed)
nodeRed.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
let nodeBlue = SKSpriteNode.init(color: .blue, size: CGSize(width:300,height:200))
nodeBlue.name = "nodeBlue"
addChild(nodeBlue)
nodeBlue.position = CGPoint(x:self.frame.midX+100,y:self.frame.midY+20)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let nodesTouched = self.nodes(at: location)
for node in nodesTouched {
guard let n = node.name else { return }
print("Node touched: \(node.name) at point:\(location)")
switch n {
case "nodeRed":
// do your exclusions for the nodeRed
print("\(node.frame)")
if !warningZone.contains(location) {
print("you have touch a safe red zone")
}
case "nodeBlue":
// do your exclusions for the nodeBlue
print("\(node.frame)")
default:
break
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出(我用白色矩形绘制 warningZone 只是为了向您显示它在哪里..):

| 归档时间: |
|
| 查看次数: |
830 次 |
| 最近记录: |