Ok, i did a quick scroller for a project and for the very first time i added the functionality where the scroll button changes size based on how much it has to scroll, so it works more like a normal window scroller. The problem i ran into was the rectangle i was using to constrain the drag was only acknowledging the default size of the button. The button is placed in the fla, i’m not creating it at run time but thinking about giving that a try. It’s really simple, just a movieclip with another movieclip in it, i’m changing the size of the child movie clip and the parent movieclip is what is dragged. So as the button changes size the rectangle still only looks at the original size of the button. Very odd. I put in a check on enter frame to put a bandaid on the problem which works but not very elegant.
Love to know if anyone else has seen this issue and if you found a solution.
Thank you!
package site.view.components
{
import com.greensock.TweenLite;
import com.utils.ButtonEvent;
import site.events.SiteEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class ScrollerComponent extends MovieClip
{
public var scroller : MovieClip;
public var scrollDistance : int;
private var dragging : Boolean;
private var scrollBtn : MovieClip;
private var dragRect : Rectangle;
public function ScrollerComponent () {}
public function init ():void
{
scrollBtn = scroller.scrollBtn_mc;
setListeners ();
}
public function destroy ( e:Event ):void
{
removeListeners();
scroller = null;
}
private function scrollRange ( _height:Number = undefined ):int
{
return scrollDistance - ( _height || scroller.scrollBtn_mc.height);
}
public function updateScroller ( _ratio:Number, _btnHeight:Number = undefined ):void
{
var btnHeight:Number = scrollDistance * _btnHeight;
var ypos:Number = scrollRange ( btnHeight ) * _ratio;
TweenLite.to ( scroller.scrollBtn_mc,.3, {y:ypos});
TweenLite.to ( scroller.scrollBtn_mc.bg_mc,.3, {height:btnHeight});
}
private function updateView( e:Event ):void
{
checkBtnPos ();
var pos:Number = scroller.scrollBtn_mc.y / scrollRange ();
dispatchEvent( new SiteEvent( SiteEvent.ITEM_DRAGGING, {pos:pos}));
}
private function checkBtnPos ():void
{
if ( scrollBtn.y + scrollBtn.height > scrollDistance )
{
scrollBtn.y = scrollDistance - scrollBtn.height;
}
}
public function show():void
{
TweenLite.to ( scroller,.3, {autoAlpha:1});
}
public function hide():void
{
TweenLite.to ( scroller,.3, {autoAlpha:0});
}
private function btnEvent ( e:MouseEvent ):void
{
switch (e.type) {
case ButtonEvent.ROLL_OVER:
case ButtonEvent.DRAG_OVER:
rollOver ();
break;
case ButtonEvent.DRAG_OUT:
case ButtonEvent.ROLL_OUT:
rollOut ();
break;
case ButtonEvent.PRESS:
btnPressed ();
break;
case ButtonEvent.RELEASE:
case ButtonEvent.RELEASE_OUTSIDE:
btnReleased ();
break;
}
dispatchEvent ( e );
}
private function btnReleased():void
{
removeEventListener( Event.ENTER_FRAME, updateView );
dragging = false;
scroller.scrollBtn_mc.stopDrag();
}
private function btnPressed():void
{
dragging = true;
addEventListener( Event.ENTER_FRAME, updateView );
dragRect = new Rectangle( 0, 0, 0, scrollDistance );
scroller.scrollBtn_mc.startDrag( false, dragRect );
}
public function rollOver ():void
{
TweenLite.to ( scroller.scrollBtn_mc, .3, {tint:0x000000} );
}
public function rollOut ():void
{
TweenLite.to ( scroller.scrollBtn_mc, .3, {removeTint:true} );
}
private function setListeners ():void
{
ButtonEvent.makeButton ( scrollBtn, true );
if(scrollBtn)
{
scrollBtn.buttonMode = true;
scrollBtn.mouseChildren = false;
scrollBtn.addEventListener (ButtonEvent.ROLL_OVER, btnEvent);
scrollBtn.addEventListener (ButtonEvent.DRAG_OVER, btnEvent);
scrollBtn.addEventListener (ButtonEvent.DRAG_OUT, btnEvent);
scrollBtn.addEventListener (ButtonEvent.ROLL_OUT, btnEvent);
scrollBtn.addEventListener (ButtonEvent.PRESS, btnEvent);
scrollBtn.addEventListener (ButtonEvent.RELEASE_OUTSIDE, btnEvent);
scrollBtn.addEventListener (ButtonEvent.RELEASE, btnEvent);
scrollBtn.addEventListener (MouseEvent.CLICK, btnEvent);
}
addEventListener (Event.REMOVED_FROM_STAGE, destroy );
}
private function removeListeners ():void
{
if(scrollBtn)
{
scrollBtn.removeEventListener (ButtonEvent.ROLL_OVER, btnEvent);
scrollBtn.removeEventListener (ButtonEvent.DRAG_OVER, btnEvent);
scrollBtn.removeEventListener (ButtonEvent.DRAG_OUT, btnEvent);
scrollBtn.removeEventListener (ButtonEvent.ROLL_OUT, btnEvent);
scrollBtn.removeEventListener (ButtonEvent.PRESS, btnEvent);
scrollBtn.removeEventListener (ButtonEvent.RELEASE_OUTSIDE, btnEvent);
scrollBtn.removeEventListener (ButtonEvent.RELEASE, btnEvent);
scrollBtn.removeEventListener (MouseEvent.CLICK, btnEvent);
}
removeEventListener (Event.REMOVED_FROM_STAGE, destroy );
}
}
}