AS3 Preloader未更新屏幕

时间:2012-08-28 23:28:06

标签: actionscript-3 preloader

我一直在努力使用Flash预加载器。我只是想用当前的百分比来更新屏幕上的文字。现在它基本上可以作为跟踪输出正确的百分比,但它不会更新我在屏幕上的文本字段。然而,一旦跟踪达到100%,代码就会在屏幕上输出“100”,但直到它全部加载为止。我没有Flash IDE,只使用FlashDevelop的纯ActionScript。这是我的代码:

package 
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.display.Loader;
  import flash.events.ProgressEvent;
  import flash.net.URLRequest;
  import flash.net.URLLoader;
  import flash.display.Loader; 
  import flash.text.TextField;
  import flash.text.TextFormat;
  import flash.text.TextFieldType;


  public class Main extends Sprite 
  {
    public var myLoader:Loader = new Loader();
    public var image:String = "tmp/Avengers-poster.jpg";


    private var Title:TextField;
    private var txt:String;
    private var Form:TextField;

    public function Main():void {
      textbox("This is the title","box",100,100,200,30);

      myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
      myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);

      var fileRequest:URLRequest = new URLRequest(image);
      myLoader.load(fileRequest);           
    }

    public function onProgressStatus(e:ProgressEvent):void {   
        // this is where progress will be monitored     
        var perc:int = Math.ceil((e.bytesLoaded/e.bytesTotal)*100);
        txt = perc.toString();
        Title.text = txt;
        addChild (Title);
        trace(perc); 
    }

    public function onLoaderReady(e:Event):void {     
        // the image is now loaded, so let's add it to the display tree!     
        addChild(myLoader);
    }   

    private function textbox (title_str:String,form_name:String,x:int,y:int,width:int,height:int):void {

       Title = new TextField ();
       Title.text = title_str;
       Title.selectable = false;
       Title.setTextFormat (new TextFormat ("Arial", 12, 0x777777, true));

       txt = ".";

       Title.x = x;
       Title.y = y;
       addChild (Title);
    }       

  }

}

感谢您的帮助。

的Darryl

1 个答案:

答案 0 :(得分:0)

这会奏效。不要在每个状态上重新创建TextField。您只需要addChild一次并更新对它的引用。

package 
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;   

    public class Main extends Sprite 
    {
        public var myLoader:Loader = new Loader();
        public var image:String = "http://apod.nasa.gov/apod/image/0605/titan5km_huygens_big.jpg";

        private var Title:TextField;
        private var Form:TextField;

        public function Main():void {

            createTextField("This is the title","box",100,100,200,30);

            myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
            myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);

            var fileRequest:URLRequest = new URLRequest(image);
            myLoader.load(fileRequest);           
        }

        public function onProgressStatus(e:ProgressEvent):void {   
            // this is where progress will be monitored     
            var perc:int = Math.ceil((e.bytesLoaded/e.bytesTotal)*100);
            Title.text = perc.toString();
        }

        public function onLoaderReady(e:Event):void {     
            // the image is now loaded, so let's add it to the display tree!     
            addChild(myLoader);
        }   

        private function createTextField (title_str:String,form_name:String,x:int,y:int,width:int,height:int):void {

            Title = new TextField();
            Title.text = title_str;
            Title.selectable = false;
            Title.setTextFormat (new TextFormat ("Arial", 12, 0x777777, true));

            Title.text = ".";

            Title.x = x;
            Title.y = y;

            addChild (Title);
        }       

    }   
}