开发者问题收集

如何使用字符串访问影片剪辑?

2015-12-17
215

我尝试使用其名称访问影片剪辑,但失败了。

TypeError: Error #1009: Cannot access a property or method of a null object reference.

var i = 5;
var me:Array = new Array("num1-xd", "num2-xd2","num3-xd3");
var cool:Array;
var sq:Square;
for each(var wew:Object in me)
{
    //trace(me);
    cool = wew.split("-");
    sq = new Square();
    sq.x = 3;
    sq.y = sq.height + i;
    i += sq.height + 4;
    sq.name = String(cool[1]);
    sq.instanceName = String(cool[1]);
    addChild(sq);
    sq.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
    {
        trace(e.target.name);
    });
}

stage.addEventListener(MouseEvent.CLICK, KeyisDown);
function KeyisDown(e:MouseEvent)
{
    var me2:String = "xd";
    var me3:DisplayObject = stage.getChildByName(me2);
    me3.x += 5;
}

我无法访问舞台上的 xd 影片剪辑并使其移动。

2个回答

假设主时间线上只有影片剪辑,您可以尝试 this["xd"].x += 5; 或 this[me2].x += 5;

Andrew Sellenrick
2015-12-17

如果您在舞台上声明了变量

使用:

this.xd

this.xd.x += 5;

John Pangilinan
2015-12-17