Turn Off Touch for Whole Screen, Spritekit, How

Turn off touch for whole screen, SpriteKit, how?

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.

Spritekit Disable Touch on Certain Area of View

It sounds like you want to ignore touches in the top half of the screen. If so, you could add a check to touchesBegan and touchesMoved like so:

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

let touch = touches.first as UITouch!
let location = touch.locationInNode(self)

guard location.y < self.frame.midY else {
return
}

let sprite = StarNode.star(touch.locationInNode(self))
touchPoint = location
touching = true
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

let touch = touches.first as UITouch!
let location = touch.locationInNode(self)

guard location.y < self.frame.midY else {
// You might want set touching to false here, depends on the rest of your code
return
}

touchPoint = location
}

The new code I added checks that the Y position of the touch is less than half the height of the scene, assuming the origin is at the bottom left. If the check fails, the function exits - you might want to add some code to cancel touch actions there.
If you'd like a certain portion of the screen to be untouchable, you could make a CGRect and check that the touch location is always outside the rect, e.g:

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

let touch = touches.first as UITouch!
let location = touch.locationInNode(self)
let untouchableArea = CGRect(x: 100, y: 100, width: 200, height: 150)

guard !untouchableArea.contains(location) else {
return
}
// Could do something similar in touchesMoved to guard against moving into the untouchable area
}

How to disable second touch in an SpriteKit game?

You want to implement UIPanGestureRecognizer in your scene. It will allow you to track the location of the user's touch and at the same time control other "stray" touches: UIPanGestureRecognizer Documentation

After you initialize it, you need to implement a method to handle the user's pans. You will have to set flags inside of this method to control when the swipe started/ended. I think this answer on StackOverflow gave a really good explanation of using it (with Swift). BTW, when you initialize it, you should set the gesture recognizer's property maximumNumberOfTouches to 1 (that will cause it to ignore other touches while the user is panning).

The trickier part will be to translate the same code you wrote before to gesture recognizer. The difference is that your handler will be called only once for each "swipe" or "pan", while the touches method you are using now is called each time there is a "touch". There are a few ways to proceed at this point, and you could try whatever you like, but I think that this would be the easiest way to go once you have your gesture recognizer set up (spoiler):

  1. make sure the gesture recognizer is an instance variable so you can access it from all methods.

  2. go to the update: method and make an if statement that checks if gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged

  3. use the same algorithm that you had before in this if statement. To check the location that the touch is at use the method: locationInView:. Use self.view as the parameter.

Hope this helped! good luck.

Swift 4 - SpriteKit over SceneKit - prevent touch for underlying camera controls

Turn off allowsCameraControl and implement your own camera controls instead. The allowsCameraControl is handy for testing/debugging purposes and cannot be disabled partly such as for the doubletap behavior only. Which besides conflicts with other gestures gets annoying as a double tap is easily made by accident and resets the screen.

Swift: how to disable user interaction while touch action is being carried out?

Try to get the view from the touch object and then dissable the user interaction on it.

touch.view.isUserInteractionEnabled = false

Only allow touch after a certain time

You can disable user interaction while clicking on the screen and enable it after 10 second as-

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

for touch in touches {

let location = touch.location(in: self)
player.position.x = location.x
player.position.y = -300//location.y

}
self.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.isUserInteractionEnabled = true
}
}

If you want to disable user interaction for 10 seconds after clicking the start button for the first time, then copy this code

self.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.isUserInteractionEnabled = true
}

and paste it inside your Start button action function.



Related Topics



Leave a reply



Submit