关闭/卸载 UiLoader as3
close / unload UiLoader as3
我正在使用 Loader
来显示 SWF。我有一个加载 SWF 的按钮,我希望同一个按钮关闭或隐藏或卸载 SWF。
这是我的部分代码:
var so:Boolean = false ;
glossary.addEventListener(MouseEvent.CLICK, glossaire)
function glossaire (e:MouseEvent) {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("glossary.swf");
if (so == false )
{
so = true ;
myLoader1.load(url);
addChild(myLoader);
}
else{
so = false ;
//i tried
myUILoader.unload();
removeChild(myUILoader);
// but my loader still appear in the stage how can remove or hide it
}
}
您的问题是您的加载程序仅限于点击功能,并且您每次点击都在创建一个新的加载程序(实际上您只想在第一次点击时创建一个新的加载程序)。此外,您似乎对加载器的名称感到困惑,因为您引用了 myLoader
、myLoader1
和 myUILoader
.
//put the loader var out of the function scope here (so it persists after the function finishes).
var myLoader:Loader;
glossary.addEventListener(MouseEvent.CLICK, glossaire);
function glossaire (e:MouseEvent){
//instead of using the so boolean, just check if loader is null
if (myLoader == null){
myLoader = new Loader();
myLoader.load(new URLRequest("glossary.swf"));
addChild(myLoader);
}else{
myLoader.unloadAndStop();
removeChild(myLoader);
myLoader = null; //set it to null now that it's been removed, so the next time this click function runs, a new loader will be made
}
}
如果您只是想隐藏加载的 swf(而不是完全卸载它),您可以这样做:
function glossaire (e:MouseEvent){
//create the loader and load if it hasn't been done yet
if (myLoader == null){
myLoader = new Loader();
myLoader.load(new URLRequest("glossary.swf"));
}
//the parent property of the loader will be null if it hasn't been added (or has been removed) via addChild/removeChild
if(myLoader.parent != null){
removeChild(myLoader);
}else{
addChild(myLoader);
}
}
好的,让我告诉你一个简单的方法来解决这个问题,
创建一个加载完成事件 Event.Complete
并监听一个函数,从那里设置一个布尔值,在您的情况下 var so:Boolean
并使用相同的功能,
var so:Boolean = false ;
glossary.addEventListener(MouseEvent.CLICK, glossaire)
function glossaire (e:MouseEvent) {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("glossary.swf");
myLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, swfload);
if (so == false )
{
so = true ;
myLoader.load(url);
addChild(myLoader);
}
else{
so = false ;
//i tried
myUILoader.unload();
removeChild(myUILoader);
// but my loader still appear in the stage how can remove or hide it
}
}
function swfload(e:Event):void
{
if(e.type == Complete)
so = false;
}
我正在使用 Loader
来显示 SWF。我有一个加载 SWF 的按钮,我希望同一个按钮关闭或隐藏或卸载 SWF。
这是我的部分代码:
var so:Boolean = false ;
glossary.addEventListener(MouseEvent.CLICK, glossaire)
function glossaire (e:MouseEvent) {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("glossary.swf");
if (so == false )
{
so = true ;
myLoader1.load(url);
addChild(myLoader);
}
else{
so = false ;
//i tried
myUILoader.unload();
removeChild(myUILoader);
// but my loader still appear in the stage how can remove or hide it
}
}
您的问题是您的加载程序仅限于点击功能,并且您每次点击都在创建一个新的加载程序(实际上您只想在第一次点击时创建一个新的加载程序)。此外,您似乎对加载器的名称感到困惑,因为您引用了 myLoader
、myLoader1
和 myUILoader
.
//put the loader var out of the function scope here (so it persists after the function finishes).
var myLoader:Loader;
glossary.addEventListener(MouseEvent.CLICK, glossaire);
function glossaire (e:MouseEvent){
//instead of using the so boolean, just check if loader is null
if (myLoader == null){
myLoader = new Loader();
myLoader.load(new URLRequest("glossary.swf"));
addChild(myLoader);
}else{
myLoader.unloadAndStop();
removeChild(myLoader);
myLoader = null; //set it to null now that it's been removed, so the next time this click function runs, a new loader will be made
}
}
如果您只是想隐藏加载的 swf(而不是完全卸载它),您可以这样做:
function glossaire (e:MouseEvent){
//create the loader and load if it hasn't been done yet
if (myLoader == null){
myLoader = new Loader();
myLoader.load(new URLRequest("glossary.swf"));
}
//the parent property of the loader will be null if it hasn't been added (or has been removed) via addChild/removeChild
if(myLoader.parent != null){
removeChild(myLoader);
}else{
addChild(myLoader);
}
}
好的,让我告诉你一个简单的方法来解决这个问题,
创建一个加载完成事件 Event.Complete
并监听一个函数,从那里设置一个布尔值,在您的情况下 var so:Boolean
并使用相同的功能,
var so:Boolean = false ;
glossary.addEventListener(MouseEvent.CLICK, glossaire)
function glossaire (e:MouseEvent) {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("glossary.swf");
myLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, swfload);
if (so == false )
{
so = true ;
myLoader.load(url);
addChild(myLoader);
}
else{
so = false ;
//i tried
myUILoader.unload();
removeChild(myUILoader);
// but my loader still appear in the stage how can remove or hide it
}
}
function swfload(e:Event):void
{
if(e.type == Complete)
so = false;
}