【问题标题】:How can I disable a SpriteKit node touch event and then enable it after 25 seconds to be touched and moved to next scene?如何禁用 SpriteKit 节点触摸事件,然后在 25 秒后启用它以被触摸并移动到下一个场景?
【发布时间】:2019-05-19 14:47:31
【问题描述】:

如何禁用 SpriteKit 节点触摸事件,然后在 25 秒后启用它,以便在触摸节点时将用户带到下一个场景?

我正在设置一个播放音频文件 25 秒的 GameScene,然后我希望用户能够单击 SpriteKit 节点,该节点会将用户带到下一个场景。问题是节点无法隐藏。它需要是可见的,但在 25 秒后禁用,然后可见并启用触摸。

    if nextButton.contains(location) {
        if nextButton.isHidden == true {
            nextButton.isUserInteractionEnabled = false
        } else {
            goToScene(scene: getNextScene()!)
        }

当 SPriteKit 节点能够被隐藏时,我已使用此代码,但这次节点必须始终可见。

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    从表面上看,您的 GameScene 负责处理触摸事件,因此当您播放音频时,您应该执行以下操作:

    self.userInteractionEnabled = false
    let audio = SKAudioNode()
    audio.run{
        SKAction.group([SKAction.play, 
            SKAction.sequence([
                SKAction.wait(forDuration:25),
                SKAction.run({self.userInteractionEnabled = true})
            ])
        )
    }
    self.addChild(audio)
    

    如果您正在使用 playSound 动作

    self.userInteractionEnabled = false
    let audio = SKSpriteNode()
    audio.run{ 
        SKAction.sequence([
            SKAction.playSoundFileNamed("mySound",true),
            SKAction.run({self.userInteractionEnabled = true})
        ])
    }
    

    基本上,它的作用是设置延迟动作以在声音结束后启用触摸。

    【讨论】:

    • 我尝试使用第一个,但仍然出现错误。该代码是否应该在 didMove 函数下?
    • 你现在在哪里做?
    • 在覆盖函数 didMove 下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2023-03-16
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多