Is their any way to use array access for movieclip children?

I am trying to access 5 children of a Movieclip “container” with name a0,a1,a2,a3, a4.

I tried to access them as container[“a”+i ] in a for loop with i ranging from 0 to 5. But get an error saying

Array access is not allowed on openfl.display.MovieClip

Is their any way to use array access in such cases ? Or any other way that can solve the purpose similarly ?

Maybe
var a = container.getChildByName(“a0”);
will work?
You can extend it into

(for i in 0...5){
  str = "a" + Std.string(i); //cast int to string and concatenate it with "a"
  var a = container.getChildByName(str);
}
1 Like

Yes, no doubt, that will work. Thanks. Forgot about that totally.

However, out of curiousity, what if the movieclip is not a child ? It’s only a public variable. Or say, it’s not any movieclip but a simple public variable. Is it possible to access it through array in that case, in OpenFl ?

PS: Anyway, I think I can test that… will update the answer here.

If You are thinking about access operator like in c ( char p ~= char q[ ]), then I think it is not possible (Maybe with cast to abstracts…? like https://haxe.org/manual/types-abstract-operator-overloading.html).
Generally in OpenFl Haxe You don’t have posibillity to operate on pointers, so there is no control over objects placement - what You will point to when You will check memory sizeof(void
) further. So even if this will work (it will return something, instead of crashing), it will be probably random data

You can try Reflect to access a variable dynamically:

var numChildren = Reflect.field (clip, "numChildren");
Reflect.setField (clip, "x", 100);

Yeah, Reflect is what you’re looking for. But since you’re dealing with DisplayObjects, you’ll want to use Reflect.getProperty() and Reflect.setProperty():

var numChildren = Reflect.getProperty (clip, "numChildren");
Reflect.setProperty (clip, "x", 100);

In @singmajesty’s example, Reflect.field() would have compiled because numChildren is declared “(get, null),” making it a physical field. However, since nothing ever assigns to it, it constantly stays at 0. I’m working on a pull request that will turn it into a non-physical field and save some memory, but that isn’t relevant to this thread, since either way the solution is to use getProperty() and setProperty().