欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flash AS3教程:动态文本滚动条

程序员文章站 2022-04-26 15:09:05
学AS3几个星期了,发一个我的小练习:动态文本滚动条 功能说明: 本版滚动条除了继续保持体积小(小于2K),界面容易修改,资源占用率小的优势外,主要有以下几点改进: ... 08-10-06...
学as3几个星期了,发一个我的小练习:动态文本滚动条
功能说明
本版滚动条除了继续保持体积小(小于2k),界面容易修改,资源占用率小的优势外,主要有以下几点改进:
    使用as3编写。 宽高动态指定。 增加滚动条背景点击事件。 消除了鼠标滚轮无法同时准确控制多个文本框的重大bug。

源文件下载
使用方法见源文件
为了方便懒人,直接帖一下代码:
package com.qoolu.component
{
import flash.events.mouseevent;
import flash.events.event;
import flash.display.simplebutton;
import flash.text.textfield;
import flash.display.sprite;
import flash.utils.gettimer;
import flash.geom.rectangle;
/**
* @author 寂寞火山:[url]http://www.huoshan.org[/url]
* @version v5 [08.3.15]
* 动态文本滚动条
*/
public class scrollbar extends sprite {
//=============本类属性==============
////接口元件
private var scrolltext : textfield;
private var scrollbar_sprite : sprite;
private var up_btn : simplebutton;
private var down_btn : simplebutton;
private var pole_sprite : sprite;
private var bg_sprite : sprite;
////初始数据
private var polestartheight : number;
private var polestarty : number;
private var totalpixels : number;
private var isselect : boolean;
////上下滚动按钮按钮下时间
private var puttime : number;
/**
* @param scrolltext_fc:被滚动的文本框
* @param scrollbarmc_fc:舞台上与本类所代理的滚动条元件
* @param height_fc:滚动条高
* @param width_fc:滚动条宽
*/
public function scrollbar(scrolltext_fc : textfield, scrollbarmc_fc : sprite, height_fc : uint = 0,width_fc : uint = 0) {
//——————滚动条_sprite,滚动条按钮和滑块mc,被滚动的文本域初始化
scrolltext = scrolltext_fc;
scrollbar_sprite = scrollbarmc_fc;
up_btn = simplebutton(scrollbar_sprite.getchildbyname("up_btn"));
down_btn = simplebutton(scrollbar_sprite.getchildbyname("down_btn"));
pole_sprite = sprite(scrollbar_sprite.getchildbyname("pole_mc"));
bg_sprite = sprite(scrollbar_sprite.getchildbyname("bg_mc"));

//——————可用性控制
pole_sprite.visible = false;
up_btn.enabled = false;
down_btn.enabled = false;

//——————其他属性初始化
bg_sprite.usehandcursor = false;
isselect = scrolltext.selectable;
if(height_fc == 0) {
bg_sprite.height = scrolltext.height;
}else {
bg_sprite.height = height_fc;
}
if(width_fc != 0) {
bg_sprite.width = width_fc 2;
pole_sprite.width = width_fc;
up_btn.width = up_btn.height = down_btn.width = down_btn .height = width_fc;
}
down_btn.y = bg_sprite.y bg_sprite.height - down_btn.height - 1;
polestartheight = math.floor(down_btn.y - up_btn.y - up_btn.height);
polestarty = pole_sprite.y = math.floor(up_btn.y up_btn.height);

//——————注册侦听器
//文本滚动与鼠标滚轮
scrolltext.addeventlistener(event.scroll, textscroll);
scrolltext.addeventlistener(mouseevent.mouse_wheel, mousewheel);
//上滚动按钮
up_btn.addeventlistener(mouseevent.mouse_down, upbtn);
up_btn.stage.addeventlistener(mouseevent.mouse_up, upbtnup);
//下滚动按钮
down_btn.addeventlistener(mouseevent.mouse_down, downbtn);
down_btn.stage.addeventlistener(mouseevent.mouse_up, downbtnup);
//滑块
pole_sprite.addeventlistener(mouseevent.mouse_down, polesprite);
pole_sprite.stage.addeventlistener(mouseevent.mouse_up, poleup);
//滑块背景点击
bg_sprite.addeventlistener(mouseevent.mouse_down, bgdown);
}
/**
* 文本滚动事件
*/
private function textscroll(event : event) : void {
//判断滑块儿是否显示,并根据文本内容多少定义滑块高度
if(scrolltext.maxscrollv != 1) {
pole_sprite.visible = true;
up_btn.enabled = true;
down_btn.enabled = true;
//定义一个高度因子,此因子随加载文本的增多,将无限趋向于1
var heightvar : number = 1 - (scrolltext.maxscrollv - 1) / scrolltext.maxscrollv;
//根据高度因子初始化滑块的高度
pole_sprite.height = math.floor(polestartheight * math.pow(heightvar, 1 / 3));
totalpixels = math.floor(down_btn.y - up_btn.y - up_btn.height - pole_sprite.height);
pole_sprite.y = math.floor(polestarty totalpixels * (scrolltext.scrollv - 1) / (scrolltext.maxscrollv - 1));
}else {
pole_sprite.visible = false;
up_btn.enabled = false;
down_btn.enabled = false;
}
}
/**
* 滑块滚动
*/
private function polesprite(event : mouseevent) : void {
//首先取消文本框滚动侦听,因为文本滚动的时候会设置滑块的位置,而此时是通过滑块调整文本的位置,所以会产生冲突
scrolltext.removeeventlistener(event.scroll, textscroll);
//监听舞台,这样可以保证拖动滑竿的时候,鼠标在舞台的任意位置松手,都会停止拖动
scrollbar_sprite.stage.addeventlistener(mouseevent.mouse_up, poleup);
//限定拖动范围
var dragrect : rectangle = new rectangle(pole_sprite.x, polestarty, 0, totalpixels);
pole_sprite.startdrag(false, dragrect);
scrollbar_sprite.addeventlistener(event.enter_frame, poledown);
}
private function poledown(event : event) : void {
//在滚动过程中及时获得滑块所处位置
var nowposition : number = math.floor(pole_sprite.y);
//使文本随滚动条滚动,这里为什么要加1,可见scroll属性值应该是取正的,也就是说它会删除小数部分,而非采用四舍五入制?
scrolltext.scrollv = (scrolltext.maxscrollv - 1) * (nowposition - polestarty) / totalpixels 2;
//误差校正
var unitpixels : number = totalpixels / (scrolltext.maxscrollv - 1);
if((nowposition - polestarty) < unitpixels) {
scrolltext.scrollv = (scrolltext.maxscrollv - 1) * (nowposition - polestarty) / totalpixels;
}
}
private function poleup(event : mouseevent) : void {
pole_sprite.stopdrag();
scrollbar_sprite.removeeventlistener(event.enter_frame, poledown);
scrollbar_sprite.stage.removeeventlistener(mouseevent.mouse_up, poleup);
scrolltext.addeventlistener(event.scroll, textscroll);
}
/**
* 滑块背景点击
*/
private function bgdown(event : mouseevent) : void {
var nowposition : number;
if((scrollbar_sprite.mousey - up_btn.y) < (pole_sprite.height / 2)) {
nowposition = math.floor(up_btn.y up_btn.height);
}else if((down_btn.y - scrollbar_sprite.mousey) < pole_sprite.height / 2) {
nowposition = math.floor(down_btn.y - pole_sprite.height);
}else {
nowposition = scrollbar_sprite.mousey - pole_sprite.height / 2;
}
pole_sprite.y = nowposition;
scrolltext.scrollv = (scrolltext.maxscrollv - 1) * (nowposition - polestarty) / totalpixels 2;
var unitpixels : number = totalpixels / (scrolltext.maxscrollv - 1);
if((nowposition - polestarty) < unitpixels) {
scrolltext.scrollv = (scrolltext.maxscrollv - 1) * (nowposition - polestarty) / totalpixels 1;
}
}
/**
* 下滚动按钮
*/
private function downbtn(event : mouseevent) : void {
scrolltext.scrollv ;
pole_sprite.y = math.floor(polestarty totalpixels * (scrolltext.scrollv - 1) / (scrolltext.maxscrollv - 1));
//当鼠标在按钮上按下的时间大于设定时间时,连续滚动
puttime = gettimer();
scrollbar_sprite.addeventlistener(event.enter_frame, downbtndown);
}
private function downbtndown(event : event) : void {
if(gettimer() - puttime > 500) {
scrolltext.scrollv ;
pole_sprite.y = math.floor(polestarty totalpixels * (scrolltext.scrollv - 1) / (scrolltext.maxscrollv - 1));
}
}
private function downbtnup(event : mouseevent) : void {
scrollbar_sprite.removeeventlistener(event.enter_frame, downbtndown);
}
/**
* 上滚动按钮
*/
private function upbtn(event : mouseevent) : void {
scrolltext.scrollv--;
pole_sprite.y = math.floor(polestarty totalpixels * (scrolltext.scrollv - 1) / (scrolltext.maxscrollv - 1));
//当鼠标在按钮上按下的时间大于设定时间时,连续滚动
puttime = gettimer();
scrollbar_sprite.addeventlistener(event.enter_frame, upbtndown);
}
private function upbtndown(event : event) : void {
if(gettimer() - puttime > 500) {
scrolltext.scrollv--;
pole_sprite.y = math.floor(polestarty totalpixels * (scrolltext.scrollv - 1) / (scrolltext.maxscrollv - 1));
}
}
private function upbtnup(event : mouseevent) : void {
scrollbar_sprite.removeeventlistener(event.enter_frame, upbtndown);
}
/**
* 鼠标滚轮事件
*/
private function mousewheel(event : mouseevent) : void {
if(isselect == false) {
scrolltext.scrollv -= math.floor(event.delta / 2);
}else if(isselect == true) {
event.delta = 1;
}
pole_sprite.y = math.floor(polestarty totalpixels * (scrolltext.scrollv - 1) / (scrolltext.maxscrollv - 1));
}
}
}