Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize an SKEmitterNode?

I just wanted to do something that to me seems really simple, which is make an emitter into the entire background of a view... then I would want the view to be able to aspectFill and scale and so forth, allowing the emitter to look proper whatever I did...

I'm not looking to just scale the emitter. I want the objects to remain the right size, but i want the area of the emitter to change... think of it like the "canvas size" (without resizing) option in photoshop..

I would use this effect, for example, to add snow or rain to an entire scene, or make a snow globe sprite...

Thing is, maybe I'm just not looking in the right place, but it seems that any size properties on an SKEmitterNode are read only... what am I doing wrong?

here's some code, where the resulting emitter is just a small rectangle in the middle of the view.

 override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        if let emitter = SKEmitterNode(fileNamed: "Bokeh") {
            emitter.position = view.center
            scene.addChild(emitter)
        }
        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill
        skView.presentScene(scene)
    }
}

I need to clarify a little more. For snow, I would want the particles to instantiate before they enter the scene, and then fall through the scene, and only die after they have left the scene, rather than randomly appear throughout the scene, and then continue to fall down...I would want the snow to take up the entire bounds of the scene.

like image 579
Dave Kliman Avatar asked Mar 26 '16 09:03

Dave Kliman


1 Answers

What you are looking for is called particlePositionRange :

The range of allowed random values for a particle’s position.

You can change it like this:

 emitterNode.particlePositionRange = CGVector(dx: dx, dy: dy)

dx should be the width of your emitter, and dy the height. So that might be the size of a scene (note that by default size of the scene is 1024x768 if not specified differently).

Same thing you can do using the Particle Editor by changing the values in Position Range section:

position range

like image 155
Whirlwind Avatar answered Oct 31 '22 12:10

Whirlwind