如何创建和删除许多子对象而不会使 AS3 中的 CPU 或 RAM 陷入困境

How to create and remove many Child Objects without bogging down the CPU or RAM in AS3

我正在为我正在设计的大富翁式游戏制作配套应用。我的目标是让所有属性都有一个 MovieClip,显示谁拥有它以及升级了多少。实际上有数百个属性,每个属性都会使我的 运行 时间缩短大约一秒(我设置了一个 1 秒的计时器来查看 运行 需要多长时间)。

我有一个 MovieClip,它会根据需要在每个 属性 中“生成”。我的想法是,我有一个按钮告诉这个 MC 转到第 10 帧,第 10 帧将包含为 属性 10 添加子项的代码,然后该子项包含所有必要的代码。这个子生成 MC 看起来像这样:

import flash.events.MouseEvent;
import flash.events.Event;

stop();
// I used a "fakeChild" as a placeholder to get something spawned in so that there is something to be removed
var fake_Child: fakeChild = new fakeChild(); 

var property_Pale15: propertyPale15 = new propertyPale15();
var property_Red15: propertyRed15 = new propertyRed15();

removeChildAt(1);
addChildAt(fake_Child, 1);

给定 属性 的每个后续帧看起来像这样(本例中的 属性 是“Pale 15”)

removeChildAt(1);
addChildAt(property_Pale15, 1);
property_Pale15.newData();
property_Pale15.propertyOwner();

我目前只有两个测试属性,每个都让我的计时器慢了大约一秒,这非常糟糕。属性本身有大量的代码,看起来像这样:

import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.Event;

stop();

var myPropertyData:SharedObject = SharedObject.getLocal("myLocalData");

var propertyName: String = ("Pale 15");
propertyDisplay.text = propertyName;

var propertyValue: int;

var propertyRent: int;
var activePlayer: int;
var currentRank: int;

loadData();

btnProperty.addEventListener(MouseEvent.CLICK, activateProperty);

function activateProperty(e:MouseEvent):void{

    propertyValue = MovieClip(root).valuePale15;    
    
    if(MovieClip(root).buyupgradeAction == true && loanedProperty.visible == false){
        buyupgradeProperty();
    }else if(MovieClip(root).paycostAction == true && bridgeProperty.visible == false && loanedProperty.visible == false){
        paycostProperty();
    }
}

