拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何在AS3中创建和洗掉许多子物件而不阻塞CPU或RAM

如何在AS3中创建和洗掉许多子物件而不阻塞CPU或RAM

白鹭 - 2022-01-25 2110 0 0

我正在为我正在设计的大富翁式游戏制作配套应用程序。我的目标是让所有属性都有一个 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很长时间了,但是没有经过正规培训,都是自学的。我已经尝试过很多次来了解“父母/孩子/私人班级/等”是如何作业的,但我无法掌握它。我想知道是否有更好的方法来做我想做的事情。

uj5u.com热心网友回复:

好的,让我写一些脚本来解释什么是OOP思维。另外,我强烈建议您阅读并理解MVC 模式的概念,因为下面的脚本代表[M]

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *