我试图通过触摸按钮来暂停游戏,但我无法让它发挥作用。
我试过了:
pause = SKSpriteNode (imageNamed: "pause.png")
pause.size = CGSizeMake(30, 30)
pause.position = CGPointMake(30, 30)
self.addChild(pause)
我试图调用这样的函数:
func touchesBegin (touches: NSSet!, withEvent event: UIEvent!)
{
for touch: AnyObject in touches
{
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.pause
{
let skView = self.view as SKView
skView.paused = true
}
}
}
当我触摸暂停按钮时没有任何反应...... 什么错了?
谢谢!
答案 0 :(得分:3)
我自己解决了这个问题:
这里我的代码功能:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!)
{
var touch:UITouch = touches.anyObject() as UITouch
pauseText.text = "Pause"
pauseText.fontSize = 50
pauseText.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
/* bouton play/pause */
var locationPause: CGPoint = touch.locationInNode(self)
if self.nodeAtPoint(locationPause) == self.pause
{
addChild(pauseText) // add the text
pause.removeFromParent () // to avoid error when you touch again
self.runAction (SKAction.runBlock(self.pauseGame))
}
}
要恢复游戏,您只需在最后一个“}”之前添加此代码:
if self.nodeAtPoint(locationPause) == self.pauseText
{
pauseText.removeFromParent() // remove the pause text
self.view.paused = false // resume the game
addChild(pause) // add the pause button
}
在SKScene子类中添加此函数以在暂停期间添加标签:
func pauseGame()
{
self.view.paused = true // to pause the game
}