這是之前 iammic 寫的一個 Flash 的全景效果,當製作完成一張全景圖後,將圖片切割成2或4

張相同長寬的圖片進行 Flash 全景程式製作。在 Flash 全景程式中按住滑鼠左鍵,向左或右拖

動,圖片會向拖動的方向進行旋轉。

Flash 全景效果:

Flash 程式:

package 
{
    import flash.display.Bitmap;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.PixelSnapping;
    import flash.events.*;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.utils.Timer;
    import flash.utils.getDefinitionByName;
    
    
    
    [SWF(width=200,height=200,backgroundColor="#ffffff")]
    public class Main extends Sprite 
    {
        [Embed(source = '../Lib/test.jpg')]
        private var Pano1:Class; 
        [Embed(source = '../Lib/test.jpg')]
        private var Pano2:Class;         
        
        private var img1:Bitmap = new Pano1();
        private var img2:Bitmap = new Pano2();        
        
        
        private var img:Bitmap;
        private var imgArray:Array = new Array(2);        
        private var time:Timer;                        
        private var offsetX:Number;     
        private var offsetY:Number;
        private var offsetImgX:Number;     
        private var offsetImgY:Number;     
        private var speedX:Number;
        private var speedY:Number;
        private var x_pos:Number; 
        private var y_pos:Number;
        private var mc:MovieClip = new MovieClip();            
        
        public function Main():void 
        {                    
            
            imgArray[0] = new Pano1();
            imgArray[1] = new Pano2();            
            
            for (var i:int = 0; i < imgArray.length; i++) {
                
                imgArray[i].width = Math.ceil(imgArray[i].width * (stage.stageHeight / imgArray[i].height));
                imgArray[i].height = stage.stageHeight;
                
                
                if(i==0){                
                    imgArray[i].x = 0;
                    imgArray[i].y = 0;                                        
                    
                }else if (i + 1 == imgArray.length) {                    
                    imgArray[i].x = -imgArray[i].width;
                    imgArray[i].y = 0;
                }else {                                                            
                    imgArray[i].x = imgArray[i-1].width*i;
                    imgArray[i].y = 0;
                }
                imgArray[i].smoothing = true;                
                mc.addChild(imgArray[i]);                
            }                                    
                        
            addChild(mc);
            
            
            //Drag 事件
            stage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
            stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
            
            
            
            //加入 Timer 事件
            time = new Timer(60);
            
        }
                        
        public function run(e:Event):void {
                        
            //全景圖左右移動
            if(offsetImgX > 20 ){
                
                for (var i:int = 0; i < imgArray.length; i++) {
                    
                    if (imgArray[i].x< -imgArray[i].width-(imgArray[i].width/5)) {
                        imgArray[i].x = imgArray[i].x + (imgArray[i].width * imgArray.length);            
                    }
                    
                        
                    x_pos = imgArray[i].x - speedX;
                    imgArray[i].x = x_pos;
                    
                    
                }             
                            
            }
            
            
            if (offsetImgX < -20) {
                                
                for (i= 0; i < imgArray.length; i++) {
                    
                    if (imgArray[i].x> imgArray[i].width-(imgArray[i].width/5)) {
                        imgArray[i].x = imgArray[i].x - (imgArray[i].width * imgArray.length);                
                    }
                    
                    x_pos = imgArray[i].x + speedX;
                    imgArray[i].x = x_pos;                    
                }                 
                            
            }
            
                                
        
        }
                
      
        public function startDragging(e:MouseEvent):void 
        { 
            
            offsetX = e.stageX - this.x;    
            offsetY = e.stageY - this.y;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCircle); 
        } 
 
        public function stopDragging(e:MouseEvent):void 
        { 
            removeEventListener(Event.ENTER_FRAME, run);        
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCircle); 
        }     
 
 
        public function dragCircle(e:MouseEvent):void 
        {     
            
            offsetImgX = e.stageX - offsetX;            
            offsetImgY = e.stageY - offsetY;            
            
            speedX = Math.floor(Math.sqrt (Math.abs ( offsetImgX )))+20;
            speedY = Math.floor(Math.sqrt (Math.abs ( offsetImgY )));
            
            addEventListener(Event.ENTER_FRAME, run);            
            
            e.updateAfterEvent(); 
        } 
        
        
 
    }
    
}

檔案下載:pano2.zip  ( FlashDevelop AS3 Project )

參考網站:

建立拖放互動

http://www.harlands.com/article.php?id=00049

iammic 發表在 痞客邦 PIXNET 留言(0) 人氣()