简体   繁体   English

Flash AS3类对象调用方法

[英]Flash AS3 class Object calling methods

HI all, i have a class file named Main.as and another class calledr icon.as 大家好,我有一个名为Main.as的类文件,另一个名为r icon.as的类

package {

    import icon;
    public class main {
        public var _time:String;
        function main() {

            _time="01:10";
            iconObj=new icon(this);

        }
        function timerFunction() {
            _time=newTime;
        }
    }
}

package {

    public class icon {
        public var mytime:NUmber;
        function icon(mainObj:*) {

            trace("My time "+mainObj._time)

        }

    }
}

//sample outout //样本输出

// My time 01:10 //我的时间01:10

How do i get the current update from the main class without calling the MainObj repeatedly. 我如何从主类获取当前更新, 而无需重复调用MainObj Is this possible in Flash AS3, or any other alternate method for this idea. 在Flash AS3中,或者在此想法的任何其他替代方法中,这是否可行?

Check out the Observer Pattern . 查看“ 观察者模式” You can have a clock which notifies its observers once the time changes. 您可以有一个时钟,该时钟在时间改变时通知观察者。

Ther are already libraries simplifing this job for you like as3-signals . 这些库已经像as3-signals一样为您简化了这项工作。 You can also use flash.events.EventDispatcher for the same task. 您也可以将flash.events.EventDispatcher用于同一任务。

Store a reference to the Main class object locally in Icon Icon本地存储对Main类对象的引用

package 
{
    public class Icon 
    {
        public var mytime:NUmber;
        //store an instance of Main obj here.
        public var mainObj:Main;
        public function Icon(mainObj:Main) 
        {
           this.mainObj = mainObj;
        }
        //call this method whenever you want time
        public function readTime():void
        {
           trace("My time " + mainObj._time);
        }
    }
}

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

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