Вот мой код:
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()
}
}
}
Когда я касаюсь узла (ошибки), он будет удален со сцены. Здесь я пытаюсь остановить воспроизведение звука, связанного с ошибкой. Но звук продолжает воспроизводиться даже после удаления узла со сцены. Как остановить звук, как только нода исчезнет со сцены?
[unowned self]
), либо удалите это действие перед фактическим переходом к следующей сцене. Всегда проверяйте, правильно ли вызываетсяdeinit
вашей сцены (делайте это для каждого пользовательского класса, который вы создали). - person Whirlwind   schedule 29.03.2016