动作脚本 2.0 中未触发调度事件
dispatch event not fired in action script 2.0
我创建了一个名为 CheckBoxSelectAll 的 class,我在其中触发了一个事件,如下所示。
import mx.events.EventDispatcher;
import flash.filters.GlowFilter;
class CheckBoxSelectAll
{
public function dispatchEvent() {};
public function addEventListener() {};
public function removeEventListener() {};
private var checkbox_mc:MovieClip;
private var parent_mc:MovieClip;
function CheckBoxSelectAll()
{
mx.events.EventDispatcher.initialize(this);
}
function CreateCheckBox(c_mc:MovieClip)
{
var labelGlow:GlowFilter = new GlowFilter(0xFFFFFF, .30, 4, 4, 3, 3);
var labelFilters:Array = [labelGlow];
this.parent_mc = c_mc;
checkbox_mc = parent_mc.createEmptyMovieClip("",this.checkbox_mc.getNextHighestDepth() );
checkbox_mc._x =450;// boxX;
checkbox_mc._y =143;// boxY;
checkbox_mc.lineStyle(1, 0);
checkbox_mc.beginFill(currentFill, currentAlpha);
checkbox_mc.moveTo(0, triSize);
checkbox_mc.lineTo(triSize, triSize);
checkbox_mc.lineTo(triSize, 0);
checkbox_mc.lineTo(0, 0);
checkbox_mc.lineTo(0, triSize);
checkbox_mc.endFill();
checkbox_mc._visible = true;
checkbox_mc.onPress = function() {
var eventObject:Object = {target:this, type:'onDataReady'};
dispatchEvent(eventObject);
trace("OnPress refresh...");
}
}
}
在父影片剪辑中,使用了以下代码
var select_all_listener:Object = new Object();
select_all_listener.onDataReady = triggerDisksLoad;
var select_all_box:CheckBoxSelectAll;
select_all_box = new CheckBoxSelectAll();
select_all_box.addEventListener("onDataReady", select_all_listener);
select_all_box.CreateCheckBox(this);
function triggerDisksLoad(evtObj) { trace("triggerDisksLoad called...!!!"); }
此处未调用函数 triggerDisksLoad。
您的代码的问题是执行 checkbox_mc.onPress
处理程序的范围,为避免这种情况,您可以使用 Delegate class,如下所示:
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class CheckBoxSelectAll extends MovieClip
{
// ...
function CreateCheckBox(c_mc:MovieClip)
{
// ...
checkbox_mc.onPress = Delegate.create(this, _onPress);
}
private function _onPress():Void {
var event:Object = {target: this, type: 'onDataReady'};
dispatchEvent(event);
}
}
同样对于新的MovieClips的创建,当我们使用getNextHighestDepth()
时,它通常与新的MovieClip的parent一起使用,所以你可以这样写:
checkbox_mc = parent_mc.createEmptyMovieClip('mc_name', parent_mc.getNextHighestDepth());
希望能帮到你。
我创建了一个名为 CheckBoxSelectAll 的 class,我在其中触发了一个事件,如下所示。
import mx.events.EventDispatcher;
import flash.filters.GlowFilter;
class CheckBoxSelectAll
{
public function dispatchEvent() {};
public function addEventListener() {};
public function removeEventListener() {};
private var checkbox_mc:MovieClip;
private var parent_mc:MovieClip;
function CheckBoxSelectAll()
{
mx.events.EventDispatcher.initialize(this);
}
function CreateCheckBox(c_mc:MovieClip)
{
var labelGlow:GlowFilter = new GlowFilter(0xFFFFFF, .30, 4, 4, 3, 3);
var labelFilters:Array = [labelGlow];
this.parent_mc = c_mc;
checkbox_mc = parent_mc.createEmptyMovieClip("",this.checkbox_mc.getNextHighestDepth() );
checkbox_mc._x =450;// boxX;
checkbox_mc._y =143;// boxY;
checkbox_mc.lineStyle(1, 0);
checkbox_mc.beginFill(currentFill, currentAlpha);
checkbox_mc.moveTo(0, triSize);
checkbox_mc.lineTo(triSize, triSize);
checkbox_mc.lineTo(triSize, 0);
checkbox_mc.lineTo(0, 0);
checkbox_mc.lineTo(0, triSize);
checkbox_mc.endFill();
checkbox_mc._visible = true;
checkbox_mc.onPress = function() {
var eventObject:Object = {target:this, type:'onDataReady'};
dispatchEvent(eventObject);
trace("OnPress refresh...");
}
}
}
在父影片剪辑中,使用了以下代码
var select_all_listener:Object = new Object();
select_all_listener.onDataReady = triggerDisksLoad;
var select_all_box:CheckBoxSelectAll;
select_all_box = new CheckBoxSelectAll();
select_all_box.addEventListener("onDataReady", select_all_listener);
select_all_box.CreateCheckBox(this);
function triggerDisksLoad(evtObj) { trace("triggerDisksLoad called...!!!"); }
此处未调用函数 triggerDisksLoad。
您的代码的问题是执行 checkbox_mc.onPress
处理程序的范围,为避免这种情况,您可以使用 Delegate class,如下所示:
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class CheckBoxSelectAll extends MovieClip
{
// ...
function CreateCheckBox(c_mc:MovieClip)
{
// ...
checkbox_mc.onPress = Delegate.create(this, _onPress);
}
private function _onPress():Void {
var event:Object = {target: this, type: 'onDataReady'};
dispatchEvent(event);
}
}
同样对于新的MovieClips的创建,当我们使用getNextHighestDepth()
时,它通常与新的MovieClip的parent一起使用,所以你可以这样写:
checkbox_mc = parent_mc.createEmptyMovieClip('mc_name', parent_mc.getNextHighestDepth());
希望能帮到你。