Jump to content
Search Community

Progress returns 0 when completed

Hulio test
Moderator Tag

Recommended Posts

Why is this happening? The value is 0, not 1, when it is fully loaded.

 

	/** A value between 0 and 1 indicating the overall progress of the loader. When nothing has loaded, it will be 0; when it is halfway loaded, progress will be 0.5, and when it is fully loaded it will be 1. **/
	public function get progress():Number {
		return (this.bytesTotal != 0) ? _cachedBytesLoaded / _cachedBytesTotal : (_status == LoaderStatus.COMPLETED) ? 1 : 0;
	}

Link to comment
Share on other sites

I cannot reproduce that issue - could you please post a sample FLA (and any support files) that I could publish to see this happen? Are you 100% sure that your asset has fully loaded and not failed? How are you checking that? Is the "status" LoaderStatus.COMPLETED (2)?

Link to comment
Share on other sites

Allright, here's the code

 

package {

import com.greensock.TweenMax;
import com.greensock.easing.Quad;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.ImageLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.plugins.AutoAlphaPlugin;
import com.greensock.plugins.TweenPlugin;

import flash.display.BlendMode;
import flash.display.Sprite;
import flash.text.AntiAliasType;
import flash.text.Font;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

TweenPlugin.activate([AutoAlphaPlugin]);
LoaderMax.activate([imageLoader]);

public dynamic class Main extends Sprite {

	// DEFINE THE VARIABLES

	// LOADER CONTENT

	private var preloader : LoaderMax;


	// STAGE ELEMENTS

	private var font_times : Font = new Times();

	private var format_preloader : StyleSheet;

	private var mc_preloader : Sprite;
	private var preloader_indicator : Sprite;
	private var preloader_txt : TextField;

	private var container : Sprite;



	public function Main() {

		// DEFINE THE TEXT FORMATS

		format_preloader = new StyleSheet();
		format_preloader.setStyle("p", {fontFamily : font_times.fontName, fontSize : 12, color : "#FFFFFF"});


		// DEFINE THE PRELOADER CONTENT

		mc_preloader = new Sprite();

		mc_preloader.graphics.beginFill(0xAAAAAA);
		mc_preloader.graphics.drawRect(0, 0, stage.stageWidth, 14);
		mc_preloader.graphics.endFill();

		// ADD THE PRELOADER INDICATOR

		preloader_indicator = new Sprite();

		preloader_indicator.graphics.beginFill(0xCCCCCC);
		preloader_indicator.graphics.drawRect(0, 0, stage.stageWidth, 14);
		preloader_indicator.graphics.endFill();


		mc_preloader.addChild(preloader_indicator);

		// ADD THE PRELOADER TEXT

		preloader_txt = create_textfield(mc_preloader, null, null, format_preloader, false, false, 100, "center", 1);


		// DEFINE THE PRELOADER DATA

		preloader = new LoaderMax({name: "preloader_data", autoDispose: true, onOpen: preload_open, onProgress: preload_progress, onComplete: preload_complete, onError: preload_error});

		preloader.append( new ImageLoader("images/photo_1.jpg", {name: "photo_1", estimatedBytes: 500}) );
		preloader.append( new ImageLoader("images/photo_2.jpg", {name: "photo_2", estimatedBytes: 100}) );
		preloader.append( new ImageLoader("images/photo_3.jpg", {name: "photo_3", estimatedBytes: 500}) );

		preloader.load();


		// CREATE A PRELOADING OPEN FUNCTION

		function preload_open(event : LoaderEvent) : void {

			// ADD THE PRELOADER TO STAGE

			addChild(mc_preloader);


			mc_preloader.y = stage.stageHeight / 2;

			preloader_indicator.width = 0;

		}


		// CREATE A PRELOADING FUNCTION

		function preload_progress(event : LoaderEvent) : void {

			preloader_indicator.width = preloader.progress * stage.stageWidth;


			preloader_txt.htmlText = '
' + Math.round(preloader.progress * 100) + ' %';

			preloader_txt.x = stage.stageWidth / 2;

		}


		// CREATE A PRELOADING ERROR FUNCTION

		function preload_error(event : LoaderEvent) : void {

			trace("Error: " + event.target + ": " + event.text);

		}


		// CREATE A PRELOADING COMPLETE FUNCTION

		function preload_complete(event : LoaderEvent) : void {

			TweenMax.to(mc_preloader, 0.5, {autoAlpha: 0, ease: Quad.easeOut});


			container = new Sprite();

			addChild(container);


			container.addChild(preloader.getContent("photo_1").rawContent);
			container.addChild(preloader.getContent("photo_2").rawContent);
			container.addChild(preloader.getContent("photo_3").rawContent);

			container.alpha = 0;

			TweenMax.to(container, 0.5, {alpha: 1, ease: Quad.easeOut, delay: 0.5});

		}

	}



	protected function create_textfield(_container : Sprite, _name : String, _html : String, _style : StyleSheet, _multiline : Boolean, _wordwrap : Boolean, _width : Number, _align : String, _alpha : Number) : TextField {

		var _textfield : TextField = new TextField();

		if (_name) _textfield.name = _name;
		if (_style) _textfield.styleSheet = _style;
		if (_width) _textfield.width = _width;
		_textfield.embedFonts = true;
		_textfield.multiline = _multiline;
		_textfield.wordWrap = _wordwrap;
		_textfield.selectable = false;
		_textfield.blendMode = BlendMode.LAYER;
		_textfield.antiAliasType = AntiAliasType.ADVANCED;

		var set_align : String = '';

		if (_align == 'left') set_align = TextFieldAutoSize.LEFT;
		if (_align == 'center') set_align = TextFieldAutoSize.CENTER;
		if (_align == 'right') set_align = TextFieldAutoSize.RIGHT;

		_textfield.autoSize = set_align;

		_textfield.alpha = _alpha;
		if (_html) _textfield.htmlText = _html;

		if (_container) {

			_container.addChild(_textfield);

		} else {

			var _sprite : Sprite = new Sprite();
			addChild(_sprite);
			_sprite.addChild(_textfield);

		}

		return _textfield;

	}

}

}

 

Thanks for your reply!

Link to comment
Share on other sites

It's because you set autoDispose:true - it actually did correctly report progress as 1 when the LoaderMax completed, but when it disposed itself immediately after that, it dispatched a PROGRESS event to notify that it has been emptied (thus progress == 0 because there were no loaders in it anymore). I just posted an update, though, that will skip that last PROGRESS event when it has been disposed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...