注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

热血易冷

宝剑锋从磨砺出,梅花香自苦寒来。

 
 
 

日志

 
 

reflections倒影类(flash AS3)  

2011-06-16 13:49:05|  分类: flash ActionScri |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

var refl :Reflection = new Reflection(mc, 150, 0.5, 10);//显示对象,渐变参数(0~255),透明参数(0~1),倒影y相对偏移量
stage.addChild( refl );

stage.addEventListener(MouseEvent.CLICK,eventHandler);
function eventHandler(evt:MouseEvent):void {
    //删除倒影
    stage.removeChild( refl );
    refl.kill();
}



Reflection class:

程序代码

package {
    import flash.display.*;
    import flash.geom.*;
    import flash.events.*;

    public class Reflection extends Sprite {
        //publics:
        public var reflectedClip      :DisplayObjectContainer;
        public var reflectionHeight   :Number;
        public var reflectionStrength :Number;
        public var reflectionOffsetY  :Number;

        //privates:
        private var reflectionHolder  :Sprite;
        private var bm_reflection     :Bitmap;
        private var refMask           :Sprite;

        //////////////////////////////////////////////
        //               CONSTRUCTOR:
        //////////////////////////////////////////////
        public function Reflection( reflectedClip:DisplayObjectContainer, height:Number = 255, strength:Number = 1, reflectionOffsetY:Number = 1 ) {
            this.reflectedClip       = reflectedClip;
            this.reflectionHeight    = height;
            this.reflectionStrength  = strength;
            this.reflectionOffsetY   = reflectionOffsetY;
            this.addEventListener(Event.ENTER_FRAME, render);
            build();
        }
       
        //////////////////////////////////////////////
        //               RENDER FUNCTIONS:
        //////////////////////////////////////////////
        private function render(l_loader:Event) : void
        {
            bm_reflection.bitmapData.fillRect(bm_reflection.getRect(bm_reflection), 16777215);
            bm_reflection.bitmapData.draw(reflectedClip);
            return;
        }// end function
       
        //////////////////////////////////////////////
        //               BUILDER FUNCTIONS:
        //////////////////////////////////////////////
        private function build():void {
            buildReflection();
            attachItems();
            positionElements();
        }

        private function buildReflection():void {
            reflectionHolder = new Sprite();

            var bmd:BitmapData = new BitmapData( reflectedClip.width, reflectedClip.height, true, 0xc61916 );
            bmd.draw( reflectedClip );

            bm_reflection             = new Bitmap( bmd );
            bm_reflection.y           = bm_reflection.height;
            bm_reflection.x           = 0;
            bm_reflection.rotation    = 180;
            bm_reflection.scaleX      = -1;
            bm_reflection.alpha       = reflectionStrength;

            refMask                   = new Sprite();
            var fillType      :String = GradientType.LINEAR;
            var colors        :Array  = [0xFFFFFF, 0x0000FF];
            var alphas        :Array  = [100, 0];
            var ratios        :Array  = [0, reflectionHeight];
            var matr          :Matrix = new Matrix();
            var spreadMethod  :String = SpreadMethod.PAD;

            matr.createGradientBox(reflectedClip.height * .9, reflectedClip.width, 0,0, 0);
            refMask.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
            refMask.graphics.drawRect(0, 0, reflectedClip.height, reflectedClip.width);

            refMask.y                        = 0;
            refMask.x                        = bm_reflection.width;
            refMask.rotation                 = 90;

            bm_reflection.cacheAsBitmap      = true;
            refMask.cacheAsBitmap            = true;
            bm_reflection.mask               = refMask;
        }

        //////////////////////////////////////////////
        //        ATTACHER & POSITION FUNCTIONS:
        //////////////////////////////////////////////
        private function attachItems():void {
            this.addChild( reflectionHolder );
            reflectionHolder.addChild( bm_reflection );
            reflectionHolder.addChild( refMask );
        }

        private function positionElements():void {
            this.x = reflectedClip.x;
            this.y = reflectedClip.y + reflectedClip.height + reflectionOffsetY;
        }

        //////////////////////////////////////////////
        //               KILL FUNCTION:
        //////////////////////////////////////////////
        public function kill():void {
            reflectionHolder.removeChild( refMask );
            reflectionHolder.removeChild( bm_reflection );
            this.removeChild( reflectionHolder );
            this.removeEventListener(Event.ENTER_FRAME, render);
        }
    }
}

  评论这张
 
阅读(453)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017