开发者

get a display object on stage/root?

开发者 https://www.devze.com 2023-02-07 13:06 出处:网络
how can I get a display object on stage/root from a class? the text field txt is in root, but how can I get it from a class?

how can I get a display object on stage/root from a class?

the text field txt is in root, but how can I get it from a class?

var txt = new TextField();
with(txt){
    type = TextFieldType.INPUT;
    border = true;
    textColor = 0xffffff;
    multiline = true;
    x = 20;
    y = 20;
    width = 270;
    height = 40;
}
addChild(txt);
txt.name = 'test';

class classTest {
    public function classTest{
        trace(this.getChildByName开发者_如何学编程('test'));
    }
}
var cls = new classTest();


Not exactly sure what your doing, but the simplest way would be to parse a reference to root or the DisplayObject object containing your Textfield object, and then access the TextField object via that reference within your ClassTest class.

var txt:TextField = new TextField();

with(txt)
{
     type = TextFieldType.INPUT;     
     border = true;
     textColor = 0xffffff;
     multiline = true;     
     x = 20;     
     y = 20;     
     width = 270;     
     height = 40; 
} 

addChild(txt); 
txt.name = 'test';  

class ClassTest 
{
     public function ClassTest(p_target:DisplayObjectContainer)
     {         
          trace(p_target.getChildByName("test"));  

     } // end function

}// end function

var classText:ClassText = new ClassTest(this); // parse a reference to root


(in a new class file)

import flash.display.Sprite;
import flash.events.Event;

public class classTest extends Sprite
{
    public function classTest
    {
        addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
    }

    private function added(evt:Event):void
    {
        trace(stage.getChildByName("test"));
    }
}

Event.ADDED_TO_STAGE will enable you to reference the stage when it fires permitting you've instantiated the class and added it to the stage (which is why the event should fire in the first place).


not sure if i understand you correct. But, if you add a name to your display object you could try next code

txt.name = "txt_1";
this.getChildByName("txt_1");

UPDATE

better not to do like this. it's better to use events to communicate between classes, but anyway:

class classTest {
    private var _r : MovieClip; //not sure about the type
    public function classTest(r:MovieClip){
        _r = r;
        trace(_r.getChildByName('test'));
    }
}
var cls = new classTest(this);
0

精彩评论

暂无评论...
验证码 换一张
取 消