ON / OFF按钮多次切换(ON / OFF Button toggles multiple times)

编程入门 行业动态 更新时间:2024-10-22 16:46:47
ON / OFF按钮多次切换(ON / OFF Button toggles multiple times)

我试图为静音/取消静音声音按钮创建一个ON / OFF按钮:

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { for touch: AnyObject in touches { let location = touch.locationInNode(self) let resizeAction = SKAction.scaleTo(1, duration: 0.05) let fadeAnimation = SKTransition.fadeWithColor(SKColor.whiteColor(), duration: 0.4) if self.nodeAtPoint(location) == soundOnButton { soundOnButton.removeFromParent() self.addChild(soundOffButton) println("test 1") } if self.nodeAtPoint(location) == soundOffButton { soundOffButton.removeFromParent() self.addChild(soundOnButton) println("test 2") } } }

但没有任何反应,因为当我触摸按钮时,它被移除并且添加了OFF按钮,但是应用程序检测到我触摸了OFF按钮,因此它将其移除并添加ON按钮,就像无限循环一样!

有人有解决方案吗?

谢谢 !

I trie to create an ON / OFF button for the mute / unmute sound button :

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { for touch: AnyObject in touches { let location = touch.locationInNode(self) let resizeAction = SKAction.scaleTo(1, duration: 0.05) let fadeAnimation = SKTransition.fadeWithColor(SKColor.whiteColor(), duration: 0.4) if self.nodeAtPoint(location) == soundOnButton { soundOnButton.removeFromParent() self.addChild(soundOffButton) println("test 1") } if self.nodeAtPoint(location) == soundOffButton { soundOffButton.removeFromParent() self.addChild(soundOnButton) println("test 2") } } }

But nothing happened, because when I touch the button, it is removed and the OFF button is added, but the application detect that I touch the OFF button, so it remove it and add the ON button, like an infinite loop !

Someone has a solution ?

Thank you !

最满意答案

else if在你的if条件下使用else if 。 否则两个条件都将被执行。 这就是您的代码失败的原因。 执行第一个条件时,放置soundOffButton而不是soundOnButton 。 当执行到达第二个条件时,该点上的节点是soundOffButton ,因此按钮也会互换。 else if确保一次只执行一个条件,则使用else if 。

if self.nodeAtPoint(location) === soundOnButton { soundOnButton.removeFromParent() self.addChild(soundOffButton) println("test 1") } else if self.nodeAtPoint(location) === soundOffButton { // Changed line soundOffButton.removeFromParent() self.addChild(soundOnButton) println("test 2") }

use else if in your if conditions. Otherwise both conditions will be executed. This is why your code fails. When the first condition is executed you place the soundOffButton instead of soundOnButton. When the executing reaches the second condition, the node at the point is soundOffButton, so again the buttons are interchanged. Using else ifensure that only one condition is executed at a time.

if self.nodeAtPoint(location) === soundOnButton { soundOnButton.removeFromParent() self.addChild(soundOffButton) println("test 1") } else if self.nodeAtPoint(location) === soundOffButton { // Changed line soundOffButton.removeFromParent() self.addChild(soundOnButton) println("test 2") }

更多推荐

本文发布于:2023-04-27 23:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   Button   toggles   multiple   times

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!