Actionscript 3 在AS3中卸载垃圾收集器的对象
我在flex builder中创建了一个名为loginInterface的对象。初始化此对象时,它将加载登录界面,创建按钮、输入框等 我为这个对象创建了一个名为unload的方法。我还有一个方法返回接口是否已加载。现在,我想卸载这个对象,这样屏幕不仅有空间容纳一个新的界面,而且还应该删除这些对象,以便重新分配内存 loginInterface.as类文件:Actionscript 3 在AS3中卸载垃圾收集器的对象,actionscript-3,flash,garbage-collection,Actionscript 3,Flash,Garbage Collection,我在flex builder中创建了一个名为loginInterface的对象。初始化此对象时,它将加载登录界面,创建按钮、输入框等 我为这个对象创建了一个名为unload的方法。我还有一个方法返回接口是否已加载。现在,我想卸载这个对象,这样屏幕不仅有空间容纳一个新的界面,而且还应该删除这些对象,以便重新分配内存 loginInterface.as类文件: package { //import required libraries import flash.display.Spr
package
{
//import required libraries
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
public class LoginInterface extends Sprite
{
private var welcomeLabel:CustomLabel;
private var versionLabel:CustomLabel;
private var loginButton:CustomButton;
private var registerButton:CustomButton;
private var usernameLabel:CustomLabel;
private var passwordLabel:CustomLabel;
private var usernameInputBox:CustomInputBox;
private var passwordInputBox:CustomInputBox;
private var loaded:Boolean = false;
public function LoginInterface()
{
trace("LoginInterface object loaded.");
init(); //initialize the login interface
}
public function init():void //initialize the login interface
{
trace("LoginInterface init method was called.");
//create objects
welcomeLabel = new CustomLabel(200, 100, 500, 30, "Welcome to the ALPHA client."); //welcome label
versionLabel = new CustomLabel(200, 120, 500, 30, "Version: v1.0.000", "Arial", 0x000000, 12); //version label
loginButton = new CustomButton(300, 300, 100, 30, "Login"); //login button
registerButton = new CustomButton(400, 300, 100, 30, "Register") //register button
usernameLabel = new CustomLabel(200, 200, 200, 30, "username: "); //username label
passwordLabel = new CustomLabel(200, 240, 200, 30, "password: "); //password label
usernameInputBox = new CustomInputBox(300, 200, 200, 30, false, 0x6D7B8D); //username input box
passwordInputBox = new CustomInputBox(300, 240, 200, 30, true, 0x6D7B8D); //password input box
//add objects to the display tree
addChild(welcomeLabel);
addChild(versionLabel);
addChild(loginButton);
addChild(registerButton);
addChild(usernameInputBox);
addChild(usernameLabel);
addChild(passwordInputBox);
addChild(passwordLabel);
//register object events
registerButton.addEventListener(MouseEvent.CLICK, registerClicked); //register button is clicked
loginButton.addEventListener(MouseEvent.CLICK, loginClicked); //login button is clicked
//interface has been loaded
loaded = true;
}
public function isLoaded():Boolean
{
if (loaded) return true;
return false;
}
public function unload():void
{
if (!loaded) return;
//welcomeLabel
removeChild(welcomeLabel);
welcomeLabel = null;
//versionLabel
removeChild(versionLabel);
versionLabel = null;
//loginButton
loginButton.removeEventListener(MouseEvent.CLICK, loginClicked);
removeChild(loginButton);
loginButton = null;
//registerButton
registerButton.removeEventListener(MouseEvent.CLICK, registerClicked);
removeChild(registerButton);
registerButton = null;
//usernameInputBox
removeChild(usernameInputBox);
usernameInputBox = null;
//usernameLabel
removeChild(usernameLabel);
usernameLabel = null;
//passwordInputBox
removeChild(passwordInputBox);
passwordInputBox = null;
//passwordLabel
removeChild(passwordLabel);
passwordLabel = null;
//set loaded to false
loaded = false;
}
private function loginClicked(event:MouseEvent):void
{
trace("Login button has been clicked.");
var username:String = usernameInputBox.text;
var password:String = passwordInputBox.text;
if (!checkLogin(username, password)) {
new Alert("Please enter a correct username and password.");
}
else {
trace("Creating socket connection.");
var socketConnection:SocketConnection = new SocketConnection(this);
}
}
private function checkLogin(username:String, password:String):Boolean
{
if ((username == "") || (password == "")) {
return false;
}
return true;
}
private function registerClicked(event:MouseEvent):void
{
trace("Register button has been clicked.");
var url:URLRequest = new URLRequest("http://url.com/client/register.php");
navigateToURL(url, "_blank");
}
}
}
请参阅加载对象的init函数和卸载对象的unload函数。这在视觉上是有效的。然而,这是最好的方法吗?一段时间后,垃圾收集器是否会将这些对象从内存中卸载?您卸载的代码将按预期工作。闪存垃圾收集器使用引用计数来确定特定对象是否是垃圾收集的候选对象。运行unload()函数后,在load()中创建的所有CustomButton对象的引用计数都将为0,最终将被垃圾收集 从其父对象的显示列表中删除对象(如LoginInterface)时,只要没有其他对象包含对子对象的引用,该对象中的所有子对象也将自动从内存中删除 因此,实际上不需要删除添加到LoginInterface的displayList中的每个对象。您只需要删除分配的事件侦听器 因此,您所做的一切都没有问题,但在
unload
方法中真正需要的是:
loginButton.removeEventListener(MouseEvent.CLICK, loginClicked);
registerButton.removeEventListener(MouseEvent.CLICK, registerClicked);
希望有帮助。LoginInterface对象已添加到其父对象的显示列表中。但是,我需要在logininterface对象的另一个子对象中卸载接口。我不认为这是可能的?所以我需要按照现在的方式来做,还是不做?@rhtx。在这种情况下,删除侦听器是一个好主意(通常是一种良好的做法),但要使LoginInterface符合GC的条件,这并不是严格必要的。