Turn off touch for whole screen, SpriteKit, how?

故事扮演 提交于 2019-12-08 13:09:10

问题


I'm trying to temporarily disable touch on the entire screen, despite their being many sprites with touchesBegun onscreen.

I thought, obviously wrongly, turning off touch for the scene would do it:

    scene?.isUserInteractionEnabled = false

But that didn't work, so I tried this, which also didn't work:

    view?.scene?.isUserInteractionEnabled = false

That also didn't work, so I tried this, also from inside the scene:

    self.isUserInteractionEnabled = false

回答1:


There is no global method to turn off the touch, whatever is at the top of the drawing queue is the first responder.

You need to iterate through all of your nodes from your scene and turn them off:

enumerateChildNodesWithName("//*", usingBlock: 
    { (node, stop) -> Void in  
       node.isUserInteractionEnabled = false
    })

Now the problem is turning them back on, if you use this method, you will turn it on for everything, so you may want to adopt a naming convention for all your touchable sprites

enumerateChildNodesWithName("//touchable", usingBlock: 
    { (node, stop) -> Void in  
       node.isUserInteractionEnabled = true
    })

This will look for any node that has a name that begins with touchable.

This method involves recursion, so if you have a ton of nodes, it can be slow. Instead you should use an alternative method:

let disableTouchNode = SKSpriteNode(color:SKColor(red:0.0,green:0.0,blue:0.0,alpha:0.1),size:self.size)
disableTouchNode.isUserinteractionEnabled = true
disableTouchNode.zPosition = 99999
self.addChild(disableTouchNode)

What this does is slap on an almost transparent node on top of all elements the size of the scene. This way when a user touches the screen, this node will absorb it instead of anything else.




回答2:


The following will disable all touches

self.view?.isUserInteractionEnabled = false


来源:https://stackoverflow.com/questions/40639820/turn-off-touch-for-whole-screen-spritekit-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!