其实我这人逻辑能力并不强,所以只能在大概了解有些什么类型的项目之后提前把所有需要用到的功能全都写一遍,这样我才能安心。

  这次的事3D图片墙,以前没有写过,只能先写一次了。

  代码如下:

模型类

 

package  {
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.MouseEvent;
    
    public class MyModel extends Sprite{
        private var bricks:Array=[];//brick砖头   储存砖头的数组
        
        private var num:int;
        
        public static const PicWidth:int=180;//单张图片的宽
        public static const PicHeight:int=135;//单张图片的高
        //public static const Radius:int=125;//半径
        public static const Radius:int=120;//半径
        
        public static const ROW:int=2;//两行
        public static const COLUMN:int=15;//每行15个图片
        public static const totalNum:int=ROW*COLUMN;//图片总数量
        public static const Radian:Number=360/COLUMN*Math.PI/180;//弧度
        public function MyModel() {
            // constructor code
            addBrick();
            addChild(addSp(100,100,0.3));
        }
        private function addBrick():void{
            for(var i:int=0;i<30;i++){
                var sp:Sprite=addSp(PicWidth,PicHeight);
                pushThumb(sp);
            }
        }
        //载入缩览图后将其压入模型中
        public function pushThumb(obj:DisplayObject):void{
            var i:int=num/COLUMN;
            var j:Number=Radian*(num%COLUMN-COLUMN/2+0.5);

            var brick:Sprite=new Sprite();//每一块砖
            brick.rotationY=j*180/Math.PI;
            brick.z=-5.0*Radius*(Math.cos(j));
            brick.x=-5.0*Radius*Math.sin(j);
            brick.y=(i-1.5)*(PicHeight+20);
            brick.name=String(num);
            
            obj.x=-PicWidth/2;
            obj.y=-PicHeight/2;
            brick.addChild(obj);
            bricks[num]=brick;
            num++;
            addChild(brick);
            brick.addEventListener(MouseEvent.CLICK,onC);
        }
        private function onC(e:MouseEvent):void{
            e.target.alpha=0.4;
        }
        private function addSp(w:int=0,h:int=0,alp:Number=1,clr:int=0):Sprite{
            var sp:Sprite=new Sprite()
            sp.graphics.beginFill(clr,alp)
            sp.graphics.drawRect(0,0,w,h)
            sp.graphics.endFill()
            //sp.name=str
            return sp
        }

    }
    
}

控制类

package  {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import com.greensock.TweenLite;
    import com.greensock.easing.Back;
    
    public class MyControl  extends Sprite{
        private var model:MyModel=new MyModel();
        
        private var oldX:Number=0;
        private var oldY:Number=0;
        private var nowX:Number=0;
        private var nowY:Number=0;
        private var isDraging:Boolean=false;//是否按下
        
        
        private var oldRotationY:int=0;
        private var speed:int=0;
        private var acceleration:Number=0.2;//加速度
        public function MyControl() {
            // constructor code
            this.addEventListener(Event.ADDED_TO_STAGE,tos);
        }
        private function tos(e:Event):void{
            this.removeEventListener(Event.ADDED_TO_STAGE,tos);
            
            addChild(model);
            model.x=400;
            model.y=400;
            model.z=-140;
            //stage.addEventListener(MouseEvent.CLICK,onLC);
            //stage.addEventListener(MouseEvent.RIGHT_CLICK,onRC);
            this.addEventListener(Event.ENTER_FRAME,onFrame);
            trace(MyModel.Radian);
            trace(MyModel.Radian*180/Math.PI);
            
            model.addEventListener(MouseEvent.MOUSE_DOWN,onD);
            //stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
            stage.addEventListener(MouseEvent.MOUSE_UP,onU);
        }
        //鼠标按下
        private function onD(e:MouseEvent):void{
            nowX=stage.mouseX;
            nowY=stage.mouseY;
            oldX=stage.mouseX;
            oldY=stage.mouseY;
            isDraging=true;
        }
        //鼠标抬起
        private function onU(e:MouseEvent):void{
            isDraging=false;
        }
        //单击鼠标左键顺时针旋转
        private function onLC(e:MouseEvent):void{
            trace("d");
            //TweenLite.to(model,2,{rotationY:model.rotationY+24,ease:Elastic.easeOut});
            TweenLite.to(model,1,{rotationY:model.rotationY+24,ease:Back.easeOut});
        }
        //单机鼠标右键逆时针旋转
        private function onRC(e:MouseEvent):void{
            trace("rd");
            TweenLite.to(model,1,{rotationY:model.rotationY-24,ease:Back.easeOut});
        }
        private function onFrame(e:Event):void{
            //model.rotationY+=1;
            //trace(stage.mouseX,stage.mouseY);
            if(isDraging==true){
                nowX=stage.mouseX;
                nowY=stage.mouseY;
                if(oldX-nowX>5){
                    model.rotationY-=((oldX-nowX)/20);
                }
                else if(nowX-oldX>5){
                    model.rotationY+=((nowX-oldX)/20);
                }
                oldX=nowX;
                oldY=nowY;
                
                speed=(model.rotationY-oldRotationY)/2;
                
                oldRotationY=model.rotationY;
            }
            else{
                model.rotationY+=speed;
                
                var afterSpeed:Number = Math.abs(speed) - acceleration;
                if (afterSpeed == 0 ){
                    this.removeEventListener(Event.ENTER_FRAME, onFrame);
                }
                
                if(afterSpeed<0){
                    afterSpeed=0;
                }
                speed = speed >= 0?afterSpeed: -afterSpeed;
                trace("s"+speed);
            }
        }

    }
    
}

 

posted on 2017-11-06 17:16  凌落成迷  阅读(434)  评论(0编辑  收藏  举报