PLEASE SOMEONE HELP!
I want to have my SKEmiterNode's scale(meaning size) get larger and smaller to the music i have built into the application using AVAudioPlayer. Right now this is pretty much all I have for the SKEmiterNode and it looks great:
beatParticle?.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
var beatParticleEffectNode = SKEffectNode()
beatParticleEffectNode.addChild(beatParticle!)
self.addChild(beatParticleEffectNode)
All the looks are done in the .sks file.
Here is where I call the "updateBeatParticle" function in a continual loop so that It can where i will put my code for making the particle's scale(meaning size) larger and smaller to the music.
var dpLink : CADisplayLink?
dpLink = CADisplayLink(target: self, selector: "updateBeatParticle")
dpLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
func updateBeatParticle(){
//Put code here
}
Any idea how i can do this? I looked at some tutorials such as this: https://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios
However, i can't quite get my head around it because they're using an emitterLayer and its in Obj-C and am also interested in any other ideas you wonderful people may have!
WARNING: The following code has not been tested. Please let me know if it works.
Firstly, it looks like you are using SpriteKit, therefore you could put the code needed to alter the emitter scale in the
SKScene
methodupdate:
, which automatically gets called virtually as often as aCADisplayLink
.Essentially all you need to do is update the emitter scale in the
update:
method based on the volume of the channels of yourAVAudioPlayer
. Note that the audio player may have multiple channels running, so you need to average out the average power for each.Firstly...
Set this after you initialise your audio player, so that it will monitor the levels of the channels.
Next, add something like this in your update method.
The method
getIntensityFromPower
is used to convert the power in decibels, to a more appropriate percentage representation. This method can be declared like so...The algorithm for this conversion was taken from this StackOverflow response https://stackoverflow.com/a/16192481/3222419.