PDA

View Full Version : AS3 preloader problem



wanda
August 9th, 2007, 04:59 AM
Hey

I have a litte problem with my preloader. I want my loaderbar to be removed from the stage when the load is completed. I'm trying to do that with removeChild(_loaderBar);

It works with the textfield, but not with the loaderBar. Can anyone tell me why?



package {

import flash.display.*;
import flash.net.URLRequest;
import flash.events.*;
import flash.text.TextField;

public class test extends Sprite {

private var _loaderStatus:TextField;
private var _loaderBar:Sprite;

public function test() {

var loader:Loader = new Loader()
addChild(loader);

loader.contentLoaderInfo.addEventListener(Event.OP EN, handleOpen);
loader.contentLoaderInfo.addEventListener(Progress Event.PROGRESS, showLoad);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, onComplete);

loader.load(new URLRequest("okay.swf"));
}

public function loadBar(n:int):void{

_loaderBar = new Sprite();

_loaderBar.graphics.lineStyle();
_loaderBar.graphics.beginFill(0x000000);
_loaderBar.graphics.lineTo(n, 0);
_loaderBar.graphics.lineTo(n, 10);
_loaderBar.graphics.lineTo(0, 10);
_loaderBar.graphics.lineTo(0, 0);
_loaderBar.graphics.endFill();
_loaderBar.y = 30
_loaderBar.x = 40
addChild(_loaderBar);

}

private function handleOpen(event:Event):void {

_loaderStatus = new TextField();
addChild(_loaderStatus);
_loaderStatus.text = "Loading: 0%";

trace("open");
}

private function showLoad(event:ProgressEvent):void {

var percent:Number = Math.round((event.bytesLoaded / event.bytesTotal) * 100);
trace(percent)
_loaderStatus.text = "Loading: " + percent + "%";
loadBar(percent);
}

private function onComplete(event:Event):void {

loader.contentLoaderInfo.removeEventListener(Progr essEvent.PROGRESS, showLoad);
trace("swf is loaded");
removeChild(_loaderBar);
removeChild(_loaderStatus);

_loaderBar = null;
_loaderStatus = null;

}
}
}

gligy
August 9th, 2007, 05:34 AM
Ok try something like this :
make another variable of type Sprite (ex. : newLoaderBar),
and add it to the stage when is still nothing ( addChild(newLoaderBar);
And then in the function loadBar when you add the child add it to newLoaderBar (ex. : newLoaderBar.addChild(_loaderBar))
After you can remove it with - newLoaderBar.removeChild(_loaderBar);

This should work.

wanda
August 9th, 2007, 05:50 AM
The thing is that if i load a very small file it removes the loaderBar, but if i load a big picture the loaderBar is still there.

I tried your example, but it didnt work :\

gligy
August 9th, 2007, 06:49 AM
package {

import flash.display.*;
import flash.net.URLRequest;
import flash.events.*;
import flash.text.TextField;

public class test extends MovieClip {

private var _loaderStatus:TextField;
private var _loaderBar:Sprite;
private var loader:Loader;
private var myVar:Sprite;
public function test() {
myVar = new Sprite();
//addChild(myVar);
loader = new Loader();
addChild(loader);

loader.contentLoaderInfo.addEventListener(Event.OP EN, handleOpen);
loader.contentLoaderInfo.addEventListener(Progress Event.PROGRESS, showLoad);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, onComplete);

loader.load(new URLRequest("Rounding_FIN.swf"));
}

public function loadBar(n:int):void{

_loaderBar = new Sprite();

_loaderBar.graphics.lineStyle();
_loaderBar.graphics.beginFill(0x000000);
_loaderBar.graphics.lineTo(n, 0);
_loaderBar.graphics.lineTo(n, 10);
_loaderBar.graphics.lineTo(0, 10);
_loaderBar.graphics.lineTo(0, 0);
_loaderBar.graphics.endFill();
_loaderBar.y = 30
_loaderBar.x = 40
myVar.addChild(_loaderBar);
addChild(myVar);

}

private function handleOpen(event:Event):void {

_loaderStatus = new TextField();
myVar.addChild(_loaderStatus);
_loaderStatus.text = "Loading: 0%";
loader.contentLoaderInfo.removeEventListener(Event .OPEN, handleOpen);
trace("open123");
}

private function showLoad(event:ProgressEvent):void {

var percent:Number = Math.round((event.bytesLoaded / event.bytesTotal) * 100);
trace(percent);
_loaderStatus.text = "Loading: " + percent + "%";
loadBar(percent);
//loader.contentLoaderInfo.removeEventListener(Progr essEvent.PROGRESS, showLoad);
}

private function onComplete(event:Event):void {
//loader.contentLoaderInfo.removeEventListener(Progr essEvent.PROGRESS, showLoad);
loader.contentLoaderInfo.removeEventListener(Progr essEvent.PROGRESS, showLoad);
trace("swf is loaded");
trace(_loaderBar.visible);
//{
//_loaderBar.visible = false;
//}
removeChild(myVar);
//myVar.removeChild(_loaderBar);
// myVar.removeChild(_loaderStatus);

_loaderBar = null;
_loaderStatus = null;
loader.contentLoaderInfo.removeEventListener(Event .COMPLETE, onComplete);
}
}
}


this is working

gligy
August 9th, 2007, 06:51 AM
you can change the class from MovieClip to Sprite this will have no effect

wanda
August 9th, 2007, 07:37 AM
Great! .. That worked out just fine! Thanks a lot. Is it because i have to put it in some sort of a container? .. In this case, myVar?

gligy
August 9th, 2007, 07:45 AM
Yes and you have to add the container late on after you have had add to it the other items :)

wanda
August 9th, 2007, 08:03 AM
Thanks alot gigly :)