如何防止在暂停场景(取消暂停后)上运行 SKAction,暂停/取消暂停场景后节点纹理不改变

2024-02-12

我在暂停和取消暂停场景时遇到两个问题。我有按钮:

    playPause = SKSpriteNode(imageNamed: "buttPause.png")
    playPause.name = "pause"
    playPause.setScale (0.65)
    playPause.position = CGPoint(x: -self.frame.width / 2.55 , y: self.frame.height / 2.27)
    playPause.zPosition = 10

    self.addChild(playPause)

我已经设置了暂停和取消暂停场景+更改按钮纹理的功能:

func buttonpauseplayTouched() {

    if isPlaying {
        playPause.texture = SKTexture(imageNamed: "buttPlay")
        isPlaying = false
        self.scene?.view?.isPaused = true
    }

    else {
        playPause.texture = SKTexture(imageNamed: "buttPause")
        isPlaying = true
        self.scene?.view?.isPaused = false
    }

}

我已经设置了触摸按钮:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        location = touch.location(in: self)
        node = self.atPoint(location)
        if node.name == "pause" {

            buttonpauseplayTouched()



        }
        else {print ("Blank space")}
    }

}

现在,当我触摸暂停按钮时,我可以暂停和取消暂停场景,但纹理不会改变?怎么了?或者,如果我想在暂停时将其他 spritekit 节点添加到场景中,我不能。

第二个问题是,我在场景中还有一些其他 SKSpriteNode,并且当我触摸它们时我已经设置了操作。如果我在场景暂停时触摸它们,则不会发生任何事情,但是当我取消暂停场景时,对象上的操作就会运行。如何防止场景暂停时无法对对象执行操作。我尝试:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
    location = touch.location(in: self)
    node = self.atPoint(location)
    if node.name == "pause" {

        buttonpauseplayTouched()



    }
    else if scene?.view?.isPaused == true && node.name == "object" {print ("Nothing!!")}
    }
    else {print ("Blank space")}
}

}

没有成功。感谢您的任何提示。

UPDATE:

再次感谢两位! 带图层的解决方案更好,我尝试了一下,效果很好!一个小问题是移动背景:

override func didMove(to view: SKView) {
   createBackground()
  }
 func createBackground(){
    for i in 0...3 {
        background = SKSpriteNode(imageNamed: "background1.png")
        background.name = "background"
        background.setScale(0.694)
        background.position = CGPoint(x: 0, y: CGFloat(i) * background.size.height)
        background.zPosition = 1


        gameLayer.addChild(background)
    }
}
func moveBackground(){

    gameLayer.enumerateChildNodes(withName: "background", using: ({
        (node, error) in

        node.position.y -= 7

        if node.position.y < -((self.background.size.height)) {
            node.position.y += (self.background.size.height) * 3

        }

    }))

}
override func update(_ currentTime: TimeInterval) {
    moveBackground()
}

当我暂停游戏层时,背景仍在移动。如何编辑代码以在游戏层暂停时停止移动背景?我希望只需要稍微改变一下代码就可以解决这个问题。谢谢!


我将控件放在自己的层(SKNode)controlsLayer.addChild(pauseButton)上,将游戏对象放在自己的层“gameLayer”上,然后当我想暂停游戏时,我只需暂停gameLayer(gameLayer.isPaused = true)。这会阻止游戏对象移动,看起来像是暂停了,但仍然允许我执行诸如操作、添加对象等操作。

