Skip to content

Commit

Permalink
move audio logic out of SKScene
Browse files Browse the repository at this point in the history
  • Loading branch information
shirakaba committed Mar 11, 2019
1 parent 2acad9d commit 32c784b
Showing 1 changed file with 39 additions and 63 deletions.
102 changes: 39 additions & 63 deletions notes/SpriteKit.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
// Ray Wenderlich: https://www.raywenderlich.com/906-scenekit-tutorial-with-swift-part-2-nodes
// For 3D experiments, see: https://www.weheartswift.com/introduction-scenekit-part-1/

function getUIViewController(uiresponder){
for(let responder = uiresponder; responder !== null && typeof responder !== "undefined"; responder = responder.nextResponder){
if(responder instanceof UIViewController) return responder;
}
return null;
}

// SKScene is SpriteKit; SCN is SceneKit
// https://stackoverflow.com/questions/26225236/swift-spritekit-adding-button-programmatically
const BattlefieldScene = SKScene.extend(
{
// private indicator!: SKSpriteNode;
// private hero!: SKSpriteNode;
// private villain!: SKSpriteNode;
downloadFileFromURL: function(url){
NSURLSession.sharedSession.downloadTaskWithURLCompletionHandler(
url,
(urlB, response, error) => {
this.playAudioFromDownloadedFile(urlB);
}
).resume();
},

playAudioFromDownloadedFile: function(url){
let player;
try {
player = AVAudioPlayer.alloc().initWithContentsOfURLFileTypeHintError(url, AVFileTypeMPEGLayer3);
} catch(error){
// The error is a JS error, not an NSError.
global.label.text = error.toString();
return;
}
player.prepareToPlay();
player.numberOfLoops = -1; // loop forever
player.volume = 1.0;
player.play();
},

didMoveToView: function (view){
const indicatorHeight = 22;
this.indicator = SKSpriteNode.alloc().initWithColorSize(
Expand All @@ -48,22 +9,11 @@ const BattlefieldScene = SKScene.extend(
this.indicator.position = CGPointMake(
// The origin of the SKSpriteNode is at the midpoint rather than corner
this.frame.size.width / 2,
this.frame.size.height - (indicatorHeight / 2)
// this.frame.size.height - (indicatorHeight / 2)
0 + (indicatorHeight / 2)
);
this.addChild(this.indicator);

this.downloadFileFromURL(
NSURL.alloc().initWithString("https://birchlabs.co.uk/blog/alex/juicysfplugin/synth/cpp/2019/01/05/TheBox_compressed_less.mp3")
);

// const ost = SKAudioNode.alloc().initWithURL(
// "https://birchlabs.co.uk/blog/alex/juicysfplugin/synth/cpp/2019/01/05/TheBox_compressed_less.mp3"
// );
// ost.autoplayLooped = true;
// ost.positional = false;
// ost.runAction(SKAction.play());
// this.addChild(ost);

const heroSize = CGSizeMake(25, 25);
this.hero = SKSpriteNode.alloc().initWithColorSize(
UIColor.alloc().initWithRedGreenBlueAlpha(0,0,1,1),
Expand Down Expand Up @@ -111,8 +61,8 @@ const BattlefieldScene = SKScene.extend(
);

this.heroTargetPos = this.hero.position;
this.heroBaseSpeed = 5;
this.villainBaseSpeed = 3;
this.heroBaseSpeed = 5 / 1.5;
this.villainBaseSpeed = 3 / 1.5;

this.addChild(this.hero);
this.addChild(this.villain);
Expand Down Expand Up @@ -284,23 +234,49 @@ const GameViewController = UIViewController.extend(
}
);

const gamevc = GameViewController.alloc().init();
function getUIViewController(uiresponder){
for(let responder = uiresponder; responder !== null && typeof responder !== "undefined"; responder = responder.nextResponder){
if(responder instanceof UIViewController) return responder;
}
return null;
}

// design.ios.view = gamevc.view;
// design.ios.addSubview(gamevc.view);
function downloadFileFromURL(url){
NSURLSession.sharedSession.downloadTaskWithURLCompletionHandler(
url,
(urlB, response, error) => {
global.player = playAudioFromDownloadedFile(urlB);
}
).resume();
}

function playAudioFromDownloadedFile(url){
let player;
try {
player = AVAudioPlayer.alloc().initWithContentsOfURLFileTypeHintError(url, AVFileTypeMPEGLayer3);
} catch(error){
// The error is a JS error, not an NSError.
global.label.text = error.toString();
return null;
}
player.prepareToPlay();
player.numberOfLoops = -1; // loop forever
player.volume = 1.0;
player.play();
return player;
}

const gamevc = GameViewController.alloc().init();

const vc = getUIViewController(design.ios);
if(vc !== null){
vc.presentViewControllerAnimatedCompletion(
gamevc,
true,
() => {
// On completion
downloadFileFromURL(
NSURL.alloc().initWithString("https://birchlabs.co.uk/blog/alex/juicysfplugin/synth/cpp/2019/01/05/TheBox_compressed_less.mp3")
);
}
);

// vc.presentModalViewControllerAnimated(
// gamevc,
// true
// );
}

0 comments on commit 32c784b

Please sign in to comment.