2015-03-30 125 views
0

我已經在舞臺上添加了許多對象(在Achievements.as類中)來創建成就板。當有人按下後退按鈕時,我想從main.as類中刪除所有的孩子。另外,有沒有辦法創建一個自定義textField類,其中每個textField.selectable = false,而不必分別分配給每個。刪除不同級別的孩子As3

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: Access of possibly undefined property descripTxt through a reference with static type Achievements.

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刪除兒童類

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]); 
    } 
}} 

} 的陣列descriptext:數組acharr:數組titletext:陣列中的所有在他們正確的內容,但它是大量的文字,所以我把它張貼此

這裏是我使用從Main.as

除去孩子的代碼
+0

是否在添加之前刪除它們? – Xela 2015-03-31 00:14:13

+0

@Xela我將他們刪除後,但在不同的類 – TomMcmann 2015-03-31 03:35:09

回答

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,因爲我沒有這些源代碼。

+0

你會如何將他們從主類中刪除? – TomMcmann 2015-03-31 03:33:17

+0

我以爲你試圖刪除Main.as中的textfields。如果你想從Main.as中移除Achievement.as中的textfields,你可以在你的類Achievement.as中添加一個方法,名爲RemoveTextboxes(),然後從Main.as中調用myAchievementClassInstance.RemoveTextBoxes(); – 2015-03-31 19:03:57

+0

也許多一點的代碼可能會有所幫助。 – 2015-03-31 19:04:22

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到矢量陣列?

+0

我已經試過了,它不工作 – TomMcmann 2015-03-31 03:49:10

+0

它得到了同樣的錯誤作爲一個我最初發布的 – TomMcmann 2015-03-31 03:50:34

+0

你是否將這些DisplayObject添加到你的矢量數組中? – Xela 2015-03-31 04:00:50