package com.cdyemail.cai.student
{
import com.cdyemail.net.events.LoadPictureEvent;
import com.cdyemail.net.LoadPicture;
import com.cdyemail.net.LoadXML;
import com.cdyemail.tools.math.Tools;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Sound;
public class CallStudent extends Sprite
{
//存放学生的数组
private var student:Array;
//引用舞台上的影片作为遮罩用
private var _mask:MovieClip;
//存放相片的容器
private var _con:Sprite;
//定义移动的速度和加速度
private var vy:Number = 0;
private var ay:Number = 1;
//引用摇杆
private var _begin:MovieClip;
//定义图片所在的左上角位置
private var left:int = 155;
private var top:int = 75;
//数据层
private var _xml:LoadXML;
private var _picture_total:int;
private var _url:String =
"com/cdyemail/cai/student/Head.xml";
public function CallStudent()
{
_mask = theMask; //引用舞台上的影片实例名为theMask
_begin = begin; //引用舞台上的摇杆对象
student = [];
_con = addChild(new Sprite()) as Sprite;
_con.mask = _mask;
_begin.addEventListener("notify", beginHandler);
_begin.mouseChildren = false;
parseXML();
}
//解析数据
private function parseXML():void
{
_xml = new LoadXML(_url);
_xml.addEventListener(LoadXML.LOAD_DATA_COMPLETE,
xmlHandler);
}
private function
xmlHandler(e:Event):void
{
_xml.removeEventListener(LoadXML.LOAD_DATA_COMPLETE,
xmlHandler);
_picture_total = _xml.data.picture.length();
for (var i:int = 0; i < _picture_total;
i++)
{
var pic:LoadPicture = new
LoadPicture(_xml.data.picture[i]);
pic.addEventListener(LoadPictureEvent.LOAD_PICTURE_COMPLETE,
picHandler);
}
}
private function
picHandler(e:LoadPictureEvent):void
{
student.push(e.target);
if (student.length == _picture_total)
{
_begin.mouseChildren = true;
list(student);
}
}
private function beginHandler(e:Event):void
{
Tools.breakShort(student);
list(student)
vy = -163;
//由于声音小,所以绑定到库里的资源即可
//摇杆声音
var snd:Sound = new BeginSound();
snd.play();
//滚动声音
snd = new Loop();
snd.play(0,2);
this.addEventListener(Event.ENTER_FRAME,runHandler);
}
private function runHandler(e:Event):void
{
vy += ay; //加速度
if (vy>=0)
{
vy = 0;
flash.media.SoundMixer.stopAll();
//抽取完的声音
var snd:Sound = new Complete();
snd.play();
this.removeEventListener(Event.ENTER_FRAME,runHandler);
_begin.init();
}
else
{
for (var i:int=0; i
{
student[i].y += vy;
}
if (student[0].y <= -
student[0].height)
{
var mc:Sprite = student[0];
student.shift();
student.push(mc);
mc.x = 0;
mc.y = student[student.length - 2].y + mc.height;
}
}
}
private function list(arr:Array):void
{
Tools.breakShort(arr);
var padding:Number = student[0].height;
_con.x = left;
_con.y = top;
_con.addChild(student[0]);
student[0].x = 0;
student[0].y = 0;
for (var i:int=1; i
{
_con.addChild(student[i]);
student[i].x = student[i - 1].x;
student[i].y = student[i - 1].y + padding;
}
}
//对外接口
// 初始化:外部引用
// 当非文档类时,做为一个课件的插件部分,则用这个引用外部资源进行初始化
// 注意:如果做为插件则需要注释掉构造函数里的代码
public function
initExObject(mask:MovieClip,begin:MovieClip,url:String):void
{
_mask = mask;
_begin = begin;
_url = url;
student = [];
_con = addChild(new Sprite()) as Sprite;
_con.mask = _mask;
_begin.addEventListener("notify", beginHandler);
_begin.mouseChildren = false;
parseXML();
}
//析构
public function destruct():void
{
_xml.destruct();
for (var i:int = 0; i < student.length;
i++)
{
student[i].destruct();
}
student = null
_con = null;
_mask = null;
_begin = null;
}
}
}