Ios 禁用除按钮内部以外的所有触摸功能?-敏捷的
当我在游戏中死亡时,我想忽略用户的所有触摸事件,除非用户点击重置游戏按钮的内部或上。这是我的密码Ios 禁用除按钮内部以外的所有触摸功能?-敏捷的,ios,swift,sprite-kit,touch,skspritenode,Ios,Swift,Sprite Kit,Touch,Skspritenode,当我在游戏中死亡时,我想忽略用户的所有触摸事件,除非用户点击重置游戏按钮的内部或上。这是我的密码 for touch in touches{ let location = touch.locationInNode(self) if died == true{ Ball.physicsBody?.velocity = CGVectorMake(0, 0) if resetGame.containsPoint(lo
for touch in touches{
let location = touch.locationInNode(self)
if died == true{
Ball.physicsBody?.velocity = CGVectorMake(0, 0)
if resetGame.containsPoint(location){
restartScene()
runAction(SKAction.playSoundFileNamed("Woosh.wav", waitForCompletion: false))
}
else {
self.userInteractionEnabled = false
}
这都是我内心的感受。这是我试图忽略用户的触摸,除非触摸的位置在按钮大小之内。除了按钮之外,我怎么能忽略用户在屏幕上任何地方的触摸?resetGame是一个SKSpriteNode图像 您的问题有两种解决方案 我想向大家介绍的第一个案例是基于手势识别器的。 您可以将按钮与其他触摸事件分开,并通过如下布尔变量打开/关闭触摸事件: 手势识别器: 在类的全局var声明部分:
var tapGesture :UITapGestureRecognizer!
var enableTouches:Bool = true
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(GameClass.simpleTap(_:)))
self.tapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(tapGesture)
myBtn.name = "myBtn"
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
return self.enableTouches
}
func simpleTap(sender: UITapGestureRecognizer) {
print("simple tap")
if sender.state == .Ended {
var touchLocation: CGPoint = sender.locationInView(sender.view)
touchLocation = self.convertPointFromView(touchLocation)
let touchedNode = self.nodeAtPoint(touchLocation)
// do your stuff
if touchedNode.name == "myBtn" {
// button pressed, do your stuff
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if died == true {
self.enableTouches = false // disable all touches but leave gestures enabled
//do whatever you want when hero is died
}
}
var enableTouches:Bool = true
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
myBtn.name = "myBtn"
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!enableTouches) {
return
}
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
// do your stuff
if touchedNode.name == "myBtn" {
// button pressed, do your stuff
if died == true {
self.enableTouches = false // disable all touches
//do whatever you want when hero is died
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!enableTouches) {
return
}
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
// do your stuff
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!enableTouches) {
return
}
}
只有一个布尔值 我想提出的第二个解决方案是使用一个简单的布尔值(虽然不是很优雅,但它可以工作),简单地停止触摸流。 此方法在点击按钮时查看,更新方法检查您的英雄是否死亡,以便禁用所有触摸: 在类的全局var声明部分:
var tapGesture :UITapGestureRecognizer!
var enableTouches:Bool = true
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(GameClass.simpleTap(_:)))
self.tapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(tapGesture)
myBtn.name = "myBtn"
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
return self.enableTouches
}
func simpleTap(sender: UITapGestureRecognizer) {
print("simple tap")
if sender.state == .Ended {
var touchLocation: CGPoint = sender.locationInView(sender.view)
touchLocation = self.convertPointFromView(touchLocation)
let touchedNode = self.nodeAtPoint(touchLocation)
// do your stuff
if touchedNode.name == "myBtn" {
// button pressed, do your stuff
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if died == true {
self.enableTouches = false // disable all touches but leave gestures enabled
//do whatever you want when hero is died
}
}
var enableTouches:Bool = true
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
myBtn.name = "myBtn"
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!enableTouches) {
return
}
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
// do your stuff
if touchedNode.name == "myBtn" {
// button pressed, do your stuff
if died == true {
self.enableTouches = false // disable all touches
//do whatever you want when hero is died
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!enableTouches) {
return
}
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
// do your stuff
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (!enableTouches) {
return
}
}
var enableTouchs:Bool=true
覆盖func didMoveToView(视图:SKView){
super.didMoveToView(视图)
myBtn.name=“myBtn”
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果(!EnableTouchs){
返回
}
让我们先接触
让positionInScene=touch!.locationInNode(自)
让touchedNode=self.nodeAtPoint(positionInScene)
//做你的事
如果touchedNode.name==“myBtn”{
//按下按钮,做你的事情
如果死亡==真{
self.enableTouchs=false//禁用所有触摸
//英雄死后,你想做什么就做什么
}
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果(!EnableTouchs){
返回
}
让我们先接触
让positionInScene=touch!.locationInNode(自)
让touchedNode=self.nodeAtPoint(positionInScene)
//做你的事
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
如果(!EnableTouchs){
返回
}
}
第一种情况允许您始终启用手势,以便您可以在英雄死亡时使用手势执行其他操作。第二种情况是,当您按下按钮并执行“模具流动”时,停止触摸。选择最适合您的类别。我们属于哪一类?在我们的GameSecene.swift中,在ToucheSBegind方法中。什么是resetGame变量?也许只是为全班张贴代码?事实上。我有个问题。这个代码有什么问题?你还能点击其他东西吗?或者这段代码到底在做什么?它会忽略屏幕上的所有触摸。甚至按钮内的触摸。resetGame只是一个图像,当按下时,它会重新启动场景。