删除不同类别的孩子As3

时间:2015-03-30 23:37:29

标签: actionscript-3 flash

我在舞台上(在Achievements.as类中)添加了许多对象来创建成就板。当有人按下后退按钮时,我想从main.as类中删除所有子节点。另外,有没有办法创建一个自定义textField类,其中每个 textField.selectable = false ,而不必单独将它分配给每个。

public static var texts:Vector.<TextField> = new Vector.<TextField>();
public static var titleTxt:Vector.<TextField> = new Vector.<TextField>();
public static var descripTxt:Vector.<TextField> = new Vector.<TextField>();
public static var rewardTxt:Vector.<TextField> = new Vector.<TextField>();
public static var achCoins:Vector.<MovieClip> = new Vector.<MovieClip>();

for (var j:int = 0; j < 30; j++)
        {
            achCoins[j].x = 240;
            achCoins[j].y = 45 + j * 70;
            addChild(achCoins[j]);
            descripTxt[j].x = 0;
            descripTxt[j].y = 30 + 70 * j;
            //descripTxt[j].antiAliasType = AntiAliasType.ADVANCED;
            descripTxt[j].defaultTextFormat = tf2;
            descripTxt[j].text = descriptext[j];
            descripTxt[j].embedFonts = true;
            addChild(descripTxt[j]);
            titleTxt[j].x = 0;
            titleTxt[j].y = j * 70;
            //titleTxt[j].antiAliasType = AntiAliasType.ADVANCED;
            titleTxt[j].defaultTextFormat = tf3;
            titleTxt[j].text = titletext[j];
            titleTxt[j].embedFonts = true;
            addChild(titleTxt[j]);
            rewardTxt[j].x = 200;
            rewardTxt[j].y = 30 + j * 70;
            rewardTxt[j].text = reward[j].toString();
            rewardTxt[j].setTextFormat(tf1);
            rewardTxt[j].embedFonts = true;
            rewardTxt[j].textColor = 0x000000;
            addChild(rewardTxt[j]);
            texts[j].text = Main.achievement[j] + "/" + totnum[j];
            texts[j].setTextFormat(tf1);
            texts[j].x = 200;
            texts[j].y = j * 70;
            texts[j].embedFonts = true;
            addChild(texts[j]);

        }

然后我使用此代码删除孩子

for (var k:int = 0; k < 30; k++)
        {
            removeChild(achievementback.descripTxt[k]);
            removeChild(achievementback.titleTxt[k]);
            removeChild(achievementback.rewardTxt[k]);
            removeChild(achievementback.texts[k]);

        }

我收到错误

  

1119:通过a访问可能未定义的属性descripTxt   参考静态类型成就。

