as3.0 电子相册

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Point;
    import flash.display.Shape;
    import flash.geom.Matrix;
    import flash.display.GradientType;
    import flash.events.MouseEvent;
    import flash.display.DisplayObject;
    import com.greensock.TweenLite;
    import com.greensock.easing.*;

    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.utils.getDefinitionByName
    import flash.utils.getQualifiedClassName
    import flash.display.StageDisplayState
 
    public class Main extends Sprite
    {
        private var num:uint;
        private var rx:Number = 1700;//每张图片的间隔(或者说所有图片的宽度)
        
        private var ry:Number = 0.8;//初始状态的时候图片的大小
        private var pics:Array = new Array();
        private var picslength:int;
        private var speed:Number;
        private var j:int = -1;
        private var picw:Number;
        private var pich:Number;
        private var picz:int = 2;
        private var stagew:Number;
        private var stageh:Number;
        private var scalex:Number;
        private var scaley:Number;
        private var displayobject1:DisplayObject;
        private var displayobject2:DisplayObject;

        private var clickCount:int = 0;
        private var thisMc:MovieClip
        private var stageMc:Stage
        private var emptyMc:MovieClip
        private var speedNum:Number=70000//图片左右循环的速度
        private var enlarge:Number=2.5//鼠标移动到图片上放大的倍数
        public function Main(_thisMc:MovieClip,_stageMc:Stage,_emptyMc:MovieClip):void
        {
            emptyMc=_emptyMc
            stageMc=_stageMc
            thisMc=_thisMc
            init();
            
            stageMc.displayState=StageDisplayState.FULL_SCREEN
        }
        private function init():void
        {
        //    var bitmapdata:pic2 = new pic2();
            
            stagew = stageMc.stageWidth / 2;
            stageh = stageMc.stageHeight / 2;
            num = 5;
            for(var i:int =1;i<=num;i++)
            {
        //    var bitmapdata:BitmapData= getChildByName("pic"+num) as BitmapData;
                
                var newClass:Class = getDefinitionByName("pic"+i) as Class;
var bitmapdata:BitmapData = new newClass();
                //trace(getDefinitionByName("pic"+i),i)
              com(bitmapdata);
            }
            stageMc.addEventListener(Event.ENTER_FRAME,onFrameHandler);
        }
        private function com(bmpdt:BitmapData):void
        {
            j++;
            var bitmap:Bitmap = new Bitmap(bmpdt);
            if ((j == 0))
            {
                picw = bitmap.width;
                pich = bitmap.height;
            }
            var topshape:Shape = new Shape();
            var spritecont:Sprite = new Sprite();
           emptyMc. addChild(spritecont);
            DrawInSprite(topshape,bitmap);
            topshape.x =  -  picw / 2;
            topshape.y =  -  pich / 2;
            spritecont.addChild(topshape);
        //    DrawRef(topshape);//倒影
         //   spritecont.addEventListener(MouseEvent.ROLL_OVER,roll_over);
           // spritecont.addEventListener(MouseEvent.ROLL_OUT,roll_out);
            spritecont.addEventListener(MouseEvent.CLICK,mouseclick);
 
            var obj:Object = {"spritecont":spritecont,"arg":j * Math.PI * 2 / num};
            pics.push(obj);
            spritecont.x = rx * Math.cos(obj.arg) + stagew;
            spritecont.y =  -  ry * Math.sin(obj.arg) + stageh;
            spritecont.z = 2500;
            spritecont.scaleX = spritecont.scaleY = spritecont.y - stageh + ry;
            if ((j == (num - 1)))
            {
                picslength = pics.length;
            }
        }
        private function roll_over(evt:MouseEvent):void
        {
            displayobject1 = evt.target as DisplayObject;
            scalex = evt.target.scaleX;
            scaley = evt.target.scaleY;
            evt.target.parent.setChildIndex(evt.target,evt.target.parent.numChildren - 1);
            TweenLite.to(evt.target,1,{scaleX:enlarge,scaleY:enlarge,ease:Back.easeOut});
        }
        private function roll_out(evt:MouseEvent):void
        {
            displayobject1 = null;
            displayobject2 = evt.target as DisplayObject;
            TweenLite.to(evt.target,1,{scaleX:getScale(displayobject2),scaleY:getScale(displayobject2),ease:Back.easeOut,onComplete:onFinishTween});
        }
        private function mouseclick(evt:MouseEvent):void
        {
            //待添加...
        }
        private function getScale(displayobject:DisplayObject):Number
        {
            if (Math.abs(speed) >= 0.04)
            {
                return 0;
            }
            else
            {
                return displayobject.y - stageh + ry;
            }
        }
        private function onFinishTween():void
        {
            displayobject2 = null;
        }
        private function DrawRef(p_source:DisplayObject):void
        {
            //对源显示对象做上下反转处理
            var bd:BitmapData = new BitmapData(p_source.width,p_source.height,true,0);
            var mtx:Matrix = new Matrix();
            mtx.d = -1;
            mtx.ty = bd.height;
            bd.draw(p_source,mtx);
            //生成一个渐变遮罩
            var width:int = bd.width;
            var height:int = bd.height;
            mtx = new Matrix();
            mtx.createGradientBox(width,height,0.5 * Math.PI);
            var shape:Shape = new Shape();
            shape.graphics.beginGradientFill(GradientType.LINEAR,[0xff0000,0xff0000],[0.5,0],[0,255],mtx);
            shape.graphics.drawRect(0,0,width,height);
            shape.graphics.endFill();
            var mask_bd:BitmapData = new BitmapData(width,height,true,0);
            mask_bd.draw(shape);
            //生成最终效果;
            bd.copyPixels(bd,bd.rect,new Point(0,0),mask_bd,new Point(0,0),false);
            //将倒影位图放在源显示对象下面
            var ref:Bitmap = new Bitmap();
            ref.x = p_source.x;
            ref.y = p_source.height / 2;
            ref.bitmapData = bd;
            p_source.parent.addChild(ref);
        }
        private function DrawInSprite(shape:Shape,bitmap:Bitmap):void
        {
            // 图片的描边
            //shape.graphics.lineStyle(1,0xffffff);
            shape.graphics.beginBitmapFill(bitmap.bitmapData);
            shape.graphics.drawRect(0,0,picw,pich);
            shape.graphics.endFill();
        }
        private function onFrameHandler(evt:Event):void
        {
            speed = (thisMc.mouseX - stagew)/speedNum;
            refreshIndex();
            refreshPosition();
        }
        private function refreshIndex():void
        {
            var gap:int = 1;
            var left:int = 0;
            var right:int = 0;
            var temp:Object;
 
            while ((gap <= picslength / 3))
            {
                gap = gap * 3 + 1;
            }
 
            while ((gap > 0))
            {
                for (right = gap; right < picslength; right++)
                {
                    temp = pics[right];
                    left = right;
                    if (displayobject1 != pics[right]["spritecont"])
                    {
                        while (((left > gap - 1) && Number(pics[left - gap]["spritecont"].y) >= Number(temp["spritecont"].y)))
                        {
                            pics[left] = pics[left - gap];
                            left -=  gap;
                        }
                        pics[left] = temp;
                    }
                }
                gap = (gap - 1) / 3;
            }
        }
        private function refreshPosition():void
        {
            for (var i:int = 0; i < picslength; i++)
            {
                var obj:Object = pics[i];
                var spritecont:Sprite = obj.spritecont as Sprite;
                obj.arg +=  speed;
                spritecont.x = rx * Math.cos(obj.arg) + stagew;
                spritecont.y =  -  ry * Math.sin(obj.arg) + stageh;
                if (displayobject1 != spritecont && displayobject2 != spritecont)
                {
                    spritecont.parent.setChildIndex(spritecont,i);
                    spritecont.scaleX = spritecont.scaleY = spritecont.y - stageh + ry;
                }
                if (displayobject2)
                {
                    displayobject2.parent.setChildIndex(displayobject2,displayobject2.parent.numChildren-1);
                }
                if (displayobject1)
                {
                    displayobject1.parent.setChildIndex(displayobject1,displayobject1.parent.numChildren-1);
                }
            }
        }
    }
}

 

posted on 2022-04-07 15:26  zqiang0803  阅读(55)  评论(0编辑  收藏  举报

导航