如何从父母的孩子那里分配父母的变量?

时间:2011-11-07 11:23:54

标签: actionscript-3 variables

我想将变量从父传递给子。如何传递变量。我可以粘贴我的代码.AnyBody请指导我。提前谢谢!

我的家长班

public class PlayerStrobe_domaincheck extends Sprite
{
    private var myService:NetConnection = new NetConnection();
    private var _loader:Loader;

    public function PlayerStrobe_domaincheck()
    {
        _loader = new Loader();
        var sourceId:String = loaderInfo.parameters.src;// I want to pass This Variable
        var some:StrobeMediaPlayback = new StrobeMediaPlayback(parentObject);// Access of undefined property parentObject(Error Message).
        some = new StrobeMediaPlayback(this);

}     }

我的孩子班

public class StrobeMediaPlayback extends Sprite
{
    private var _loader:Loader;
    private var sourceId:String; //i want to access the the parent  variable(sourceId) here
    parentClass = parent;
    function Test():void
        {
                mediaContainer.addMediaElement(alert);
                            alert.alert("Error",parentClass.sourceId);
                 }

3 个答案:

答案 0 :(得分:0)

除非父级中的变量为public

,否则无法直接执行此操作

然后,这样做并不是一个好的代码,因为那时孩子被限制只被添加到你的父类而不是其他任何东西。

最好的方法是派遣一个事件,并让父母听取该事件。父级中的事件处理程序应更改父级中的变量。

答案 1 :(得分:0)

Yoc可以将父级传递给子级

public class StrobeMediaPlayback extends Sprite
{
    private var _loader:Loader;
    private var sourceId:String; //i want to access the the parent  variable(sourceId) here
    private var parentClass:PlayerStrobe_domaincheck;

    public function StrobeMediaPlayback(parent:PlayerStrobe_domaincheck)
    {
        parentClass = parent;
        trace("welcome");
    }   
    public function Test():void
    {
        trace(parentClass.sourceId);
    }
}

当你从父母(例如)构造你的孩子时,将父对象作为参数传递

var some:StrobeMediaPlayback = new StrobeMediaPlayback(parentObject)

你和父的定义应该是这样的

public class PlayerStrobe_domaincheck extends Sprite
{
    private var myService:NetConnection = new NetConnection();
    private var _loader:Loader;
    private var some:StrobeMediaPlayback; //update
    public var sourceId:String;

    public function PlayerStrobe_domaincheck()
    {
        _loader = new Loader();
        sourceId = loaderInfo.parameters.src;// I want to pass This Variable
        some = new StrobeMediaPlayback(this) //update
    }
}

你可以在父母构造函数中玷污你的子对象

答案 2 :(得分:0)

public class PlayerStrobe_domaincheck extends Sprite
{
    private var myService:NetConnection = new NetConnection();
    private var _loader:Loader;


    public function PlayerStrobe_domaincheck()
    {
        _loader = new Loader();
        var sourceId:String = loaderInfo.parameters.src;

        var some:StrobeMediaPlayback = new StrobeMediaPlayback(sourceId);

        new StrobeMediaPlayback(this);
    }
}



public class StrobeMediaPlayback extends Sprite
{
    private var _loader:Loader;
    private var sourceId:String;
    parentClass = parent;

   public function StrobeMediaPlayback(sourceIdVariablePassed:String):void
    {
                mediaContainer.addMediaElement(alert);
                trace("Source ID "+sourceIdVariablePassed);
    }
}