简体   繁体   English

as3 addEventListner在另一个类的函数上

[英]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 正如您所看到的,该函数称为loadImages(),它位于名为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 { Main.class包{

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 viewLanguages.as

package com.views 包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"));
        }



    }

}

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. 考虑到您要将剪辑添加到显示列表中,我假设它扩展了Movieclip或Sprite,两者都扩展了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. 在你的图片类中,你可以简单地使用dispatchEvent(new Event("MyEvent");来调度一个事件。然后你可以添加myMC.addEventListener("MyEvent", eventHandler);来对它作出反应。

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"; 如果你声明一个public static const IMAGES_LOADED:String = "imagesLoaded";会更好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
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM