as3使用scrollRect来实现内容滚动

 

以前一直用sprite套sprite实现滚动......今天发一个清爽一点的....

package {

    

    import flash.display.Sprite;

    import flash.geom.Rectangle;

    import flash.events.MouseEvent;

    import flash.events.Event;

    

    public class SampleScrollRect extends Sprite {

        private var circle : Sprite;

        private var slider : Sprite;

        

        public function SampleScrollRect() {

            circle = new Sprite();

            circle.graphics.beginFill(0xFFCC00);

            circle.graphics.drawCircle(200, 200, 200);

            circle.scrollRect = new Rectangle(0, 0, 200, 200);

            addChild(circle);

            

            slider = new Sprite();

            slider.graphics.beginFill(0xff0000);

            slider.graphics.drawRect(0, 0, 10, 30);

            slider.graphics.endFill();

            addChild(slider);

            slider.x = 300;

            

            slider.addEventListener(MouseEvent.MOUSE_DOWN, _down);

            slider.addEventListener(MouseEvent.MOUSE_UP, _up);

        }

        

        private function _down(e : MouseEvent) : void {

            stage.addEventListener(MouseEvent.MOUSE_MOVE, _move);

            stage.addEventListener(MouseEvent.MOUSE_UP, _up);

            

        }

        

        private function _up(e : MouseEvent) : void {

            stage.removeEventListener(MouseEvent.MOUSE_MOVE, _move);

            stage.removeEventListener(MouseEvent.MOUSE_UP, _up);

            

        }

        

        private function _move(event : MouseEvent) : void {

            

            var rect : Rectangle = circle.scrollRect;

            trace(mouseY);

            rect.y = mouseY;

            slider.y = mouseY;

            trace(rect);

            circle.scrollRect = rect;

        }

    }

}

 

 

 

你可能感兴趣的:(scroll)