So simple, but it doesn't work!

Hi,
don’t laught ! but i’ve got some difficulty to make a preloader for downloading a JPG picture into a MC !

i’ve got a simple MC on my scene. i want to download a picture inside it, when i lauche my swf.

this my as code:


stop();
MC_Cont._alpha = 0;
_root.preloader_mc._visible = true;
preloader_mc.txt_loader = "backGround loading";

function preload(uurl) {
    this.onEnterFrame = function() {
        var loaded:Number = this.getBytesLoaded();
        var total:Number = this.getBytesTotal();
        var percent:Number = Math.round((loaded/total)*100);
        _root.preloader_mc.percent = percent+"%";
        trace(loaded);
        if (percent>=100) {
            delete this.onEnterFrame;
            _root.preloader_mc._visible = false;
            this.onEnterFrame = function() {
                this._alpha += 2;
                if (this._alpha == 100) {
                    delete this.onEnterFrame;
                }
            };
        }
    };
}
MC_Cont.preload("backgrounds/backg_1.jpg");


what’s wrong ?

Initially while loading something into a movieclip bytesLoaded and bytesTotal may equal. The value may be either the current bytesize of the movieclip or -1. So first do an unload on the target movieclip and then make sure that the amount of loaded bytes is larger than the movieclips initial size. For an empty movieclip put on stage at authortime that size is 4 bytes.

/Mirandir

thanks, but i think it’s not the solution , since i try an “unLoadMovie” with no result.
i change my code to this:


bgConteneur.loadMovie("backgrounds/backg_1.jpg");
bgConteneur.onEnterFrame = function() {
    var loaded:Number = this.getBytesLoaded();
    var total:Number = this.getBytesTotal();
    var percent:Number = Math.round((loaded/total)*100);
    preloader_mc.percent = percent+"%";
    trace(loaded);
    if (percent>=100) {
        delete this.onEnterFrame;
        preloader_mc._visible = false;
        this.onEnterFrame = function() {
            this._alpha += 2;
            if (this._alpha == 100) {
                delete this.onEnterFrame;
            }
        };
    }
};

i think it’s not a big pb…but a vicious pb !

ok, i found the error :slight_smile:

If you load in a mc you can’t use that mc’s onEnterframe to check the loading…

bgConteneur.loadMovie("backgrounds/backg_1.jpg");
var temp = this.createEmptyMovieClip("temp", 998);
bgConteneur._alpha = 0;
temp.onEnterFrame = function() {
	var loaded:Number = bgConteneur.getBytesLoaded();
	var total:Number = bgConteneur.getBytesTotal();
	var percent:Number = Math.round((loaded/total)*100);
	preloader_mc.percent = percent+"%";
	if (bgConteneur._width) {
		preloader_mc._visible = false;
		bgConteneur._alpha += 2;
		if (bgConteneur._alpha>100) {
			bgConteneur._alpha = 100;
			delete this.onEnterFrame;
		}
	}
};

