# snow3 technique on 2 layers

**URL:** <https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333>\
**Category:** flash\
**Created:** [June 25, 2008, 3:26am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333 "2008-06-25T03:26:34Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 25, 2008, 3:26am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/1 "2008-06-25T03:26:34Z")

</div>

Hi,

I would like to replicate the falling snow technique ( [http://www.kirupa.com/developer/flash8/snow.htm](http://www.kirupa.com/developer/flash8/snow.htm) ) on 2 layers.  
This is so I can have foreground snow in front of a character and background snow behind him.

Any ideas on the best way to go about this? I tried duplicating a copy of the script onto a second layer but it only shows one set of snow. Even when I change all the variable names and make a new snowflake.

My developer skills are very minimal so I don’t know why it’s not working - I suspect there’s a more efficient way anyway?

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 25, 2008, 10:25pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/2 "2008-06-25T22:25:09Z")

</div>

When I duplicated the code I appended a “2” to the second copy of everything (all the variable names etc, including the function name). So it’s trying to call init and init2 both at once. Could this be the problem? Or because it’s trying to perform two “onEnterFrame” at the same time?

I made one snowflake white and the other red so I could see what’s happening. If I remove one script, the other works and vice versa. This leads me to believe that in my programming ignorance I’m breaking some basic law. When trying to run, it times out and crashes. I’m definately confusing it somehow.

---

<div class="post-metadata">

**Author:** ![Insane\_au](https://avatars.discourse-cdn.com/v4/letter/i/439d5e/32.png) [@Insane\_au](https://forum.kirupa.com/u/Insane_au)\
**Post date:** [June 26, 2008, 8:10am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/3 "2008-06-26T08:10:56Z")

</div>

Why are you running two identical scripts simultaneously in the same movie?

---

<div class="post-metadata">

**Author:** ![pantas](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pantas](https://forum.kirupa.com/u/pantas)\
**Post date:** [June 26, 2008, 2:32pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/4 "2008-06-26T14:32:48Z")

</div>

post the code!

it’s a lot more simple 😃

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 26, 2008, 10:36pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/5 "2008-06-26T22:36:31Z")

</div>

Yeah I know that’s dodgy. I was just trying to get it working in any way I could before refining. But I didn’t even get that far…

The code is on this page (there’s only one script so it’s easy to see what it does in one place):  
[http://www.kirupa.com/developer/flash8/snow.htm](http://www.kirupa.com/developer/flash8/snow.htm)  
Mine is exactly the same except I’ve duplicated the script. Which is incorrect of course, so what would be the best way to go about achieving what I want? I really would need to be able to set the parametres individually for both sets of snow (so the one’s in background layer are smaller and slower to create a better sense of depth in the scene).

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 26, 2008, 11:05pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/6 "2008-06-26T23:05:17Z")

</div>

This graphic shows how I want to separate the layers - green behind character layer, white on top.  
 ![](http://www.elfshot.com/snow-eg.gif)

---

<div class="post-metadata">

**Author:** ![pantas](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pantas](https://forum.kirupa.com/u/pantas)\
**Post date:** [June 27, 2008, 11:16am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/7 "2008-06-27T11:16:00Z")

</div>

if its the same script, why not put the script in a movieclip, and then duplicate it?

one with 0 depth, contents in 1, and the other snow clip in 3?

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 28, 2008, 9:43am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/8 "2008-06-28T09:43:37Z")

</div>

Is using this script the most efficient way to animate snow? It seems to run poorly pretty quicky. If I add more than 50 snowflakes it slows down pretty badly. My stage is 800 x 500 px. Are there any better snow scripts out there or is this as good as it’ll get?

Pantas, how do I put it into a movie script?

---

<div class="post-metadata">

**Author:** ![scallywag](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@scallywag](https://forum.kirupa.com/u/scallywag)\
**Post date:** [June 28, 2008, 3:21pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/9 "2008-06-28T15:21:18Z")

</div>

i think it looks fairly efficient.  
You can put into seperate clips just adapting the original script slightly

```auto
width = 300;
height = 200;
max_snowsize = 10;
snowflakes = 50;
//depth,linkage id,and vertical velocity
function makesnow(d, s, vely) {
 var c = this.createEmptyMovieClip("t", d);
 for (i=0; i<snowflakes; i++) {
  var t = c.attachMovie(s, "snow"+i, i);
  t._alpha = 20+Math.random()*60;
  t._x = -(width/2)+Math.random()*(1.5*width);
  t._y = -(height/2)+Math.random()*(1.5*height);
  t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
  t.k = 1+Math.random()*vely;
  t.wind = -1.5+Math.random()*(1.4*3);
  t.onEnterFrame = mover;
 }
}
mover = function () {
 this._y += this.k;
 this._x += this.wind;
 if (this._y>height+10) {
  this._y = -20;
 }
 if (this._x>width+20) {
  this._x = -(width/2)+Math.random()*(1.5*width);
  this._y = -20;
 } else if (this._x<-20) {
  this._x = -(width/2)+Math.random()*(1.5*width);
  this._y = -20;
 }
};
//mc is the instance name of the character thingy on the stage -made into a movieclip
mc.swapDepths(15);
makesnow(10, "snow", 2);
//you need another instance of snow in the library which you can color,resize etc
//linkage identifier snow2
makesnow(20, "snow2", 5);

```

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 28, 2008, 8:44pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/10 "2008-06-28T20:44:37Z")

</div>

Thanks heaps Scallywag, that’s working pretty well. It would be good to control the amount of snow separately for each of the two groups but I guess this isn’t critical.

The main problem with the original script (and same with modded version) is that when you add wind it means a lower corner of the stage won’t get any snow across it because it floats down from the top then across -leaving a triangle shape of uncovered stage on the opposite side to the wind direction. I made the width wider than the stage which helps a little, but I’m guessing this is adding uneccessary processing weight? (My stage is 800 wide so I set the snow width to 900 -tends to cover up the hole a little bit but not perfect.)

---

<div class="post-metadata">

**Author:** ![scallywag](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@scallywag](https://forum.kirupa.com/u/scallywag)\
**Post date:** [June 28, 2008, 10:12pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/11 "2008-06-28T22:12:39Z")

</div>

[quote=Gombul;2350758]Thanks heaps Scallywag, that’s working pretty well. It would be good to control the amount of snow separately for each of the two groups but I guess this isn’t critical.

The main problem with the original script (and same with modded version) is that when you add wind it means a lower corner of the stage won’t get any snow across it because it floats down from the top then across -leaving a triangle shape of uncovered stage on the opposite side to the wind direction. I made the width wider than the stage which helps a little, but I’m guessing this is adding uneccessary processing weight? (My stage is 800 wide so I set the snow width to 900 -tends to cover up the hole a little bit but not perfect.)[/quote]  
you could just add another param to the function and remove the snowflakes variable

```auto
width = 300;
// pixels 
height = 200;
// pixels 
max_snowsize = 10;
// pixels 
function makesnow(d, s, vely, snowflakes) {
 var c = this.createEmptyMovieClip("t", d);
 for (i=0; i<snowflakes; i++) {
  var t = c.attachMovie(s, "snow"+i, i);
  t._alpha = 20+Math.random()*60;
  t._x = -(width/2)+Math.random()*(1.5*width);
  t._y = -(height/2)+Math.random()*(1.5*height);
  t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
  t.k = 1+Math.random()*vely;
  t.wind = -1.5+Math.random()*(1.4*3);
  t.onEnterFrame = mover;
 }
}
mover = function () {
 this._y += this.k;
 this._x += this.wind;
 if (this._y>height+10) {
  this._y = -20;
 }
 if (this._x>width+20) {
  this._x = -(width/2)+Math.random()*(1.5*width);
  this._y = -20;
 } else if (this._x<-20) {
  this._x = -(width/2)+Math.random()*(1.5*width);
  this._y = -20;
 }
};
mc.swapDepths(15);
makesnow(10, "snow", 2, 60);
makesnow(20, "snow2", 3, 10);

```

you could fiddle with the properties \_x & wind to cover the triangle thing

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 30, 2008, 5:59am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/12 "2008-06-30T05:59:27Z")

</div>

Cool, snowflakes parametre doing the trick.  
Still not sure about the triangle issue though. Need some to fall from the sides too maybe?

---

<div class="post-metadata">

**Author:** ![scallywag](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@scallywag](https://forum.kirupa.com/u/scallywag)\
**Post date:** [June 30, 2008, 6:53am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/13 "2008-06-30T06:53:04Z")

</div>

[quote=Gombul;2351190]Cool, snowflakes parametre doing the trick.  
Still not sure about the triangle issue though. Need some to fall from the sides too maybe?[/quote]

yes you could try that - maybe reduce wind as well so more fall vertically.Heres one variation but i think you just need to play about until you get the effect you want

```auto
width = 800;
// pixels 
height = 200;
// pixels 
max_snowsize = 10;
// pixels 
function makesnow(d, s, vely, snowflakes) {
 var c = this.createEmptyMovieClip("t", d);
 for (i=0; i<snowflakes; i++) {
  var t = c.attachMovie(s, "snow"+i, i);
  t._alpha = 20+Math.random()*60;
  t._x = Math.random()*width
  t._y = -(height/2)+Math.random()*(1.5*height);
  t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
  t.k = 1+Math.random()*vely;
  t.mod = Math.round(Math.random())
  t.wind =( -1.5+Math.random()*(1.4*3))*t.mod;
  trace(t.mod)
  t.onEnterFrame = mover;
 }
}
mover = function () {
 this._y += this.k;
 this._x += this.wind;
 if (this._y>height+10) {
  this._y = -20;
  this._x = Math.random()*width
 }
 if (this._x>width+20) {
  this._x =-20
 } else if (this._x<-20) {
  this._x = width +20
 }
};
mc.swapDepths(15);
makesnow(10, "snow", 2, 60);
makesnow(20, "snow2", 3, 10);

```

---

<div class="post-metadata">

**Author:** ![Gombul](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gombul](https://forum.kirupa.com/u/Gombul)\
**Post date:** [June 30, 2008, 11:59pm UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/14 "2008-06-30T23:59:16Z")

</div>

Why is Flash throwing an output of random 0’s and 1’s when I run this inside flash?

```auto

width = 800;
height = 500;
max_snowsize = 10;
// depth, linkage id, vertical velocity, amount of snowflakes
function makesnow(d, s, vely, snowflakes) {
 var c = this.createEmptyMovieClip("t", d);
 for (i=0; i<snowflakes; i++) {
  var t = c.attachMovie(s, "snow"+i, i);
  t._alpha = 20+Math.random()*60;
  t._x = Math.random()*width;
  t._y = -(height/2)+Math.random()*(1.5*height);
  t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
  t.k = 1+Math.random()*vely;
  t.mod = Math.round(Math.random());
  t.wind = (-4+Math.random()*(1.4*3))*t.mod;
  trace(t.mod);
  t.onEnterFrame = mover;
 }
}
mover = function () {
 this._y += this.k;
 this._x += this.wind;
 if (this._y>height+10) {
  this._y = -20;
  this._x = Math.random()*width;
 } if (this._x>width+20) {
  this._x =-20;
 } else if (this._x<-20) {
  this._x = width +20;
 }
};
// mc is instance name of symbol between snow layers
mc.swapDepths(15);
makesnow(10, "snow-background", 2, 60);
makesnow(20, "snow-foreground", 3, 40);

```

---

<div class="post-metadata">

**Author:** ![scallywag](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@scallywag](https://forum.kirupa.com/u/scallywag)\
**Post date:** [July 1, 2008, 7:39am UTC](https://forum.kirupa.com/t/snow3-technique-on-2-layers/264333/15 "2008-07-01T07:39:04Z")

</div>

it is the trace

```auto
t.mod = Math.round(Math.random());
  t.wind = (-4+Math.random()*(1.4*3))*t.mod;
  trace(t.mod);

```

tmod has a value of either 0 or 1 -determines if wind is/is not applied