function buyupgradeProperty(e:MouseEvent = null){
    
// Active Player (if Owner) Upgrades Property
    
    if(activePlayer == 1 && MovieClip(root).activePlayer == 1 && currentRank < 10){
        MovieClip(root).myLocalData.data.moneyDisplay1 -= propertyValue * 0.8;
        currentRank += 1;
        currentRankDisplay.text = currentRank;
        propertyRent = propertyValue * currentRank * currentRank * 0.1;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay1 + " upgraded " + propertyName + " to Rank " + currentRank + " for $" + propertyValue * 0.8 + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(activePlayer == 2 && MovieClip(root).activePlayer == 2 && currentRank < 10){
        MovieClip(root).myLocalData.data.moneyDisplay2 -= propertyValue * 0.8;
        currentRank += 1;
        currentRankDisplay.text = currentRank;
        propertyRent = propertyValue * currentRank * currentRank * 0.1;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay2 + " upgraded " + propertyName + " to Rank " + currentRank + " for $" + propertyValue * 0.8 + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(activePlayer == 3 && MovieClip(root).activePlayer == 3 && currentRank < 10){
        MovieClip(root).myLocalData.data.moneyDisplay3 -= propertyValue * 0.8;
        currentRank += 1;
        currentRankDisplay.text = currentRank;
        propertyRent = propertyValue * currentRank * currentRank * 0.1;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay3 + " upgraded " + propertyName + " to Rank " + currentRank + " for $" + propertyValue * 0.8 + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(activePlayer == 4 && MovieClip(root).activePlayer == 4 && currentRank < 10){
        MovieClip(root).myLocalData.data.moneyDisplay4 -= propertyValue * 0.8;
        currentRank += 1;
        currentRankDisplay.text = currentRank;
        propertyRent = propertyValue * currentRank * currentRank * 0.1;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay4 + " upgraded " + propertyName + " to Rank " + currentRank + " for $" + propertyValue * 0.8 + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }
    
// Active Player buys Property
    
    if(MovieClip(root).activePlayer == 1 && currentFrame == 1){
        MovieClip(root).myLocalData.data.moneyDisplay1 -= propertyValue;
        MovieClip(root).myLocalData.data.propertyDisplay1 += 1;
        gotoAndStop(2);
        MovieClip(root).stageProperties.propertyColor.color = (0xFF0000);
        colorChange();
        propertyRent = 0;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay1 + " purchased " + propertyName + " for $" + propertyValue + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 2 && currentFrame == 1){
        MovieClip(root).myLocalData.data.moneyDisplay2 -= propertyValue;
        MovieClip(root).myLocalData.data.propertyDisplay2 += 1;
        gotoAndStop(3);
        MovieClip(root).stageProperties.propertyColor.color = (0x0000FF);
        colorChange();
        propertyRent = 0;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay2 + " purchased " + propertyName + " for $" + propertyValue + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 3 && currentFrame == 1){
        MovieClip(root).myLocalData.data.moneyDisplay3 -= propertyValue;
        MovieClip(root).myLocalData.data.propertyDisplay3 += 1;
        gotoAndStop(4);
        MovieClip(root).stageProperties.propertyColor.color = (0x00FF00);
        colorChange();
        propertyRent = 0;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay3 + " purchased " + propertyName + " for $" + propertyValue + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 4 && currentFrame == 1){
        MovieClip(root).myLocalData.data.moneyDisplay4 -= propertyValue;
        MovieClip(root).myLocalData.data.propertyDisplay4 += 1;
        gotoAndStop(5);
        MovieClip(root).stageProperties.propertyColor.color = (0xFFFF00);
        colorChange();
        propertyRent = 0;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay4 + " purchased " + propertyName + " for $" + propertyValue + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }
    
    saveData();
}

function paycostProperty(e:MouseEvent = null){
    
    if(MovieClip(root).activePlayer == 1 && activePlayer == 2){
        MovieClip(root).myLocalData.data.moneyDisplay1 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay2 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay1 + " paid " + MovieClip(root).myLocalData.data.nameDisplay2 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 1 && activePlayer == 3){
        MovieClip(root).myLocalData.data.moneyDisplay1 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay3 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay1 + " paid " + MovieClip(root).myLocalData.data.nameDisplay3 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 1 && activePlayer == 4){
        MovieClip(root).myLocalData.data.moneyDisplay1 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay4 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay1 + " paid " + MovieClip(root).myLocalData.data.nameDisplay4 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 2 && activePlayer == 1){
        MovieClip(root).myLocalData.data.moneyDisplay2 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay1 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay2 + " paid " + MovieClip(root).myLocalData.data.nameDisplay1 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 2 && activePlayer == 3){
        MovieClip(root).myLocalData.data.moneyDisplay2 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay3 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay2 + " paid " + MovieClip(root).myLocalData.data.nameDisplay3 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 2 && activePlayer == 4){
        MovieClip(root).myLocalData.data.moneyDisplay2 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay4 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay2 + " paid " + MovieClip(root).myLocalData.data.nameDisplay4 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 3 && activePlayer == 1){
        MovieClip(root).myLocalData.data.moneyDisplay3 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay1 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay3 + " paid " + MovieClip(root).myLocalData.data.nameDisplay1 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 3 && activePlayer == 2){
        MovieClip(root).myLocalData.data.moneyDisplay3 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay2 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay3 + " paid " + MovieClip(root).myLocalData.data.nameDisplay2 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 3 && activePlayer == 4){
        MovieClip(root).myLocalData.data.moneyDisplay3 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay4 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay3 + " paid " + MovieClip(root).myLocalData.data.nameDisplay4 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 4 && activePlayer == 1){
        MovieClip(root).myLocalData.data.moneyDisplay4 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay1 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay4 + " paid " + MovieClip(root).myLocalData.data.nameDisplay1 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 4 && activePlayer == 2){
        MovieClip(root).myLocalData.data.moneyDisplay4 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay2 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay4 + " paid " + MovieClip(root).myLocalData.data.nameDisplay2 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }else if(MovieClip(root).activePlayer == 4 && activePlayer == 3){
        MovieClip(root).myLocalData.data.moneyDisplay4 -= propertyRent;
        MovieClip(root).myLocalData.data.moneyDisplay3 += propertyRent;
        MovieClip(root).transactionLog = (MovieClip(root).myLocalData.data.nameDisplay4 + " paid " + MovieClip(root).myLocalData.data.nameDisplay3 + " $" + propertyRent + " for landing on " + propertyName + " (" + MovieClip(root).Time_txt.text + ")")
        MovieClip(root).updateLogs();
    }
    
    saveData();
}

function saveData(e:MouseEvent = null){
    myPropertyData.data.Pale15Rent = propertyRent;
    myPropertyData.data.Pale15Rank = currentRank;
    myPropertyData.data.Pale15Frame = currentFrame;
    myPropertyData.data.Pale15Loan = loanedProperty.visible;
    myPropertyData.data.Pale15Bridge = bridgeProperty.visible;
    
    myPropertyData.flush();
}

function loadData(e:Event = null){
    propertyRent = myPropertyData.data.Pale15Rent;
    currentRank = myPropertyData.data.Pale15Rank;
    gotoAndStop(myPropertyData.data.Pale15Frame);
    loanedProperty.visible = myPropertyData.data.Pale15Loan;
    bridgeProperty.visible = myPropertyData.data.Pale15Bridge;
}

function newData(e:Event = null){
    if(MovieClip(root).gameSave == 1){
        myPropertyData.data.Pale15Rent = 0;
        myPropertyData.data.Pale15Rank = 0;
        myPropertyData.data.Pale15Frame = 1;
        myPropertyData.data.Pale15Loan = false;
        myPropertyData.data.Pale15Bridge = false;
        loadData();
    }
}

function propertyOwner(e:Event = null){
    if(activePlayer == 1){
        MovieClip(root).stageProperties.propertyColor.color = (0xFF0000);
        colorChange();
    }else if(activePlayer == 2){
        MovieClip(root).stageProperties.propertyColor.color = (0x0000FF);
        colorChange();
    }else if(activePlayer == 3){
        MovieClip(root).stageProperties.propertyColor.color = (0x00FF00);
        colorChange();
    }else if(activePlayer == 4){
        MovieClip(root).stageProperties.propertyColor.color = (0xFFFF00);
        colorChange();
    }
}

function colorChange (e:MouseEvent = null){
    MovieClip(root).stageProperties.Pale15.transform.colorTransform = MovieClip(root).stageProperties.propertyColor;
}

我试图让代码尽可能通用,以便我可以复制和粘贴其他属性。目前,这个 属性 可以被购买、升级,并根据激活的按钮向合适的玩家出租,以及告诉“属性 显示器”改变棋盘上的颜色基于谁拥有什么。

注:我用了很长时间的AS3,但是没有经过正规培训,都是自学的。我已经尝试了很多次来理解“parent/child/private class/etc”是如何工作的,但我无法理解它。我想知道是否有更好的方法来做我想做的事情。

好吧,让我写一些脚本来解释什么是OOP-thinking。此外,我强烈建议您阅读并理解MVC pattern的概念,因为下面的脚本代表[M][C],而 [V] 并不是很重要,以后添加它也不是太困难,只要您直接了解应用程序的架构即可。

首先,让我们定义游戏板单元格,玩家可以经过或访问的地方。

package
{
    // A data [M]odel class.
    public class Place
    {
        // What do we call this place.
        public var title:String = "";
        
        // A guest list.
        public var visitors:Array = new Array;
        
        // The Person class will be defined later.
        
        public function visit(who:Person):void
        {
            // Add the guest to the list of visitors.
            visitors.push(who);
        }
        
        public function leave(who:Person):void
        {
            // Remove the visitor and remove its location record.
            var anIndex:int = visitors.indexOf(who);
            
            visitors.splice(anIndex, 1);
        }
    }
}

Person class 这样它就可以访问和离开地方了。

package
{
    // A data [M]odel class.
    public class Person
    {
        // Personal name.
        public var title:String = "";
        
        // A place of residence.
        public var stayingAt:Place;
    }
}

为了将它们粘在一起,我们需要一些控制器,允许将人们从一个地方移动到另一个地方。这个想法是 NEVER 解决 [M] 对象 PersonPlace,所以下面的 [C] 脚本确保 Person 总是正确地离开和访问,不会坐超过 10 地方

package
{
    // A [C]ontroller class.
    public class City
    {
        // City name.
        public var title:String = "";
        
        public function move(who:Person, where:Place):void
        {
            // Remove the guest from where it is currently staying.
            if (who.stayingAt)
            {
                who.stayingAt.leave(who);
                who.stayingAt = null;
            }
            
            where.visit(who);
            who.stayingAt = where;
        }
    }
}

现在,就大富翁而言,建筑是什么?它是一种 Place,但具有更复杂的行为。那么一个Player就是一个Person,有钱,可以拥有建筑物蚂蚁轮流做事

package
{
    // A data [M]odel class.
    public class Player extends Person
    {
        public var money:int;
        
        public var color:uint;
        
        public var turnsToAct:int;
        
        // A list of owned buildings.
        public var owns:Array = new Array;
        
        // Charges money from the Player
        // then returns how much was actually charged.
        public function charge(value:int):int
        {
            var acharge:int = 0;

            if (value <= 0)
            {
                // You cannot charge non-positive sums.
            }
            else if (money - value < 0)
            {
                // Bankruptcy handling block.
                acharge = Math.max(0, money);
                turnsToAct = 1000000;
                money -= value;
            }
            else
            {
                acharge = value;
                money -= value;
            }
            
            return acharge;
        }
    }
}

那么,建筑物就像...

package
{
    // A data [M]odel class.
    public class Building extends Place
    {
        public var owner:Player;
        public var color:uint;
        
        public var cost:int;
        public var rent:int;
        
        public function purchase(who:Player):void
        {
            who.charge(cost);
            
            owner = who;
            color = owner.color;
        }
        
        // Building can take rent from a visiting Player.
        // So, the "visit" method needs some additional behavior.
        override public function visit(who:Person):void
        {
            // Invoke previously defined visit logic.
            super.visit(who);
            
            // If the Building is not owned, there's no rent.
            // There's also no rent, if the owner visits.
            if (!owner || who == owner) return;
            
            // Person class does not have "charge" method. Read up "typecasting".
            
            // The following one line is actually that HORRIBLE block
            // of code named "paycostProperty" in your question.
            owner.money += (who as Player).charge(rent);
            // Seriously.
        }
    }
}

所以,还有一些事情要做(我越来越懒了,不打算在这里编写整个大富翁的程序):

  • 设计 City.init() 方法,以便它创建一个 Player 的有序列表,一个 Player 的有序列表=30=]放置和建筑并将所有玩家移动到第一个单元格。
  • 编写控制回合的逻辑和 玩家 轮到的流程:投掷骰子、移动、做出决定、完成轮到。
  • 设计 [V] 组件以显示 Players 的统计数据,Places 建筑物及其所有可能的状态,城市可视化事物的整体状态。