as 烟雾效果的核心代码

作者:袖梨 2022-06-28
 代码如下 复制代码

package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;

      public class FlashTest extends Sprite {
        public function FlashTest(){
            w = 200;
            vectormap = new BitmapData(w, w);

            bmpdata = new BitmapData(w, w, false, 0);
            addChild(new Bitmap(bmpdata));

            stage.addEventListener(Event.ENTER_FRAME, reset);

        }

        private var vectormap:BitmapData;
        private var bmpdata:BitmapData;
        private var oy:int = 0;
        private var w:int;

        private function reset(e:Event):void {
            vectormap.perlinNoise(w * .5, w * .5, 3, 1, false, true, 7, false, [new Point(0, oy++)]);

            bmpdata.fillRect(bmpdata.rect, 0);

            for (var y:int = 0; y < vectormap.height; y++){
                for (var x:int = 0; x < vectormap.width; x++){
                    var px:uint = vectormap.getPixel(x, y);
                    var r:int = px >> 16 & 0xFF;
                    var g:int = px >> 8 & 0xFF;
                    var b:int = px & 0xFF;

                    var tx:int = x + (g / 0xFF - .5) * w;
                    var ty:int = y + (b / 0xFF - .5) * w;

                    if (ty < 0 || ty >= bmpdata.height || tx < 0 || tx >= bmpdata.width)
                        continue;
                    var col:uint = bmpdata.getPixel(tx, ty);
                    col += 0x010101 * (int)(r * .2);
                    if (col > 0xFFFFFF)
                        col = 0xFFFFFF;
                    bmpdata.setPixel(tx, ty, col);
                }
            }

        }
    }
}

相关文章

精彩推荐