Papa De Beau
Papa De Beau

Reputation: 3828

as3 addEventListner on a function in another class

I have a class calling a function in another class. I want to know if we can add an Event Listener to know if that function has fired and is finished etc.

Here is the section of the code that is relevant.

myMC = new pictures(); 
addChild(myMC);
myMC.loadImages("Joyful",1)
// Add Event Listener here to let me know loadImages was called and worked. 

As you can see the function is called loadImages() and it is located in a class called pictures.as

Can I add an event listener to this?

Here is a new issue. I am using a similar method and I used your example below and it did not work. Am I missing an import or something? I am only showing the functions that pertain and not my whole script.

Main.class package {

import flash.display.MovieClip;
import flash.events.*;
import flash.display.*;
import flash.filesystem.*;
import com.greensock.*;
import com.greensock.easing.*;
import flash.system.System;

// Ed's Scripts
import com.*;
import com.views.*;


public class iRosaryMain extends MovieClip
{

    /// (Get Main Doc flow) this creates an instace of the main timeline
    /// and then I send it 
    private static var _instance:iRosaryMain;
    public static function get instance():iRosaryMain
    {
        return _instance;
    }

    /// Declaring Vars
    var audio:audioPrayers;

    /// Loading Images
    //public var theImages:pictures = new pictures();

    /// Movie Clips
    public var myMary:beautifulMary;
    public var myMC:MovieClip;
    public var myPic:MovieClip;
    public var myFlags:MovieClip;
    public static var mt:MovieClip;



    var vars:defaultVars = new defaultVars();



    public function iRosaryMain()
    {
        // (Get Main Doc flow) Here I send the an instacne of iRosaryMain to defaultVars.as
        _instance = this;
        vars.getMainDoc(_instance);

        // Sets timeline to mt :) I hope
        mt = _instance;

        audio = new audioPrayers();
        trace("Jesus I trust in you!");// constructor code
        audio.sayHailMary();
        if (stage)
        {
            init();


        }
    }


    public function moveLanguages()
    {

        /// File to languages folder
        var checkLanguageFolder:File = File.applicationStorageDirectory.resolvePath("Languages");

        ///// CHECK LANGUAGES FOLDER - if not in App Storage move it
        if (! checkLanguageFolder.exists)
        {
            var sourceDir:File = File.applicationDirectory.resolvePath("Languages");
            var resultDir:File = File.applicationStorageDirectory.resolvePath("Languages");
            sourceDir.copyTo(resultDir, true);
            trace( "Moved Language!");
        }
    }


    //// MAIN FUNCTIONS IN iRosaryMainClass
    function init()
    {



        //loadFlags();
        moveLanguages();
        //addChild(theImages);
        intro();

    }


    public function loadFlags()
    {
        myFlags.addEventListener("MyEvent", eventHandler);
        myFlags = new viewLanguages();

        addChild(myFlags);
        myFlags.x = stage.stageWidth / 2 - myFlags.getBounds(this).width / 2;

        function eventHandler()
        {
            trace("yes loaded");
            TweenMax.fromTo(myMary,3, {alpha:1}, {alpha:0, ease:Quint.easeOut,  onComplete: closePic} );
        }
    }

    function intro()
    {

        myMary = new beautifulMary();
        addChild(myMary);
        loadFlags();
    }



    public function closePic()
    {
        removeChild(myMary);
    }



    public function languageLoaded(lang:String)
    {
        trace("YES... " + lang);
        homeIn();
    }


    public function homeIn()
    {
        if (myFlags)
        {
            TweenMax.fromTo(myFlags,1, {x:myFlags.x}, {x:0-myFlags.width, ease:Quint.easeInOut, onComplete:unloadMyFlags} );
        }

        function unloadMyFlags()
        {
            trace("Called Ease out");
            if (myFlags is MovieClip)
            {
                //trace("CURRENT DISPLAY " + currentDisplayScreen);
                trace("CURRENT mc " + myFlags);
                removeChild(myFlags);
                System.gc();
                myFlags = null;
                trace("CURRENT mc " + myFlags);

            }

        }

        if (! myMC)
        {
            myMC = new viewHome();
            myMC.name = "Home";
            myMC.x = stage.stageWidth / 2 - myMC.width / 2;
            addChild(myMC);
        }

        theEaseIn(myMC);
        //Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
    }


    public function loadLanguage(Language:String):Function
    {
        return function(e:MouseEvent):void ;
        {
            trace("Did it work? " +Language);
            vars.loadButtonVars(Language);;
        };

        //TweenMax.fromTo(EnglishButton,1, {x:EnglishButton.x}, {x:0-EnglishButton.width, ease:Quint.easeOut} ); 
    }


    public function loadView(screen:String)
    {

        trace("Received  " + screen);
        if (screen=="Home")
        {
            myMC = new viewHome();
            addChild(myMC);
            theEaseIn(myMC);
            //Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
        }
        if (screen=="PrayTheRosary")
        {
            myMC = new viewPrayTheRosary();
            addChild(myMC);
            theEaseIn(myMC);
            //Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
        }
        if (screen=="Options")
        {
            myMC = new viewOptions();
            addChild(myMC);
            theEaseIn(myMC);
        }
        if (screen=="Downloads")
        {
            myMC = new viewDownloads();
            addChild(myMC);
            theEaseIn(myMC);
        }
        if (screen=="beautifulMary")
        {
            myMC = new beautifulMary();
            addChild(myMC);
            theEaseIn(myMC);
        }
        if (screen=="Flags")
        {
            myFlags = new viewLanguages();
            addChild(myFlags);
            theEaseIn(myFlags);
        }
        if (screen=="Joyful" || screen=="Sorrowful" || screen=="Glorious" || screen=="Luminous")
        {
            myPic = new pictures();
            addChild(myPic);
            myPic.addEventListener( "ImagesLoaded",  doTheEaseIn);
            myPic.loadImages(""+screen+"",1);
            function doTheEaseIn()
            {
                theEaseIn(myPic);
            }
        }

    }



    public function theEaseIn(mc:MovieClip)
    {
        if (myMC)
        {
            TweenMax.fromTo(mc,1, {x:stage.stageWidth+mc.width}, {x:stage.stageWidth/2 - mc.width/2, ease:Quint.easeInOut} );


        }
    }

    public function theEaseOut(mc:MovieClip,nextMC:String):Function
    {
        return function(e:MouseEvent):void ;
        {
            loadView(nextMC);
            /// Tweens out view on screen
            TweenMax.fromTo(mc,1, {x:mc.x}, {x:0-mc.width, ease:Quint.easeInOut, onComplete:unloadMC} );
            function unloadMC();
            {
                trace("Called Ease out");
                if(mc is MovieClip);
                {
                    //trace("CURRENT DISPLAY " + currentDisplayScreen);
                    trace("CURRENT mc " + mc);
                    removeChild(mc);
                    System.gc();
                    myMC = null;
                    trace("CURRENT mc " + mc);

                };

            };
        };/// end return
    }




    ////////////
}/// End iRosaryMain

}// End Package

