dispatch event not fired in action script 2.0

164 views Asked by At

I have created a class named CheckBoxSelectAll in which I am triggering an event as below.

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...");
        }
    }   
}

In Parent movie clip, used following code

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...!!!"); }

Here function triggerDisksLoad is not called.

1

There are 1 answers

0
akmozo On BEST ANSWER

The problem of your code is the scope where the checkbox_mc.onPress handler is executed, to avoid that, you can use the Delegate class, like this :

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);
    }
}

Also for the new MovieClips creation, when we use getNextHighestDepth(), it's usually used with the parent of the new MovieClip, so you can write :

checkbox_mc = parent_mc.createEmptyMovieClip('mc_name', parent_mc.getNextHighestDepth());

Hope that can help.