for (var i:int = 0; i < 30; i++)
        {
            texts.push(new TextField());
            titleTxt.push(new TextField());
            rewardTxt.push(new TextField());
            descripTxt.push(new TextField());
            achCoins.push(new coinSmall());
        {

在Achievements.as Class

public function RemoveTextboxes(){
for (var k:int = 0; k < 30; k++)
    {
       removeChild(descripTxt[k]);
       removeChild(titleTxt[k]);
       removeChild(rewardTxt[k]);
       removeChild(texts[k]);
    }
}

在Main.as类中

achievementback.RemoveTextBoxes()

这是我的整个成就课

package 
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
import flash.display.Graphics;
import flash.text.AntiAliasType;
public class Achievements extends MovieClip
{
    public var backRect:Shape = new Shape();
    public var coinsmall:MovieClip = new coinSmall();
    public var titleText:MyTextField = new MyTextField();
    public var descripText:MyTextField = new MyTextField();
    public var reward_txt:MyTextField = new MyTextField();
    public static var texts:Vector.<TextField> = new Vector.<TextField>();
    public static var titleTxt:Vector.<TextField> = new Vector.<TextField>();
    public static var descripTxt:Vector.<TextField> = new Vector.<TextField>();
    public static var rewardTxt:Vector.<TextField> = new Vector.<TextField>();
    public static var achCoins:Vector.<MovieClip> = new Vector.<MovieClip>();
    public var tf1:TextFormat = new TextFormat();
    public var tf2:TextFormat = new TextFormat();
    public var tf3:TextFormat = new TextFormat();
    //Achievement Variables
    public var descriptext:Array = new Array();
    public static var acharr:Array = new Array();
    public var titletext:Array = new Array();
    public var reward:Array = new Array(25,50,250,500,200,2000,10,100,500,10,25,50,10,25,100,25,25,50,50,100,500,500,5,5,5,50,10,50,100,250);


    public function Achievements()
    {
        //Defining Objects
        //Set Up Text Formats
        //Text Format 1
        tf1.font = "Myriad pro";
        tf1.size = 20;
        tf1.color = 0x660000;
        tf1.align = "right";
        //Text Format 2
        tf2.size = 16;
        tf2.color = 0xB8461D;
        tf2.font = "Myriad Pro";
        tf2.align = "center";
        //Text Format 3
        tf3.size = 23;
        tf3.color = 0x660000;
        tf3.font = "Myriad Pro";
        tf3.align = "center";
        //Text Box: Fraction Completed

        //Text Box: Reward Amount For Each Achievement
        //Text Box: Description of Achievement

        for (var i:int = 0; i < 30; i++)
        {
            texts.push(new TextField());
            titleTxt.push(new TextField());
            rewardTxt.push(new TextField());
            descripTxt.push(new TextField());
            achCoins.push(new coinSmall());
            backRect.graphics.beginFill(0x86B46D, 0.7);
            backRect.graphics.drawRoundRect(0, i*70, 300, 60, 10, 10);
            backRect.graphics.endFill();
            addChild(backRect);
        }

        //change the text in a loop;
        var acharr:Array = new Array();
        var totnum:Array = new Array(50,100,500,1000,5,10,10,100,500,25,50,100,1,1,3,1,50,100,50,100,2,5,5,5,5,20,10,50,100,250);
        for (var j:int = 0; j < 30; j++)
        {
            achCoins[j].x = 240;
            achCoins[j].y = 45 + j * 70;
            addChild(achCoins[j]);
            descripTxt[j].width = 225;
            descripTxt[j].height = 30;
            descripTxt[j].x = 0;
            descripTxt[j].y = 30 + 70 * j;
            //descripTxt[j].antiAliasType = AntiAliasType.ADVANCED;
            descripTxt[j].defaultTextFormat = tf2;
            descripTxt[j].text = descriptext[j];
            descripTxt[j].embedFonts = true;
            addChild(descripTxt[j]);
            titleTxt[j].width = 225;
            titleTxt[j].height = 30;
            titleTxt[j].x = 0;
            titleTxt[j].y = j * 70;
            //titleTxt[j].antiAliasType = AntiAliasType.ADVANCED;
            titleTxt[j].defaultTextFormat = tf3;
            titleTxt[j].text = titletext[j];
            titleTxt[j].embedFonts = true;
            addChild(titleTxt[j]);
            rewardTxt[j].width = 100;
            rewardTxt[j].height = 30;
            rewardTxt[j].selectable = false;
            rewardTxt[j].x = 200;
            rewardTxt[j].y = 30 + j * 70;
            rewardTxt[j].text = reward[j].toString();
            rewardTxt[j].setTextFormat(tf1);
            rewardTxt[j].embedFonts = true;
            rewardTxt[j].textColor = 0x000000;
            addChild(rewardTxt[j]);
            texts[j].text = acharr[j] + "/" + totnum[j];
            texts[j].setTextFormat(tf1);
            texts[j].width = 100;
            texts[j].height = 30;
            texts[j].selectable = false;
            texts[j].x = 200;
            texts[j].y = j * 70;
            texts[j].embedFonts = true;
            addChild(texts[j]);

        }
    }
public function RemoveTextboxes(){
for (var k:int = 0; k < 30; k++)
    {
       removeChild(descripTxt[k]);
       removeChild(titleTxt[k]);
       removeChild(rewardTxt[k]);
       removeChild(texts[k]);
    }
}}

} 数组描述文本:数组acharr:数组titletext:数组所有内容都有正确的内容,但它是很多文本所以我把它拿出来发布这个

以下是我用来从Main.as中删除子项的代码

function achievementExit():void
    {
        removeChild(back1);
        removeChild(coin_sm);
        removeChild(coinAmt);
        removeChild(completed);
        removeChild(achTit);
        removeChild(achback);
        achievementback.RemoveTextBoxes()
        removeChild(achievementback)
        removeChild(scrollPane);
    }

2 个答案:

答案 0 :(得分:0)

我不得不稍微修改代码并注释掉你在框中设置文本的位置以便运行(因为我错过了一些代码)但是我的问题似乎是文本框被删除但是你的绿色形状停留在舞台上。添加了一个向量来保存所有这些并将它们添加到remove函数。

package {
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.Shape;
    import flash.display.Graphics;
    import flash.text.AntiAliasType;
    public class Achievements extends MovieClip
    {
        public var coinsmall:MovieClip = new MovieClip();
        public var titleText:TextField = new TextField();
        public var descripText:TextField = new TextField();
        public var reward_txt:TextField = new TextField();
        public static var texts:Vector.<TextField> = new Vector.<TextField>();
        public static var titleTxt:Vector.<TextField> = new Vector.<TextField>();
        public static var descripTxt:Vector.<TextField> = new Vector.<TextField>();
        public static var rewardTxt:Vector.<TextField> = new Vector.<TextField>();
        public static var achCoins:Vector.<MovieClip> = new Vector.<MovieClip>();
        public static var graphix:Vector.<Shape> = new Vector.<Shape>();
        public var tf1:TextFormat = new TextFormat();
        public var tf2:TextFormat = new TextFormat();
        public var tf3:TextFormat = new TextFormat();
        //Achievement Variables
        public var descriptext:Array = new Array();
        public static var acharr:Array = new Array();
        public var titletext:Array = new Array();
        public var reward:Array = new Array(25,50,250,500,200,2000,10,100,500,10,25,50,10,25,100,25,25,50,50,100,500,500,5,5,5,50,10,50,100,250);


        public function Achievements()
        {
            //Defining Objects
            //Set Up Text Formats
            //Text Format 1
            tf1.font = "Myriad pro";
            tf1.size = 20;
            tf1.color = 0x660000;
            tf1.align = "right";
            //Text Format 2
            tf2.size = 16;
            tf2.color = 0xB8461D;
            tf2.font = "Myriad Pro";
            tf2.align = "center";
            //Text Format 3
            tf3.size = 23;
            tf3.color = 0x660000;
            tf3.font = "Myriad Pro";
            tf3.align = "center";

            for (var i:int = 0; i < 30; i++)
            {
                descriptext.push("THIS IS " + i);
                texts.push(new TextField());
                titleTxt.push(new TextField());
                rewardTxt.push(new TextField());
                descripTxt.push(new TextField());
                achCoins.push(new MovieClip());
                var backRect:Shape = new Shape();
                backRect.graphics.beginFill(0x86B46D, 0.7);
                backRect.graphics.drawRoundRect(0, i*70, 300, 60, 10, 10);
                backRect.graphics.endFill();
                graphix.push(backRect);
                addChild(backRect);
            }

            //change the text in a loop;
            var acharr:Array = new Array();
            var totnum:Array = new Array(50,100,500,1000,5,10,10,100,500,25,50,100,1,1,3,1,50,100,50,100,2,5,5,5,5,20,10,50,100,250);
            for (var j:int = 0; j < 30; j++)
            {
                achCoins[j].x = 240;
                achCoins[j].y = 45 + j * 70;
                addChild(achCoins[j]);
                descripTxt[j].width = 225;
                descripTxt[j].height = 30;
                descripTxt[j].x = 0;
                descripTxt[j].y = 30 + 70 * j;
                //descripTxt[j].antiAliasType = AntiAliasType.ADVANCED;
                descripTxt[j].defaultTextFormat = tf2;
                //descripTxt[j].text = descriptext[j];
                descripTxt[j].embedFonts = true;
                addChild(descripTxt[j]);
                titleTxt[j].width = 225;
                titleTxt[j].height = 30;
                titleTxt[j].x = 0;
                titleTxt[j].y = j * 70;
                //titleTxt[j].antiAliasType = AntiAliasType.ADVANCED;
                titleTxt[j].defaultTextFormat = tf3;
                //titleTxt[j].text = titletext[j];
                titleTxt[j].embedFonts = true;
                addChild(titleTxt[j]);
                rewardTxt[j].width = 100;
                rewardTxt[j].height = 30;
                rewardTxt[j].selectable = false;
                rewardTxt[j].x = 200;
                rewardTxt[j].y = 30 + j * 70;
                //rewardTxt[j].text = reward[j].toString();
                rewardTxt[j].setTextFormat(tf1);
                rewardTxt[j].embedFonts = true;
                rewardTxt[j].textColor = 0x000000;
                addChild(rewardTxt[j]);
                //texts[j].text = acharr[j] + "/" + totnum[j];
                texts[j].setTextFormat(tf1);
                texts[j].width = 100;
                texts[j].height = 30;
                texts[j].selectable = false;
                texts[j].x = 200;
                texts[j].y = j * 70;
                texts[j].embedFonts = true;
                addChild(texts[j]);

            }
        }

        public function RemoveTextboxes(){
        trace("removing")
        for (var k:int = 0; k < 30; k++)
            {
               removeChild(descripTxt[k]);
               removeChild(titleTxt[k]);
               removeChild(rewardTxt[k]);
               removeChild(texts[k]);
                removeChild(graphix[k]);
            }
        }
    }
}

我还必须将自定义类更改回MovieClip和TextBox,因为我没有源代码。

答案 1 :(得分:0)

应该是:

for (var k:int = 0; k < 30; k++)
        {
            achievementback.removeChild(achievementback.descripTxt[k]);
            achievementback.removeChild(achievementback.titleTxt[k]);
            achievementback.removeChild(achievementback.rewardTxt[k]);
            achievementback.removeChild(achievementback.texts[k]);
        }

<强>更新 您是否将这些DisplayObject添加到矢量数组?