私はSwiftコーディングが初めてで、ノードに触れているときにオン/オフ機能を備えたループオーディオを作成する方法を理解しようとしています. それを実装する最善の方法は SKAudioNode を使用することだと思いましたが、次のコードで何が間違っているのかわかりません。Node を押すと - SKAudioNode が再生を開始し、押してサウンドをオフにしても、サウンドは停止しません。もう一度押すと、前の音に重なって音が鳴ります。本質的な部分全体をここに置きます。前もって感謝します。
import SpriteKit
import AVFoundation
ここでは、サウンドがオンまたはオフの場合にトリガーするブール値を作成しています。
var firesoundon = false
次にシーンを設定します。
class GameScene2: SKScene {
override func didMoveToView(view: SKView) {
setUpScenery()
}
private func setUpScenery() {
//BACKGROUND
let background = SKSpriteNode(imageNamed: backgroundImage, normalMapped: true)
background.anchorPoint = CGPointMake(0, 0)
background.position = CGPointMake(0, 0)
background.zPosition = Layer.ZBackground
background.size = CGSize(width: self.view!.bounds.size.width, height: self.view!.bounds.size.height)
background.lightingBitMask = 1;
addChild(background)
//FIRE
let fireLayerTexture = SKTexture(imageNamed: fireImage)
let fireLayer = SKSpriteNode(texture: fireLayerTexture)
fireLayer.anchorPoint = CGPointMake(1, 0)
fireLayer.position = CGPointMake(size.width, 0)
fireLayer.zPosition = Layer.Z4st
addChild(fireLayer)
let firePath = UIBezierPath()
firePath.moveToPoint(CGPointMake(0, 0))
firePath.addCurveToPoint(CGPointMake(115, 215), controlPoint1: CGPointMake(5, 170), controlPoint2: CGPointMake(90, 190))
firePath.addCurveToPoint(CGPointMake(150, 260), controlPoint1: CGPointMake(115, 215), controlPoint2: CGPointMake(120, 295)) //curve1
firePath.addCurveToPoint(CGPointMake(180, 280), controlPoint1: CGPointMake(150, 260), controlPoint2: CGPointMake(155, 350)) //curve2
firePath.addCurveToPoint(CGPointMake(225, 280), controlPoint1: CGPointMake(180, 280), controlPoint2: CGPointMake(210, 395)) //curve3
firePath.addCurveToPoint(CGPointMake(250, 260), controlPoint1: CGPointMake(230, 280), controlPoint2: CGPointMake(245, 290)) //curve4
firePath.addCurveToPoint(CGPointMake(290, 220), controlPoint1: CGPointMake(250, 260), controlPoint2: CGPointMake(280, 270)) //curve5
firePath.addLineToPoint(CGPointMake(380, 200))
firePath.addLineToPoint(CGPointMake(380, 0))
firePath.addLineToPoint(CGPointMake(0, 0))
let fireNode = SKShapeNode(path: firePath.CGPath, centered: false)
fireNode.alpha = 0.2
fireNode.zPosition = Layer.Z4st+1
fireNode.lineWidth = 2
fireNode.strokeColor = SKColor.whiteColor()
fireNode.fillColor = SKColor.blueColor()
fireNode.position = CGPointMake(size.width/2, 0)
fireNode.name = "fireNode"
self.addChild(fireNode)
火のオン/オフ機能の作成:
func fireTurnsOnOff () {
let playguitar: SKAudioNode
playguitar = SKAudioNode(fileNamed: "guitar.wav")
if firesoundon == false {
addChild(playguitar)
firesoundon = true
print("firesoundon is == \(firesoundon)")
}
else if firesoundon == true {
let stopguitar = SKAction.stop()
playguitar.runAction(stopguitar)
print("GuiterStopped")
firesoundon = false
print("firesoundon is == \(firesoundon)")
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let touchLocation = touch.locationInNode(self)
let node: SKNode = nodeAtPoint(touchLocation)
if node.name == "fireNode" {
fireTurnsOnOff()
}
}
}