Flash AS3 class Object calling methods
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开发者_JAVA技巧
// 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.
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.
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);
}
}
}
精彩评论