2010-12-16 70 views
1

我想複製文本字段。首先,我使用mc.getChildAt(0)獲取文本,然後將所有內容複製到新的文本字段中。我遇到的問題是,getChildAt從它所在的動畫片段中刪除文本字段。如何獲取文本字段的屬性而不移動它?或者也許這是別的,我所做的很好。任何有識之士將是一個巨大的幫助......as3複製文本字段而不從階段中刪除它

var dupeTField:MovieClip = duplicateTextField($value.sourceImg.getChildAt(0)); 

private function duplicateTextField($textField):MovieClip 
     { 
      var currTextField:TextField = $textField; 
      var dupeTextHolder:MovieClip = new MovieClip(); 
      var dupeTextField:TextField = new TextField(); 
      dupeTextField.text = currTextField.text; 
      dupeTextField.textColor = currTextField.textColor; 
      dupeTextField.width = $textField.width; 
      dupeTextField.height = $textField.height; 
      dupeTextHolder.addChild(dupeTextField); 


      return dupeTextHolder; 

     } 

回答

1

我想你會發現你的問題是其他地方。 getChildAt不會將其目標從其父項中移除,並且您發佈的函數的工作原理與我一樣,可以創建重複的剪輯而不會影響原始內容。

+0

您的權利,問題是其他地方,但感謝您的幫助。 – pfunc 2010-12-16 19:51:36

0
private var dupeTField:MovieClip; 

    private function init():void 
    {   
     //getChildAt will return a DisplayObject so you 
     //should cast the return DisplayObject as a TextField 
     var tf:TextField = $value.sourceImg.getChildAt(0) as TextField; 

     dupeTField = duplicateTextField(tf); 

     //don't forget to add your duplicate to the Display List 
     //& make sure to change the x, y properties so that 
     //it doesn't sit on top of the original 
     addChild(dupeTField); 

    } 

    private function duplicateTextField(textField:TextField):MovieClip 
    { 
     var dupeTextHolder:MovieClip = new MovieClip(); 
     var dupeTextField:TextField = new TextField(); 

     //if you pass a TextField as a parameter, you don't need to 
     //replicate the instance inside the function, simply access the 
     //parameter directly. 
     //You may consider copying the TextFormat as well 
     dupeTextField.defaultTextFormat = textfield.defaultTextFormat; 

     dupeTextField.text = textField.text; 
     dupeTextField.textColor = textField.textColor; 
     dupeTextField.width = textField.width; 
     dupeTextField.height = textField.height; 

     dupeTextHolder.addChild(dupeTextField); 


     return dupeTextHolder; 

    } 
2

使用這樣的事情:

package com.ad.common { 
    import flash.text.TextField; 
    import flash.utils.describeType; 

    public function cloneTextField(textField:TextField, replace:Boolean = false):TextField { 
     var clone:TextField = new TextField(); 
     var description:XML = describeType(textField); 
     for each (var item:XML in description.accessor) { 
      if ([email protected] != 'readonly') { 
       try { 
        clone[[email protected]] = textField[[email protected]]; 
       } catch(error:Error) { 
        // N/A yet. 
       } 
      } 
     } 
     clone.defaultTextFormat = textField.getTextFormat(); 
     if (textField.parent && replace) { 
      textField.parent.addChild(clone); 
      textField.parent.removeChild(textField); 
     } 
     return clone; 
    } 
}