事件派发

在舞台上创建一个影片剪辑mc,创建类Mouse;

package
{

import flash.display.MovieClip;
import flash.events.MouseEvent;
import com.Main_mc;//导入要调用的包
import flash.events.Event;

public class Mouse extends MovieClip
{
private var Mc:Main_mc;

public function Mouse()
{
mc.addEventListener(MouseEvent.CLICK,onSpClickHandler);//为mc添加CLICK单击事件,每次单击时接受com文件夹Main_mc类的派发
}
private function onSpClickHandler(e:MouseEvent):void
{//
// while (numChildren)
// {
// removeChildAt(0);//清除舞台上的对象,括号内为对象的个数
// }

Mc = new Main_mc();
addChild(Mc);
Mc.addEventListener("MOVE",Move);//接受"MOVE"派发并添加Move方法
Mc.MC(2);//设置Main_mc类内speed的值

}

private function Move(e:Event):void
{

}
}

}

在同一目录下创建com文件夹放入Main_mc类文件

package com
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;

public class Main_mc extends Sprite
{
public var sp:Sprite;
public var m:int;
public function Main_mc()
{

}
public function MC(speed:Number):void
{
sp = new Sprite();

var g:Graphics = sp.graphics;
g.beginFill(0x00ff00);
g.drawRect(0,0,20,20);
g.endFill();
this.addChild(sp);

m = speed;
sp.addEventListener(Event.ENTER_FRAME,onMoveHandler);
sp.dispatchEvent(new Event("MOVE"));//将"MOVE"派发

}
private function onMoveHandler(e:Event):void
{
sp.x += m;
}

}

}

posted @ 2012-11-04 21:56  流年亂了浮生  阅读(174)  评论(0编辑  收藏  举报