scotty(-:

hey, Scotty ! :slight_smile:

thanks very much.

if i understand, we must create another temp MC ( “temp” ) to make the “onEnterFrame” and make the preloader ? but why ?

another question about your AS code: what’s the " [COLOR=#0000FF]if[/COLOR] COLOR=#000000" condition ? i don’t understand…

so, this is my version, it’s seems to work… ( but i think i will try your AS code :slight_smile: i know you legendary AS code quality :wink: )
[/COLOR]
if you can explain me my AS error, you’re welcome !

thanks !


 [COLOR=#0000FF][/COLOR][COLOR=#000000][/COLOR]onEnterFrame = function () {
    var loaded:Number = bgConteneur.getBytesLoaded();
    var total:Number = bgConteneur.getBytesTotal();
    var percent:Number = Math.round((loaded/total)*100);
    preloader_mc.percent = percent+"%";
    if (percent>=90) {
        bgConteneur._alpha += 2;
        preloader_mc.tween(["_y", "_alpha"], [0, 0], 4, "easeOutExpo");
        if (bgConteneur._alpha>=100) {
            delete this.onEnterFrame;
            play();
        }
    }
};
bgConteneur.loadMovie("backgrounds/backg_1.jpg");

stef ( always want to learn )

If you load something into a movieclip all properties of that movieclip are reset to default. For example the onEnterFrame handler the movieclip had before it loaded has to be replaced by a new onEnterFrame handler the loaded movie uses. That’s why I use mc ‘temp’.

if (bgConteneur._width) {

checks if bgConteneur has a width (ie is full loaded), as long as it is not loaded, it will returns false (width=0).
In your code, it’s better to assign the onenterframe to a mc plus (as said above) you can’t use the alpha property before it’s fully loaded, so the

if (percent>=90) {
       bgConteneur._alpha += 2;

will work, as soon percent equals 100 :slight_smile:

scotty(-:

thanks very much professor :wink:

i will write this in my learning book :slight_smile:
more i learn, more i found flash AS vicious !!!
it’s not easy to found THE good tip each time i’ve got a pb…

You’re welcome =)

ah Scotty !

another question:
i notice a strange thing with your AS version code, and mine…

when i reload my page quickly, and several times ( with Firefox ) , i see a %NAN% for 0.5sec, and then, the preload work…strange !

what’s this ? is it normal ? how can i resolve this “bug” ?

Try

percent !=isNaN ? preloader_mc.percent = percent+"%" : preloader_mc.percent = 0%;

scotty(-:

thanks scotty :slight_smile:
i will try this tonight…
what do you think about this issue ?? i think the preloader work too quickly, due to my connection speed ?

The preloader works ok, but if the load is initiated for a moment bgConteneur.getBytesTotal() returns 0 and you can’t didide by 0 :slight_smile:

scotty(-:

don’t work :-(… synthax error !
(% need operator …humm seems to have pb with the “:”)

humm… your tips seems not to work ( always NaN% when my swf is online )…maybe i make an error :


var temp = this.createEmptyMovieClip("temp", 998);
bgConteneur._alpha = 0;
temp.onEnterFrame = function() {
    var loaded:Number = bgConteneur.getBytesLoaded();
    var total:Number = bgConteneur.getBytesTotal();
    var percent:Number = Math.round((loaded/total)*100);
    percent != isNaN ? preloader_mc.percent=percent+"%" : preloader_mc.percent=0;
    preloader_mc.percent = percent+"%";
    if (bgConteneur._width) {
        bgConteneur._alpha += 4;
        preloader_mc.tween(["_alpha"], [0], 2, "easeOutExpo");
        if (bgConteneur._alpha>100) {
            bgConteneur._alpha = 100;
            delete this.onEnterFrame;
            play();
        }
    }
};

Two things:
First my code wasn’t correct, it should read

isNaN(percent) ? preloader_mc.percent="0 %" : preloader_mc.percent=percent;

Second you have a line

 preloader_mc.percent = percent+"%";

skip this cause this one overwrites the if statement line that’s above it
So the complete code

var temp = this.createEmptyMovieClip("temp", 998);
bgConteneur._alpha = 0;
temp.onEnterFrame = function() {
    var loaded:Number = bgConteneur.getBytesLoaded();
    var total:Number = bgConteneur.getBytesTotal();
    var percent:Number = Math.round((loaded/total)*100);
    isNaN(percent) ? preloader_mc.percent="0 %" : preloader_mc.percent=percent;
    if (bgConteneur._width) {
        bgConteneur._alpha += 4;
        preloader_mc.tween(["_alpha"], [0], 2, "easeOutExpo");
        if (bgConteneur._alpha>100) {
            bgConteneur._alpha = 100;
            delete this.onEnterFrame;
            play();
        }
    }
};

scotty(-:

thanks very much Scotty ! it work perfectly now ! :beer::beer:

Please, write a book about learning and making good AS code ! i will buy it for sure !:slight_smile:

now, i can continue to developpe my personnal website ! let’s go !:red:

welcome =)