Advertisement
rvz420

Efecto de nieve realista

Oct 13th, 2017
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. control_mc.onEnterFrame = function() {
  3.     createSnow();
  4. };
  5. function createSnow() {
  6.     /* Crea un copo de nieve a partir del patrón SnowFlake que está en la biblioteca */
  7.     i = _root.getNextHighestDepth();
  8.     tmp = _root.attachMovie("SnowFlake", "snowflake_mc"+i, i);
  9.     /* El copo de nieve cae desde una posición horizontal en [1,550] */
  10.     tmp._x = randRange(1, 550);
  11.     /* Inicialmente, se coloca el copo de nieve fuera del escenario */
  12.     tmp._y = -1;
  13.     /* Ahora se particulariza el copo de nieve estableciendo aleatoriamente su transparencia, * velocidad y tamaño */ 
  14.     tmp._alpha = randRange(50, 100);
  15.     tmp.speed = randRange(1, 10);
  16.     tmp._xscale = randRange(70, 110);
  17.     tmp._yscale = tmp._xscale;
  18.     /* Todo copo de nieve inicia su descenso inmediatamente después de creado */
  19.     tmp.moving = true;
  20.     /* moveSnow es responsable de la dinámica del copo de nieve */
  21.     tmp.onEnterFrame = moveSnow;
  22. }
  23. function moveSnow() {
  24.     /* Si el copo está en movimiento... */
  25.     if (this.moving) {
  26.         /* El copo desciende según su velocidad prefijada */
  27.         this._y += this.speed;
  28.         /* Para mayor realismo, el copo experimenta un desplazamiento lateral, oscilatorio */
  29.         this._x += Math.cos(this._y/10);
  30.         /* Si el copo colisiona con Ice_mc detenerlo. Como no se desea que el copo
  31.                    permanezca detenido indefinidamente, se utilizará un contador (stopCounter)
  32.                    para verificar que transcurra un tiempo prudente antes de remover el copo */
  33.         if (this.hitTest(_root.rect_mc.Ice_mc)) {
  34.             this.moving = false;
  35.             this.stopCounter = 0;
  36.         }
  37.         /* Finalmente, los copos que lleguen al suelo serán eliminados de inmediato */
  38.         if (this._y>327) {
  39.             removeMovieClip(this);
  40.         }
  41.     } else {
  42.         /* Entrar aquí implica que el copo está detenido por colisionar con Ice_mc. Por ende,
  43.                    se incrementa el contador stopCounter, y si éste supera la cota superior
  44.                    preestablecida,  se procede a eliminar el copo */
  45.         this.stopCounter++;
  46.         if (this.stopCounter>500) {
  47.             this.onEnterFrame = null;
  48.             this.removeMovieClip();
  49.         }
  50.     }
  51. }
  52. function randRange(min:Number, max:Number):Number {
  53.     var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
  54.     return randomNum;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement