How to make confetti?

I really want to emit particles of confetti. Each particle has the same shape, however I want each particle to be a random color from the set of colors that I specify.

Is there a way that each emitted particle has a random color, or do I need a separate emitter for each color of the particles?

+7
ios swift sprite-kit particles
source share
2 answers

You can use a single emitter to achieve what you want:

import SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { let emitter = SKEmitterNode(fileNamed: "particle") let colors = [SKColor.whiteColor(),SKColor.grayColor(),SKColor.greenColor(),SKColor.redColor(),SKColor.blackColor()] override func didMoveToView(view: SKView) { self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) emitter.position = CGPoint(x: 200, y:300) emitter.particleColorSequence = nil emitter.particleColorBlendFactor = 1.0 self.addChild(emitter) let action = SKAction.runBlock({ [unowned self] in let random = Int(arc4random_uniform(UInt32(self.colors.count))) self.emitter.particleColor = self.colors[random]; println(random) }) let wait = SKAction.waitForDuration(0.1) self.runAction(SKAction.repeatActionForever( SKAction.sequence([action,wait]))) } } 

EDIT:

Try changing the duration of the wait to get different results.

You can also play with the color ramp (in the particle editor) to achieve the same effect:

enter image description here

Or you can use particleColorSequence and SKKeyframeSequence to change the color of the particles during its lifetime. Hope this helps.

+7
source share

Just for those who want an answer to this question. There is a structure called SAConfettiView https://github.com/sudeepag/SAConfettiView . Definitely check it out! It worked for me.

0
source share

All Articles