(if you're interested in the scrollBar not the question just download the file or copy/paste the code NO HARD FEELING AT ALL!
I have a (custom) SimpleScrollBar package/script that I found to be very usefull, it's based on Lee's tutorial and other pieces of code i found on the web.
Here's my question since i learned everything about as3 that i know from the web I was hoping someone could tell if my way of coding is bad, what i could do better/different, maybe more efficient ways of scripting, ...
Any hints/tips/comments are welcome...
This is a weird question perhaps but i need some feedback to see if i'm not following the wrong path.
So if it's not to much to ask take a look and let me know what i'm doing wrong or good.
Here's the script for people who dont like to download but prefer to copy/paste:
- Code: Select all
package
{
import flash.display.Sprite;
import flash.display.DisplayObjectContainer;
import flash.events.*;
import caurina.transitions.*;
public class SimpleScrollBar extends Sprite
{
private var _thumb:Sprite;
private var _track:Sprite;
private var _sb:Sprite;
private var _masker:Sprite;
private var _yOffset:Number;
private var _yMin:Number;
private var _yMax:Number;
private var _height:Number;
private var _value:Number;
private var _isScrolling:Boolean;
private var _added:Boolean = false;
private var _content:DisplayObjectContainer;
public function ScrollBar()
{
}
public function createScrollBar(c:DisplayObjectContainer, h:Number):void
{
_content = c;
_height = h;
init();
}
private function init():void
{
createMasker();
createSb();
addChild(_masker);
_sb.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
addChild(_sb);
}
private function addedHandler(e:Event):void
{
_sb.removeEventListener(Event.ADDED_TO_STAGE, updateSize);
_added = true;
updateSize();
_thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
}
private function thumbDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
_yOffset = mouseY - _thumb.y;
}
private function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
}
private function thumbMove(e:MouseEvent):void
{
_thumb.y = mouseY - _yOffset;
if(_thumb.y <= _yMin)
_thumb.y = _yMin;
if(_thumb.y >= _yMax)
_thumb.y = _yMax;
var sp:Number = _thumb.y / _yMax;
Tweener.addTween(_content, {y:_masker.y+(-sp*(_content.height-_masker.height)),
time:0.5});
e.updateAfterEvent();
}
private function createMasker():void
{
_masker = new Sprite();
_masker.graphics.beginFill(0x000000, 0.5);
_masker.graphics.drawRect(0, 0, 10, 10);
_masker.graphics.endFill();
}
private function createSb():void
{
_sb = new Sprite();
_thumb = createThumb();
_track = createTrack();
_sb.addChild(_track);
_sb.addChild(_thumb);
_thumb.buttonMode = true;
}
private function createThumb():Sprite
{
// Adjust graphics to what suits your project
var th:Sprite = new Sprite();
th.graphics.beginFill(0x000000);
th.graphics.drawRect(0, 0, 6, 6);
th.graphics.endFill();
return th;
}
private function createTrack():Sprite
{
// Adjust graphics to what suits your project
var tr:Sprite = new Sprite();
tr.graphics.beginFill(0x000000);
tr.graphics.drawRect(2, 0, 2, 100);
tr.graphics.endFill();
return tr;
}
public function updateSize():void
{
if (_added) {
_masker.x = _content.x;
_masker.y = _content.y;
_masker.width = _content.width;
_masker.height = _height;
_content.mask = _masker;
_sb.x = _masker.x + _masker.width + 10;
_sb.y = _masker.y;
_track.height = _masker.height;
_thumb.height = Math.ceil((_masker.height / _content.height) * _masker.height);
_yMin = 0;
_yMax = _track.height - _thumb.height;
if ((_thumb.y + _thumb.height) > _masker.height) _thumb.y = _masker.height - _thumb.height;
_thumb.y = (_content.height < _masker.height) ? 0 : _thumb.y;
_sb.visible = (_content.height > _masker.height);
_value = _masker.height - _thumb.height;
_content.y = _masker.y -((_content.height - _masker.height) * (_thumb.y / _value));
}
}
}
}
And here's an example on how you could use it:
- Code: Select all
import SimpleScrollBar;
var _ssb:SimpleScrollBar;
var contentArray:Array = [{c:cont, h:200}, {c:cont2, h:400}];
for (var i:int = 0; i < contentArray.length; i++){
_ssb = new SimpleScrollBar();
_ssb.createScrollBar(contentArray[i].c, contentArray[i].h);
addChild(_ssb);
}
The example has 2 displayObjectContainers.
Instance names: 'cont' & 'cont2'.
Thanks for taking a look. I also hope someone will be able to use/learn from it.
Else i shouldn't of have posted it here
Thanks or you're welcome whatever suits the situation
xxx
