/ Published in: ActionScript 3

This may not be useful to anyone else as is, but I wanted to keep my typical scrolling code somewhere for easy access. This uses TweenMax as well which obviously isn't included. This assumes you have content_mc for the stuff to be scrolled, scrollArrow as your dragger, scrollerTrack as the bar the dragger follows, and contentMask to mask your content within a certain area.
Expand |
Embed | Plain Text
// grab its top position before we scroll anything private var contentStart:Number = content_mc.landPort.content_mc.y; private var dragRatio:Number; // add the mouse_down to the scrollArrow and add the mouse_up to the stage scrollArrow.addEventListener (MouseEvent.MOUSE_DOWN, drag); this.addEventListener (MouseEvent.MOUSE_UP, dragOff); private function drag (evt:Event):void { // figure out how far the content needs to move for each pixel the scrollArrow moves dragRatio = (content_mc.height-contentMask.height) / (scrollerTrack.height-scrollArrow.height); // define where the scrollArrow can move within var rectangle:Rectangle = new Rectangle(scrollArrow.x, scrollerTrack.y, 0, scrollerTrack.height-scrollArrow.height); // listen for scrolling and start dragging this.addEventListener (MouseEvent.MOUSE_MOVE, dragging); evt.target.startDrag (false, rectangle); } private function dragging (evt:Event):void { // figure out where the content needs to scroll to and tween it var nextPos:Number = contentStart-((scrollArrow.y-contentStart)*dragRatio); TweenMax.to(content_mc, .5,{y:nextPos}); } private function dragOff (evt:Event):void { // quit yo draggin evt.target.stopDrag (); this.removeEventListener (MouseEvent.MOUSE_MOVE, dragging); }
You need to login to post a comment.