# Complex preloader

**URL:** <https://forum.kirupa.com/t/complex-preloader/263802>\
**Category:** Uncategorized\
**Created:** [June 19, 2008, 10:49am UTC](https://forum.kirupa.com/t/complex-preloader/263802 "2008-06-19T10:49:35Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 10:49am UTC](https://forum.kirupa.com/t/complex-preloader/263802/1 "2008-06-19T10:49:35Z")

</div>

(flash cs3/as2)

here what i have so far:

loaded\_bytes = Math.round(\_root.getBytesLoaded());  
total\_bytes = Math.round(\_root.getBytesTotal());  
loadpercent = (loaded\_bytes/total\_bytes)\*100;

//function to randomize the order of myArray  
function randmonArray() {  
r = function () {  
return random(2)\*2-1;  
};  
myArray.sort®;  
}

//myArray with 100 square names that is randomized  
var myArray:Array = [“s1”, “s2”, “s3”, “s4”, “s5”, …“s100”];  
randmonArray();

if (loaded\_bytes \< total\_bytes) {  
for (loadpercent; loadpercent \< myArray.length {  
var removedSquare = myArray.splice(0,1);  
unloadMovie(removedSquare);  
trace(Math.round(loadpercent) +"removed square is: " +removedSquare);  
}  
}  
else {  
gotoAndPlay(“start”);  
}

first question: How do i generate at array with 100 items “s1”, “s2”, etc rather than typing every item out by hand!

last question: I want the for loop to splice and unload once every time the loadpercent increases: so rather than have a bar increase in size every 1% that is loaded, i want one item to be spliced from the array and that is used to unload one movie every 1%.

i imagine that i will also need a way to find out how much is loaded instantly and unload clips at the start acoringly: splice and unload at the start so the items in the array match the loadpercent?

Im really close and its these last little things that I need some help on. I’d really appreciate an expert hand so i can get this thing ticking over.

Thanks  
Chris

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 19, 2008, 1:16pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/2 "2008-06-19T13:16:58Z")

</div>

```auto
// create array
var myArray:Array = new Array();
for (var i = 0; i <= 100; i++) {
	myArray[i - 1] = "s" + i;
}
trace(myArray);

```

I don’t see any point in splicing the array. Just loop through your array and place the movieclips onto the stage like so:

```auto
// create loading bar
for (var i = 0; i < 100; i++) {
	duplicateMovieClip(s, "s" + (i + 1), this.getNextHighestDepth()); // assumes an MC on stage called "s"
	this["s"+(i+1)]._x = 10 + (i * 5); // assumes "s" is 3 pxs wide and spaces each duplicate 5 px apart
}

```

Then you could use an onEnterFrame to remove each duplicate as the loadpercent increases

```auto
this.onEnterFrame = function() {
	removeMovieClip("s" + Math.round(loadpercent));
	if (Math.round(loadpercent) == 100) {
		gotoAndPlay("start");
	}
};

```

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 1:56pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/3 "2008-06-19T13:56:20Z")

</div>

😃 😃  
the first array works fantasticly, never again will i have to type pointlessly again!

The second bit works fine, i can prob work out how to get the code to arrange the clips how i want them, (in lines of 10 to create a 10X10 grid)

the last bit of code doesnt remove the clips. i think it removes one but doesnt seem to check to see if the value of loadpercent has changed?..im not sure.

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 19, 2008, 2:05pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/4 "2008-06-19T14:05:40Z")

</div>

I scribbled it all down off the top of my head…obviously you’ll also need to include the code that updates the value of loadpercent within the onEnterFrame handler.

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 2:06pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/5 "2008-06-19T14:06:16Z")

</div>

…the last bit of code doesn’t seem to be picking up every change in loadpercent, so it starts at 24 (because 24% is loaded instantly) 24 then comes up twice, so it seems to try and remove that clip twice. the next number traced is 31 and so on. so rather than all the clips being removed when loadpercent is at 100, loads are left behind.

Any ideas?

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 2:09pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/6 "2008-06-19T14:09:13Z")

</div>

this.onEnterFrame = function() {  
loaded\_bytes = Math.round(\_root.getBytesLoaded());  
total\_bytes = Math.round(\_root.getBytesTotal());  
loadpercent = (loaded\_bytes/total\_bytes)\*100;  
unloadMovie(“s” + Math.round(loadpercent));  
trace(Math.round(loadpercent));  
if (Math.round(loadpercent) == 100) {  
gotoAndPlay(“start”);  
}  
};

this traces:  
24,28,31,34,36,39,42,46,49,52,56,60,64,67,71,76,80,84,89,92,97,100

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 2:24pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/7 "2008-06-19T14:24:08Z")

</div>

…the reason for using an array splice was to get random clip names rather than getting them in order as it is doing now

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 19, 2008, 2:27pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/8 "2008-06-19T14:27:16Z")

</div>

Can you post your fla?

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 2:40pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/9 "2008-06-19T14:40:12Z")

</div>

[www.onemorechris.co.uk/pt3.fla](http://www.onemorechris.co.uk/pt3.fla)  
(deleted everything else in file but still to big to upload, its saved at the above link)

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 19, 2008, 3:09pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/10 "2008-06-19T15:09:10Z")

</div>

Should’ve said…it needs to be F8 for me to take a look…sorry.

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 3:20pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/11 "2008-06-19T15:20:55Z")

</div>

[www.onemorechris.co.uk/pt4.fla](http://www.onemorechris.co.uk/pt4.fla)  
^ that should work

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 19, 2008, 4:49pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/12 "2008-06-19T16:49:02Z")

</div>

I’ll take a look, although I can forsee a few problems that you might encounter because of the usual Flash preloading constraints. You’ve identified those problems in your first post. The biggest issue is making sure that your squares are loaded before the rest of the items in the fla, so that they can be positioned on the stage.

Creating the grid is quite straightforward though:

```auto
// create grid
initX = 0;
initY = 0;
counter = 0;
for (var i = 1; i <= 10; i++) {
	for (var j = 1; j <= 10; j++) {
		counter++;
		duplicateMovieClip(s, "square" + counter, counter); // assumes an instance of your square on the stage called "s" 
		gridSquare = this["square" + counter];
		trace (gridSquare)
		gridSquare._x = initX;
		gridSquare._y = initY;
		initX += 30;
	}
	initY += 30;
	initX = 0;
}

```

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 19, 2008, 5:05pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/13 "2008-06-19T17:05:09Z")

</div>

Aha…this tutorial is sure to help  
[http://www.kirupa.com/developer/mx/fadegrid.htm](http://www.kirupa.com/developer/mx/fadegrid.htm)

---

<div class="post-metadata">

**Author:** ![onemorechris](https://avatars.discourse-cdn.com/v4/letter/o/aeb1de/32.png) [@onemorechris](https://forum.kirupa.com/u/onemorechris)\
**Post date:** [June 19, 2008, 5:09pm UTC](https://forum.kirupa.com/t/complex-preloader/263802/14 "2008-06-19T17:09:35Z")

</div>

thank you for your efforts, they are very much welcomed. ill keep going, im sure ill get there

thanks again
