如何从场景中删除SKSpriteNode的音频(Swift)?

时间:2016-03-28 19:45:45

标签: swift sprite-kit skspritenode skaction sknode

这是我的代码:

 override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let backgroundImage = SKSpriteNode(imageNamed: "Background.jpeg")
    backgroundImage.size = self.frame.size
    backgroundImage.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    backgroundImage.zPosition = 1
    self.addChild(backgroundImage)


    let addBugAction = SKAction.sequence([SKAction.runBlock({
        self.addBugsToScene()
    }), SKAction.waitForDuration(1)])
    self.runAction(SKAction.repeatActionForever(addBugAction))

}

func addBugsToScene() {

    let bug = SKSpriteNode(imageNamed: "Mos2.gif")
    bug.name = "Mosquito"


    //giving random position to and assigning to bugs
    let randomPoint = gettingRandomPosition(bug.size)
    bug.position = CGPoint(x: randomPoint.x, y: randomPoint.y)
    bug.zPosition = 12



    let blockOFaction = SKAction.runBlock({

        let randomMovingPoint = self.gettingRandomPosition(bug.size)

         let action = SKAction.moveTo(CGPoint(x: randomMovingPoint.x, y: randomMovingPoint.y), duration: 2)

        //action.speed = 3.0
        let movePlusSoundAction = SKAction.group([action,SKAction.playSoundFileNamed("MosquitoNoise.wav", waitForCompletion: false)])

        bug.runAction(movePlusSoundAction)

    })
    let waitAction = SKAction.waitForDuration(1)
    bug.runAction(SKAction.repeatActionForever(SKAction.sequence([blockOFaction, waitAction])))


    self.addChild(bug)

}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {        

   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        let touchedNode = self.nodeAtPoint(location)

        if touchedNode.name == "Mosquito"{

            touchedNode.removeAllActions()

            //touchNode.runAction(SKAction.Stop())

            //removing the bug which is tapped
            touchedNode.removeFromParent()
        }
    }
}

当我触摸节点(bug)时,它将从场景中删除。在这里,我试图阻止播放与bug相关的音频。但是,即使节点从场景中移除,音频也会继续播放。如果节点从场景中删除,我该如何停止音频?

2 个答案:

答案 0 :(得分:1)

+ playSoundFileNamed:waitForCompletion:只是播放声音。您无法控制正在播放的声音(无法暂停,取消暂停,停止等)。有些人说你可以为声音动作分配一个键,并通过移除键来停止动作。到目前为止,这对我来说没有用(在许多iOS版本上尝试过)。

另外,我用动作键试过这个技巧的最后一个iOS版本是9.1 ......所以,有机会你可以试试9.3 ...

无论如何,这是来自文档:

  

使用SKAction playSoundFileNamed:waitForCompletion:仅用于简短   杂费。使用AVAudioPlayer可以长时间播放背景音乐。这个   行动是不可逆的;反向动作与   原始行动。

所以,它只是播放短音,就是这样。

作为替代方案,您可以使用SKAudioNode,但仅适用于iOS9及更高版本。

答案 1 :(得分:1)

以下是我编码以获得结果的方式。

我在addBugToScene()

中添加了以下代码
let mosquitoNoise = SKAudioNode(fileNamed: "MosquitoNoise.wav")
    mosquitoNoise.autoplayLooped = true
    mosquitoNoise.runAction(SKAction.changeVolumeTo(0.3, duration: 60))
    bug.addChild(mosquitoNoise)

//changes made here
 let movePlusSoundAction = SKAction.group([action,SKAction.runBlock({

            mosquitoNoise.runAction(SKAction.play())
            })])

        bug.runAction(movePlusSoundAction)

    })


 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        let touchedNode = self.nodeAtPoint(location)

        if touchedNode.name == "Mosquito"{

            //removes the SKAudioNode so audio will stop
            touchedNode.removeAllChildren()

            //removing the bug which is tapped
            touchedNode.removeFromParent()

        }