【问题标题】:addChild does not work inside functionaddChild 在函数内部不起作用
【发布时间】:2013-02-12 12:10:23
【问题描述】:

我在程序中使用 addChild 时遇到问题,文档类如下:

public function gridtest()
    {
        turn++;
        var plusX = 1;
        var plusY = 0;

        trace ("game started");

        var i;
        var j;

        for (i = 0; i < 5; i++)
        {
            trace ("first for loop");

            for (j = 0; j < 8; j++)
            {
                gridbutton = new GridButton();

                gridbutton.x += plusX;
                gridbutton.y += plusY;
                gridbutton.GBID = (i*10)+ j;

                trace ("created button at " + plusX + "," + plusY);

                addChild(gridbutton);
                GBList.push(gridbutton);
                trace("grid button number " + i + j + " added");
                plusX += 92.5;
            }

            plusY += 61.5;
            plusX = 1;
        }
    }

我在主函数gridtest()中有一个addChild,效果很好,但是这里的第二个函数中有另一个(还是同一个文档):

            static function NewUnit(locX, locY)
    {
    unit = new Unit(locX, locY);
    unit.Set_ID(UnitList.length);
    addChild(unit);
    UnitList.push(Unit);
    }

给我错误 1180:调用可能未定义的方法 addChild,它与函数是静态的有关吗?还是完全不同。

【问题讨论】:

    标签: actionscript-3 flash-cs5


    【解决方案1】:

    addChild 是一个非静态函数。静态和非静态函数之间的交互不可用。您应该首先引用 Object 的实例并从该实例调用函数。请在静态函数中查看trace(this);

    如果你想要对象 addChild。应该为要添加到的父级添加一个附加参数。

    static function NewUnit(locX:Number, locY:Number, parent:DisplayObjectContainer)
    {
        unit = new Unit(locX, locY);
        unit.Set_ID(UnitList.length);
        parent.addChild(unit);
        UnitList.push(Unit);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      相关资源
      最近更新 更多