override func update(_ currentTime: TimeInterval) {

    if !gameLayer.isPaused {
        moveBackground()
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何防止在暂停场景(取消暂停后)上运行 SKAction,暂停/取消暂停场景后节点纹理不改变 的相关文章

  • iOS - 当 UIView 移动时将 UITextField 移动到不同的位置

    我有一个主 UIView 它通过开关向上移动 我有这个工作 那里没有问题 现在 UIView 当向下时 占据屏幕的大约一半 当它向上推时 它会显示底部 40px 在 UIView 中 当它处于向下状态时 它有一个 UITextField 并
  • Swift:如何减少 didupdatelocations 调用

    我想出了一些代码来打印我所在位置的地址和邮政编码 这是在 didupdatelocation 函数中完成的 我遇到的唯一问题是 didupdatelocation 函数每秒都会更新该地址 因为这电池效率非常低 所以我一直在寻找使用间隔的方法
  • 为什么在授予用户权限时,UIUserNotificationType.None 在当前设置中返回 true?

    我正在编写一种方法来检查当前用户设置是否包含某些通知类型 当检查当前设置是否包含 UIUserNotificationsType None 时 无论授予权限还是拒绝权限 它都会返回 true 有谁知道这是为什么吗 func register
  • Swift Codable 将空 json 解码为 nil 或空对象

    这是我的代码 class LoginUserResponse Codable var result String var data LoginUserResponseData var mess String public class Log
  • 从 iOS 应用程序内的 Junos Pulse 获取用户凭据

    我正在通过 Junos Pulse 在 iPad 中建立 VPN 连接 以进入我组织的 Intranet 谁能告诉我是否有任何 iOS api 或 SDK 可用于获取在 iOS 应用程序内的 Junos pulse 中输入的用户凭据 Jun
  • Objective-c 中的块递归

    当执行涉及 Objective C 块的递归时 我在 iOS 应用程序中收到 EXC BAD ACCESS 信号 这是简化的代码 void problematicMethod FriendInfo friendInfo onComplete
  • iPhone UINavigationBar 使用 [UINavigationBar 外观] 更改所有控制器的字体样式

    我知道我可以单独更改导航栏的字体 如本答案所述 更改导航栏的字体 https stackoverflow com questions 5832036 change the navigation bars font 目前我正在使用一种更全局的
  • 所需框架与静态库

    构建现代框架 https developer apple com videos play wwdc2014 416 says 每个应用程序都有自己的自定义框架副本 https stackoverflow com a 15262463 242
  • 使用 Interface Builder 创建 UIScrollView 的步骤

    我正在尝试使用 UIScrollView 但似乎有一些基本的事情我不理解 假设我想在我的 iPhone 应用程序中使用 UIScrollView 我有一个充满按钮的视图 尺寸为 320x700 显然 这对于 320x480 的 iPhone
  • 在真实设备上展示测试广告

    这是我的代码 let request GADRequest request testDevices kGADSimulatorID XXXX2F32d69CCA859FFB559D0FEA3CF6483D08A6 adView load r
  • 进入前台后刷新数据

    更改默认设置后 我想在AppDelegate中进入前台时刷新myViewController的数据 我所做的是 AppDelegate m void applicationDidFinishLaunching UIApplication a
  • iOS:提高图像绘制速度

    我有一系列想要制作动画的图像 UIImageView支持一些基本的动画 但不足以满足我的需求 我的第一个方法是使用UIImageView并设置image当图像属性 这太慢了 速度慢的原因是图像的绘制 这让我感到惊讶 我以为瓶颈会加载图像 我
  • iphone NSDate 转换问题

    在我的 facebook 图表 Api 中 我正在获取这些数据 来自杰森 updated time 2011 05 17T14 52 16 0000 我正在使用此代码将其转换为有效的日期格式 NSDateFormatter df NSDat
  • 频繁绘制 CGPath 时的性能

    我正在开发一个将数据可视化为折线图的 iOS 应用程序 该图被绘制为CGPath在全屏自定义中UIView最多包含 320 个数据点 数据经常更新 图表需要相应地重新绘制 刷新率为 10 秒就很好了 到目前为止很容易 然而 我的方法似乎需要
  • iOS 循环对象的属性并添加操作

    我有一个具有几个类似属性的类 UISliders 我想添加用户开始和结束使用每个滑块时的操作 每个滑块都将链接到同一个选择器 因此我考虑只是迭代它们 而不是编写 10 个几乎相同的代码块 问题是 最有效的方法是什么 我尝试过这样的事情 在运
  • Google 地图 API -> OpenGLES 崩溃

    日志是从 Crashlytics 粘贴的 对于许多用户来说 崩溃经常发生 据我所知 它与设备 iOS 版本无关 我在我的代码中找不到任何错误 这似乎是纯粹的库问题 是 Google 地图 API 错误吗 我可以做些什么来修复它 或者我应该在
  • 苹果企业程序分发问题[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 这个问题涉及到Apple iOS 开发者企业计划 http developer apple com programs ios enterprise 我
  • UIWebView Bug:-[UIWebView cut:]:无法识别的选择器发送到实例

    In the UIWebView 如果包含文本的输入元素具有焦点 并且按下按钮导致输入失去焦点 则随后双击输入以重新获得焦点并从出现的弹出栏中选择 剪切 或 复制 或 粘贴 会导致这UIWebView因错误而崩溃 UIWebView cut
  • Swift 中的 UIAlert 自动消失?

    我有以下代码 Creates Alerts on screen for user func notifyUser title String message String gt Void let alert UIAlertController
  • RemoteIO 音频单元播放回调中的 AudioBufferList 内容

    我想 拦截 音频数据传送到 iOS 设备扬声器的过程 我相信这可以使用 RemoteIO 音频单元和回调来完成 在下面的playbackCallback中 ioData实际上包含任何音频数据吗 static OSStatus playbac

随机推荐