How to resize an SKEmitterNode?
Asked Answered
S

1

5

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.

Schechter answered 26/3, 2016 at 9:29 Comment(0)
T
10

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

Ticktock answered 26/3, 2016 at 15:5 Comment(8)
so i position the emitter in the center of the view, and set the range to be the width and height? That doesn't sound so bad. :-)Schechter
that totally worked and is such a relief to know and understand :-) thanks so much!Schechter
one more question... for snow, how do i make the particles only initially appear ABOVE the view, and not randomly throughout the view?Schechter
@DaveKliman You should ask a different question because that is whole another issue. Also "above the view" or "throughout the view" are pretty unclear statements. The emitter is a part of the scene, not of the view.Ticktock
i noticed the zParticlePositionRange property, which looks interesting. but it seems to have been depreciated. zPosition is definitely helpful if I want some snow to appear in front of and other snow to appear in back of objects.Schechter
I deleted my comment about zPositions and appearing order because it seems that is not what you were asking about. By the way particleZPosition property is not deprecated. I was referring to it, not to the particleZPositionRange which is actually deprecated.Ticktock
@DaveKliman And about your edit... You want snow to spawn above the scene and fall down ? From the above , right ? If so, just place an emitter right above the scene (use self.frame.maxY) and change the emitter position range to something like dx = scene.size.width and dy = 10.Ticktock
Let us continue this discussion in chat.Schechter

© 2022 - 2024 — McMap. All rights reserved.