海底世界

      做生活 2007-7-25 12:30

制造一个水波的效果主要可以使用sin()或cos()产生的波形用公式转化,用惯了director的copyPixels再用as3是有点不习惯的,总结几点
1、要使用alphaBitmapData实现maskImage,blendLevel 比如:
var alphaimg:BitmapData = new BitmapData(400, 300, true, 0xff000000);
buffer.copyPixels(bmp,rects,pts,alphaimg,null,true);
2、使用matrix实现rectangle or quad to rectangle;
3、as3里面可以使用generateFilterRect更容易达到更多的视觉效果;

例子如下:

代码如下:

package {
  import flash.display.Sprite;
  import flash.display.Loader;
  import flash.display.LoaderInfo;
  import flash.events.Event;
  import flash.net.URLRequest;
  import flash.display.Bitmap;
  import flash.display.BitmapData;
  import flash.geom.Point;
  import flash.geom.Rectangle;
  public class water extends Sprite {    
    var bmp:BitmapData = new BitmapData(400, 300, false, 0x000000);
    var buffer:BitmapData = new BitmapData(400, 300, false, 0x000000);
    var stagepic:Bitmap;
    var simg:Bitmap;
    var maxrects:int;
    var r_h:Number;
    var rect_h:Number;
    var rect_y:Number;
    var step:Number;
    public function water() {  
      var sloader:Loader = new Loader();  
      sloader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
      sloader.load(new URLRequest("http://blog.5d.cn/images/zq02.jpg"));
    }
    function completeHandler(event:Event):void {
      simg = event.currentTarget.content as Bitmap;
      stagepic =new Bitmap(bmp);
      addChild(stagepic);
      addEventListener(Event.ENTER_FRAME,onEnterFrame);
    }    
    private function onEnterFrame(event:Event):void {
      maxrects=Math.random()*300;
      r_h=300/maxrects;
      step=Math.random();      
      bmp.draw(simg);
      rect_y=0;
      for (var i:int=1; i < maxrects+1; i++) {
        if (i%2==0){
          rect_h=r_h*((1+Math.sin(step))/1)
        }else{
          rect_h=r_h*((1+Math.sin(Math.PI/2+step))/1)      
        }
        var rects:Rectangle = new Rectangle(0, rect_y,400, rect_h);    
        var pts:Point = new Point(0, rect_y);
        buffer.copyPixels(bmp,rects,pts);
        rect_y=rect_y+rect_h;  
      }  
      bmp.draw(buffer);
    }
  }
}

标签集:TAGS:flash example as3
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}