Brian Chia
       
 
categories
actionscript
actionscript 1.0
actionscript 2.0
misc
 
processing
 
links
brian chia
kelvin zhao
ronald leong
 
 
Scroller v01
 
Instead of using the common method of using a mask to mask out the content area and shift the whole content area. This post show how we can implement a scrolling using scrollRect. The result will be the same, in fact it will be faster.

ScrollRect property allow us to scroll movie clip content quickly. Text fields and complex content scroll much faster as pixel level copying is used to scroll the data instead of regenerating the entire movie clip. To enhance the performace, use scrollRect together with cacheAsBitmap.

For more information on scrollRect and cacheAsBitmap, refer to http://mxdj.sys-con.com/read/142694_1.htm or Optimising Flash Movie Frame Rates.


import flash.geom.Rectangle;

// set the scrollbar content width and height
var sWidth:Number=350;
var sHeight:Number=250;

// set allowance at the end of the scroll
var allowance:Number=3;

// declare the scrollRect area using the Rectangle class
var rect:Rectangle=new Rectangle(0,0,sWidth,sHeight);

// calculate the content height & add allowance to it
var cHeight:Number=Math.ceil(content_mc._height)+allowance;
scroll_mc._height=Math.floor(sHeight/cHeight*sHeight); // set the scrollbar height

// bitmap caching for content_mc
content_mc.cacheAsBitmap=true;

var top:Number=0;
var btm:Number=sHeight-scroll_mc._height;

scroll_mc.onPress=function():Void{

this.startDrag(false,this._x,top,this._x,btm);
this.onMouseMove=function():Void{

updateAfterEvent();

}

}

scroll_mc.onRelease=scroll_mc.onReleaseOutside=function():Void{

this.stopDrag();
this.onMouseMove=undefined;

}

scroll_mc.onEnterFrame=function():Void{

// calculate percentage of the scroll content
var percent:Number=(this._y-top)/(btm-top);

// set a new scrollRect rectangle
rect.y=percent*(cHeight-sHeight);
content_mc.scrollRect=rect;

}

scroll_mc.useHandCursor=false;



Download Fla

suggestion

 
 
 
         
  © copyright 2007 THINKLUNATIC.    
Brian Chia Jonathan Ng