通过静态文本引用更改文本值?

时间:2012-07-24 17:23:50

标签: actionscript-3 flash

我在DisplayContainer中搜索文本字段以更改它们,但我在更改引用时遇到问题:

    for (var i:uint = 0, n:uint = rootContainer.numChildren; i < n; ++i) {
        var item:DisplayObject = rootContainer.getChildAt(i);
        if (item is DisplayObjectContainer) {
            updateFields(DisplayObjectContainer(item));
        } else if (item is TextField) {
            item.text  = "hej";
        }
    }

item.text抛出:

1119: Access of possibly undefined property text through a reference with static type flash.display:DisplayObject.

我如何转换它以便我可以使用它?

1 个答案:

答案 0 :(得分:1)

您需要先将其键入TextField,然后才能调用TextField方法:

for (var i:uint = 0, n:uint = rootContainer.numChildren; i < n; ++i) {
    var item:DisplayObject = rootContainer.getChildAt(i);
    if (item is DisplayObjectContainer) {
        updateFields(DisplayObjectContainer(item));
    } else if (item is TextField) {
        var textField:TextField = item as TextField;
        textField.text  = "hej";
    }
}