views:

268

answers:

2

HI all, i have a class file named Main.as and another class calledr 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

How do i get the current update from the main class without calling the MainObj repeatedly. Is this possible in Flash AS3, or any other alternate method for this idea.

+1  A: 

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. You can also use flash.events.EventDispatcher for the same task.

Joa Ebert
actually i dont want to trigger the timer from the main clas. I need to check this from icon.as file. at a specific point i need to get the time from main class.. i think now you understood what i want :)
coderex
If you "need to get the time from main class" you will have to ask for it, like "mainObj.time".
Joa Ebert
A: 

Store a reference to the Main class object locally in Icon

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