gpt4 book ai didi

ios - 如何在 SpriteKit 和 SceneKit 中暂停游戏 - SWIFT

转载 作者:可可西里 更新时间:2023-11-01 01:06:45 25 4
gpt4 key购买 nike

我想通过触摸按钮来暂停我的游戏,但我无法让它工作。

我试过这个:

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
}
}

}

当我触摸暂停按钮时,没有任何反应......怎么了?

谢谢!

最佳答案

我自己解决了这个问题:

这里是我的代码函数:

  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
}

关于ios - 如何在 SpriteKit 和 SceneKit 中暂停游戏 - SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25823116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com