viewLanguages.as

package com.views

package com.views
{

    import flash.display.MovieClip;
    import flash.filesystem.File;
    import com.roundFlag;
    import flash.events.*;
    import flash.display.*;


    public class viewLanguages extends MovieClip
    {

        var Flag:MovieClip;
        //public static const FLAGS_LOADED:String = "flagsLoaded"; 
        public function viewLanguages()
        {
            getLang();
        }


        function numCheck(num:int):Boolean
        {
            return (num % 2 != 0);
        }
        /// Get for App Opening
        public function getLang()
        {
            /// When Live
            var folderLanguages:File = File.applicationStorageDirectory.resolvePath("Languages");
            var availLang = folderLanguages.getDirectoryListing();
            var Lang = new Array();
            var LangPath = new Array();
            var fl:int = 0;
            for (var i:uint = 0; i < availLang.length; i++)
            {

                if (availLang[i].isDirectory)
                {
                    //flag = new Loader();
                    //flag.load(new URLRequest(File.applicationStorageDirectory.url + "Languages/" + Lang[i] + "/roundFlag.png"));


                    Lang.push(availLang[i].name);
                    LangPath.push(availLang[i].nativePath);
                    Flag = new roundFlag(availLang[i].name);
                    Flag.name = availLang[i].name;

                    if (numCheck(i)==false)
                    {
                        Flag.y = fl;
                        Flag.x = 0;

                    }
                    else
                    {
                        Flag.y = fl;
                        Flag.x = Flag.width + 33;
                        fl = fl + Flag.height + 33;
                    }

                    Flag.btext.text = availLang[i].name;
                    addChild(Flag);


                    trace(availLang[i].nativePath);// gets the name
                    trace(availLang[i].name);

                }
            }
            trace("Get Lang Called");
            dispatchEvent(new Event("MyEvent"));
        }



    }

}

Upvotes: 0

Views: 156

Answers (2)

Hardy
Hardy

Reputation: 5631

You have to create custom event class for this to work. Like:

package com.some
{

    import flash.events.Event;

    public class SomeEvent extends Event
    {

        // Loading events
        public static const MAINDATA_LOADING:String = "onMainDataLoading";
        // Other event
        public static const SOME_LOADING:String = "onSomeLoading";      


        public var params:Object;

        public function SomeEvent($type:String, $params:Object, $bubbles:Boolean = false, $cancelable:Boolean = false)
        {
            super($type, $bubbles, $cancelable);
            this.params = $params;
        }

        public override function clone():Event
        {
            return new SomeEvent(type, this.params, bubbles, cancelable);
        }

    }

}

Add event dispatch inside wanted class (Pictures) and pass wanted params to event (this or any else)

dispatchEvent(new SomeEvent(SomeEvent.IMAGES_LOADED, this));

Handle event listening:

var myMc:Pictures = new Pictures();
myMc.addEventListener(SomeEvent.IMAGES_LOADED, onImagesLoaded);
myMc.loadImages();

function onImagesLoaded(e:SomeEvent):void {
   // handle event.
   var picts:Pictures = (e.params as Pictures);
}

Upvotes: 1

Fygo
Fygo

Reputation: 4665

Yes, you can. Considering you are adding your clip to the display list I assume it extends either Movieclip or Sprite, both of which extend EventDispatcher. In your pictures class you can simply use dispatchEvent(new Event("MyEvent"); to dispatch an event. Then you could add myMC.addEventListener("MyEvent", eventHandler); to react to it.

You should also not use strings for events like I wrote above. It would be better if you declared a public static const IMAGES_LOADED:String = "imagesLoaded"; in your pictures class. Then you should use this constant by dispatching and by listening to it.

EDIT: Here how it would look with the constant:

//your class
package {

    import flash.events.Event;
    import flash.display.Sprite;

    public class Pictures extends Sprite {

        public static const IMAGES_LOADED:String = "imagesLoaded"; 

        public function Pictures() {

        }

        public function onAllImagesLoaded():void {
            dispatchEvent(new Event(IMAGES_LOADED));
        }
        //rest of your methods
    }
}

//you would call it like this:
var myMc:Pictures = new Pictures();
myMc.addEventListener(Pictures.IMAGES_LOADED, onImagesLoaded);
myMc.loadImages();

function onImagesLoaded(e:Event):void {
    //...etc
}

Upvotes: 1

Related Questions