我想同步场景中多个动作的完成(例如语音,视频和动画),并在一组中的多个动作完成后触发另一个动作。下面的简单案例在语音和SKAction完成后打印出一些内容。
import SpriteKit
import PlaygroundSupport
import AVFoundation
let start = CGPoint(x: 100, y: 50)
let end = CGPoint(x: 200, y: 50)
let radius: CGFloat = 20;
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let skview = SKView(frame: bounds)
PlaygroundPage.current.liveView = skview
PlaygroundPage.current.needsIndefiniteExecution = true
class MyScene: SKScene,AVSpeechSynthesizerDelegate {
var motionComplete = false
var speechComplete = false
let synth = AVSpeechSynthesizer();
override func sceneDidLoad() {
synth.delegate = self
synth.speak(AVSpeechUtterance(string: "Ola"))
let greenball = SKShapeNode(circleOfRadius: radius);
greenball.position = start;
greenball.fillColor = .green;
let motionpath = CGMutablePath();
motionpath.move(to: start)
motionpath.addLine(to: end);
let motion = SKAction.follow(motionpath, asOffset: false, orientToPath: true,duration: 2);
greenball.run(motion) {
print ("motion complete")
self.motionComplete = true;
self.syncUp()
};
self.addChild(greenball);
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
print ("speech complete")
speechComplete = true;
self.syncUp()
}
func syncUp() {
if (motionComplete && speechComplete) {
print ("speech and motion complete");
}
}
}
let scene = MyScene(size: CGSize(width: 400, height: 200));
scene.scaleMode = SKSceneScaleMode.aspectFill
scene.size = skview.bounds.size
skview.presentScene(scene);
实际上,我正在尝试协调多个用户驱动和应用驱动的操作。是否有更好的技术来实现无错同步?