简单的实现雪花的十几行as代码

看网上的雪花,三方库的,粒子系统,看着很高深复杂,我觉得有点杀鸡用牛刀。

我耍了小聪明,一共三种轨迹,正弦曲线,两种抛物线,然后两种曲线求导(自己求导),得到刚好是x  y坐标的变化量  程序可以更简单

package
{
 import flash.display.GradientType;
 import flash.display.Shape;
 import flash.display.SpreadMethod;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.geom.Matrix;
 import flash.net.SharedObject;
 
 
 [SWF(width=960,height=560,backgroundColor="#000000")]
 public class test_snow extends Sprite
 {
  private var snow_list:Array = new Array();
  private var i:int = 0;
  
  private static const NUM:int = 300;
  private var r:int = 1;
  public function test_snow()
  {
   addEventListener(Event.ENTER_FRAME,onEnterFrame); 
   for(i = 0; i<NUM; i++)
   { 
    var  circle:Shape = new Shape();
    var x:int = Math.random()*960;
    var y:int = Math.random()*560;
  
    var temp:int = Math.random();
    if(Math.random() > 0.3)
    {
     circle.alpha = 0.5;
    }
    if(Math.random() <0.3)
    {
     circle.alpha = 0.8;
    }
    circle.x = x;
    circle.y = y;
    snow_list.push(circle);
    draweEllipse(circle,0, 0);
   }
  }
  
  private function draweEllipse(circle:Shape, x:int, y:int):void
  {
   circle.graphics.beginFill(0xffffff);
   if(i % 2 == 0)
   {
    r = 2.5;
   }
   else if(i % 7 || i % 3 == 0)
   {
    r = 2;
   }
   else
   {
    r = 1.5;
   }
   circle.graphics.drawCircle(x,y,r);
   circle.graphics.endFill();
   addChild(circle);
  }
  
  private function onEnterFrame(event:Event):void
  {
   for(i = 0;i < NUM;i++)
   {
    if(i % 5 == 0)
    {
     snow_list[i].y += 1;
     snow_list[i].x += 2*Math.cos(snow_list[i].y/20.0);
    }
    else if(i % 3 == 0)
    {
     snow_list[i].y += 2;
     snow_list[i].x -= 0.005*snow_list[i].y; 
    }
    else if(i % 7 == 0 )
    {
     snow_list[i].y += 2;
     snow_list[i].x += 0.005*snow_list[i].y; 
    }
    else
    {
     snow_list[i].y += 1.5;
    }
    if(snow_list[i].x + r >= stage.stageWidth)
    {
     snow_list[i].x =  Math.random()*960;
     snow_list[i].y = 1;
    }
    if(snow_list[i].y + r >= stage.stageHeight)
    {
     snow_list[i].y = 1;
    }
    if(snow_list[i].x <= 0)
    {
     snow_list[i].x = stage.stageWidth;
    }
   }
  }

 }
}

欢迎大家拍砖

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值