3

I've been having a play with Swift today, I forked the FlappyBird clone and have been making a few changes, like adding collision detection.

I'd like to have some particles emitted when the bird hits the pipe, so I've done a bit of reading and found the SKEmitterNode class. However all the documentation is in Objective-C so I'm having to transpose it all to Swift as there's currently nothing online about how to use SpriteKit in Swift.

This is how I'm attempting to create the Emitter

// sparkles and burstEmitter are defined as class variables
sparkles = SKTexture(imageNamed: "bird-01") //reusing the bird texture for now
burstEmitter = SKEmitterNode()
burstEmitter.particleTexture = sparkles
burstEmitter.position = CGPointMake(200, 200)
burstEmitter.particleBirthRate = 20
burstEmitter.numParticlesToEmit = 200;
self.addChild(burstEmitter)

Does this look right?

When I build I don't see any particles on screen.

The full source of my fork is on github here - https://github.com/jolyonruss/FlappySwift

Thanks for any help

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jolyonruss
  • 1,770
  • 2
  • 19
  • 34

2 Answers2

2

You can also make use of Xcode 6's own particle editor via .sks files.

Here's some code I got working in Swift with the added benifit of being able to edit the particle effect in XCode

//spark particle effect

    let sparkEmmitterPath:NSString = NSBundle.mainBundle().pathForResource("sparkParticleEffect", ofType: "sks")

    let sparkEmmiter = NSKeyedUnarchiver.unarchiveObjectWithFile(sparkEmmitterPath) as SKEmitterNode

    sparkEmmiter.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 - 200)
    sparkEmmiter.name = "sparkEmmitter"
    sparkEmmiter.zPosition = 1
    sparkEmmiter.targetNode = self

    self.addChild(sparkEmmiter)

This just places the effect at the center of the screen. Obviously this can be edited based on how you'd use the effects. Hope this helps.

Chuck Gaffney
  • 256
  • 3
  • 9
  • 1
    Thanks for the update @Chuck, as an Objective-C novice that actually makes sense, one of the advantages of it's verbosity, I guess. – jolyonruss Jul 07 '14 at 13:39
  • No problem at all :-) Yeah, I'm liking the ability to edit the particles in Xcode directly with 6. In the past I used a program called PhysicsEditor to sort of do the same thing. Saves time from having to input all of those properties individual in the code. – Chuck Gaffney Jul 07 '14 at 21:18
  • Just off the top of my head self.removeChild(sparkEmmitter) should remove the effect in this case. Granted, you'd have to re-add it to have it back on. I believe there's also a way to shut it off without removing it entirely form the scene. Just not sure of it at moment; doing so could save framerate by not having to reallocate the particle effect's data. – Chuck Gaffney Sep 29 '14 at 05:17
0

So it turns out that more properties are required by the SKEmitterNode in order for it to work, I added the following and I have some particles emitting.

burstEmitter.particleLifetime = 3.0
burstEmitter.particleSpeed = 10.0
burstEmitter.xAcceleration = 100
burstEmitter.yAcceleration = 50
jolyonruss
  • 1,770
  • 2
  • 19
  • 34