最近在学习flash,自己用AS3.0写了一个滚动条,不是用组件的
import app.view.ui.friend.FriendsColumn;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import com.iceengine.interfaces.IProcess;
import com.iceengine.input.IceInput;
import com.iceengine.core.Processer;
internal class FriendScrollBar extends IceSprite {
private var _friendsColumn:FriendsColumn=null;
private var _friends:Array = [];
private var _page:int=0;
private var _topBtn:SimpleButton = null;
private var _bottomBtn:SimpleButton = null;
private var _upBtn:SimpleButton = null;
private var _downBtn:SimpleButton = null;
public var _slider:SimpleButton = null;
public var _scrollBar:SimpleButton = null;
private var _isDown:Boolean = false;
public function FriendScrollBar (mc:DisplayObject, friendsColumn:FriendsColumn, parent:DisplayObjectContainer=null, initX:Number=0,initY:Number=0){
super(mc, parent);
_friendsColumn = friendsColumn;
_mc.x = initX;
_mc.y = initY;
_topBtn = _mc.topBtn;
_bottomBtn = _mc.bottomBtn;
_upBtn = _mc.upBtn;
_downBtn = _mc.downBtn;
_slider = _mc.slider ;
_scrollBar = _mc.bar;
_friends = _friendsColumn._friendList;
initListeners();
// Processer.addUpdateFunc(_update);
}
private function _update():void {
if(_isDown){
if(_slider.y >_scrollBar.y +_scrollBar.height)
{
_slider.y = _scrollBar.y +_scrollBar.height;
}
else if(_slider.y< _scrollBar.y){
_slider.y = _scrollBar.y;
}
else{_slider.y = mouseY-64;}
// _slider.removeEventListener(MouseEvent.MOUSE_DOWN,_sliderMouseDown);
_friendsColumn._start =Math.max(0,(_friendsColumn._friendList.length/_scrollBar.height)*(_slider.y - _scrollBar.y));
_friendsColumn.showFriendItem(_friendsColumn._friendList,_friendsColumn._start);
if(IceInput.mouseUp) {
_slider.addEventListener(MouseEvent.MOUSE_DOWN, _sliderMouseDown);
_isDown = false;
}
}
}
public function turn(param1:int) : void
{
var _rows:* = 8; // Config.getNumber("flash.friendList.blocks.cols");
this.shift(param1 * _rows);
return;
}// end function
public function shift(param1:int) : void
{
var _rows:* = 8 ; //Config.getNumber("flash.friendList.blocks.cols");
var _startIndex:* = Math.max(0, _friendsColumn._friendList.length - _rows);
_friendsColumn._start = Math.max(0, Math.min(_startIndex, _friendsColumn._start + param1));
_friendsColumn.showFriendItem(_friendsColumn._friendList,_friendsColumn._start);
if(_friendsColumn._friendList.length-_friendsColumn._start >7 ){
_slider.y =(_friendsColumn._start/_friendsColumn._friendList.length)*_scrollBar.height+_scrollBar.y
}
return;
}// end function
public function get pages() : int
{
var _rows:* = 8; //Config.getNumber("flash.friendList.blocks.cols");
return Math.ceil(this._friends.length / _rows);
}// end function
override protected function init(e:Event=null):void {
super.init();
}
override protected function end(e:Event=null):void {
super.end();
}
private function _sliderMouseDown(event:MouseEvent):void {
event.stopPropagation();
_slider.removeEventListener(MouseEvent.MOUSE_DOWN, _sliderMouseDown);
stage.addEventListener(MouseEvent.MOUSE_MOVE,_sliderMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP,_sliderMouseUp);
_isDown = true;
}
private function _sliderMouseMove(event:MouseEvent):void {
if(mouseY - 64 >_scrollBar.y+_scrollBar.height){
_slider.y = _scrollBar.y+_scrollBar.height-13;
}
else if(mouseY - 64 <_scrollBar.y){
_slider.y = _scrollBar.y;
}
else{
_slider.y = mouseY-64;
}
_friendsColumn._start =Math.max(0,(_friendsColumn._friendList.length/_scrollBar.height)*(_slider.y - _scrollBar.y));
_friendsColumn.showFriendItem(_friendsColumn._friendList,_friendsColumn._start);
}
private function _sliderMouseUp(event:MouseEvent):void {
_slider.addEventListener(MouseEvent.MOUSE_DOWN, _sliderMouseDown);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,_sliderMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP,_sliderMouseUp);
_isDown = true;
}
private function scrollBarClick(event:MouseEvent):void {
_slider.y = mouseY-64;
_friendsColumn._start =Math.max(0,(_friendsColumn._friendList.length/_scrollBar.height)*(_slider.y - _scrollBar.y));
// trace("_start="+_start);
_friendsColumn.showFriendItem(_friendsColumn._friendList,_friendsColumn._start);
}
private function initListeners() : void{
_topBtn.addEventListener(MouseEvent.CLICK, function () :void{turn(-1);});
_bottomBtn.addEventListener(MouseEvent.CLICK, function () :void{turn(1);});
_upBtn.addEventListener(MouseEvent.CLICK, function () :void{shift(-1);});
_downBtn.addEventListener(MouseEvent.CLICK, function () :void{shift(1);});
_scrollBar.addEventListener(MouseEvent.CLICK,scrollBarClick);
_slider.addEventListener(MouseEvent.MOUSE_DOWN,_sliderMouseDown);
}
}