利用 scrollrect滾動場景

 

下面是完整的代碼,就不多說廢話了。

package {

    import com.greensock.layout.ScaleMode;

    import com.greensock.loading.ImageLoader;

 

    import flash.display.DisplayObject;

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    import flash.geom.Point;

    import flash.geom.Rectangle;

 

    public class scrollrectTest extends Sprite {

        private var ldr:ImageLoader;

        private var _mousePosition:Point;

        private var _scenePosition:Point;

        private var left:int = 0;

        private var top:int = 0;

 

        public function scrollrectTest() {

            ldr = new ImageLoader('IMG_0943.JPG', {

                name:'image',

                container: this,

                width: 800, height: 600,

                scaleMode : ScaleMode.PROPORTIONAL_INSIDE,

                requireWithRoot : root

            });

            ldr.load();

            this.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);

            this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

            this.addEventListener(MouseEvent.MOUSE_UP, mouseUp);

        }

 

        private function mouseUp(event:MouseEvent):void {

            this._mousePosition = null;

        }

 

        private function mouseDown(event:MouseEvent):void {

            this._mousePosition = new Point(event.stageX, event.stageY);

        }

 

        private function mouseMove(event:MouseEvent):void {

            var pt:Point;

            var child:DisplayObject;

            child = this.getChildByName("image");

            if (event.buttonDown && _mousePosition) {

                pt = new Point(event.stageX, event.stageY);

                pt = pt.subtract(this._mousePosition);

                _mousePosition = new Point(event.stageX, event.stageY);

                left = left - pt.x;

                top = top - pt.y;

                if (left < 0) {

                    left = 0;

                }

                if (top < 0) {

                    top = 0;

                }

                if (left + stage.stageWidth > child.width) {

                    left = child.width - stage.stageWidth;  

                }

                if (top + stage.stageHeight > child.height) {

                    top = child.height - stage.stageHeight;  

                }

 

                this.scrollRect = new Rectangle(this.left, this.top, this.width, this.height);

            }

        }

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章