您的位置:首页 > 其它

关于flash好友列表的练习

2013-04-17 10:25 316 查看
package module.friendship
{
import com.greensock.TweenMax;
import com.xinglong.dataManager.UserData;

import flash.display.MovieClip;
import flash.events.MouseEvent;

import module.JModule;
import module.JModuleConfig;
import module.JTool;
import module.ResourceFactory;
import module.events.JModuleEvent;
import module.friendship.cell.Friend_cell;

public class Module_Friendship extends JModule{

public var _data:Array = new Array(); //装载从外部来的数据
public var listArr:Array = new Array();	//朋友数组
private var currentPage:int = 1; 	//当前页码
private var pageSize:int = 6; 	//每页显示六个好友
private var totalPage:int = 30;	//所有页数
private var filter:Object;

/**
* @param data 好友数据
*/
public function Module_Friendship(data:Array) {
trace("Module_Friendship");
//资源加载URL,默认为模块下swf/resource.swf,无需配置
ResourceFile=null;
//模块唯一标识ID,与模块包名相同
moduleID="friendship";
//资源MC列表,键名与链接ID配对,多个用逗号分隔
MCList="mc:_mc";
//是否单例
isSingleton=true;
//是否模式窗口
isModel=false;
_data = data;
super();
}

/**
* 初始化模板接口,子类覆盖继承作初始化
*/
public override function init()
{
var uiClass:* = ResourceFactory.getModuleClass(moduleID, "_mc");
creatMC(uiClass, 904, 71); //隐藏
initUI();
initEvent();
}

private function creatMC(mcCS:*, initX:int, initY:int) {
this.mc = new mcCS();
this.mc.x = initX;
this.mc.y = initY;
this.addChild(this.mc);
}

/**
* 设置列表数据,同时更新UI列表
*/
//		public function setData(d:*){
//			_data = d;
//			pageControl(1);
//		}

/**
*设置过滤条件 ,同时更新UI列表
*/
private function setFilter(f:Object){
filter = f;
pageControl(1);
}

private function initUI() {
this.setFilter({type:"好友"});
this.mc.btn_switch.gotoAndStop(2);
}

/**
* 初始化监听器
*/
private function initEvent() {
this.mc.btn_switch.addEventListener(MouseEvent.CLICK,mouseClick); //展入展出按钮监听器
this.mc.btn_friend.addEventListener(MouseEvent.CLICK,mouseClick);//好友监听器
this.mc.btn_adversary.addEventListener(MouseEvent.CLICK,mouseClick);//仇人
this.mc.left_btn.addEventListener(MouseEvent.CLICK,mouseClick);//左边按钮
this.mc.right_btn.addEventListener(MouseEvent.CLICK,mouseClick);//右边按钮
}

private function mouseClick(e:MouseEvent) {
if (e.currentTarget.enabled) {
switch (e.currentTarget.name) {
case "btn_switch":
switchType();
break;
case "btn_friend":
this.setFilter({type:"好友"});
//						currentType = 0;
break;
case "btn_adversary":
this.setFilter({type:"仇人"});
//						currentType = 1;
break;
case "left_btn":
if(currentPage > 1){
currentPage--;
pageControl(currentPage);
}
break;
case "right_btn":
if(currentPage < totalPage){
currentPage ++ ;
pageControl(currentPage);
}
break;
}
}
}

/**
* @return
* 面板收起或展开
*/
private function switchType(){
//判断面板是否展开
if(this.mc.btn_switch.currentFrame == 1){
hide();
this.mc.btn_switch.gotoAndStop(2);
trace("bt hide");
}
else if(this.mc.btn_switch.currentFrame == 2){
show();
this.mc.btn_switch.gotoAndStop(1);
trace("bt open");
}
}

/**
* @param pageNum 页数
* 分页
*/
private function pageControl(pageNum:Number)
{
for(var i:uint = 0;i < listArr.length ; i++){

listArr[i].remove();
listArr[i].removeEventListener(MouseEvent.MOUSE_DOWN,selectItem);
}
listArr = [];
var counter:uint = 0;
//获得同种类型列表的长度
for(var i=0;i<_data.length;i++){
//判断是否符合过滤条件
if(_data[i] && JTool.meet(_data[i],filter))
{
var cell:Friend_cell = new Friend_cell(_data[i]);

listArr.push(cell);
}
}
//全部页数
listArr.length % pageSize < pageSize ? totalPage =listArr.length / pageSize + 1:totalPage =listArr.length / pageSize;
//判断当前页数
if(pageNum > totalPage && pageNum<1){
currentPage = 1;
}
else if(pageNum < totalPage){
currentPage = pageNum;
}
//求取每页的数据条数,再进行分页添加
var num:uint = listArr.length - currentPage * pageSize > 0 ? pageSize: pageSize - (currentPage * pageSize - listArr.length);
for(var i:uint = 0;i < num ; i++){
if(listArr[i]!=null){
listArr[i].x = 0 ;
listArr[i].y = counter * listArr[i].height + 10;
mc.loadMc.addChild(listArr[i]);
listArr[i].addEventListener(MouseEvent.MOUSE_DOWN,selectItem);
listArr[i].mouseChildren = false;
listArr[i].mouseEnabled = true;
listArr[i].buttonMode = true;
counter ++ ;
}
}
mc.page_txt.text = currentPage +"/"+totalPage;
}

/**
*  派发点击单个好友选项的事件
*/
private function selectItem(e:MouseEvent){
var cell:Friend_cell = e.currentTarget as Friend_cell ;
this.dispatchEvent(new JModuleEvent(JModuleEvent.ITEM_SELECTED,cell.itemObj));
trace("cell click");
}

//显示效果
public function show() {
TweenMax.to(mc, 0.3, {bezierThrough:[{x:715, y:71}]});
}
//隐藏效果
public function hide() {
TweenMax.to(mc, 0.3, {bezierThrough:[{x:904, y:71}]});
}

public override function handleRemove()
{
this.mc.btn_switch.removeEventListener(MouseEvent.CLICK,mouseClick);
this.mc.btn_friend.removeEventListener(MouseEvent.CLICK,mouseClick);
this.mc.btn_adversary.removeEventListener(MouseEvent.CLICK,mouseClick);
this.mc.left_btn.removeEventListener(MouseEvent.CLICK,mouseClick);
this.mc.right_btn.removeEventListener(MouseEvent.CLICK,mouseClick);
}

}

}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: