开发者

AS3 - Can't access properties or methods of a MC child that has been added in script

开发者 https://www.devze.com 2022-12-28 14:41 出处:网络
I am still a bit of a beginner at AS3, so bear with me, please. I have created a loop to instantiate tiles on a board.In the following example, \"Gametiles\" is an arr开发者_开发技巧ay containing obj

I am still a bit of a beginner at AS3, so bear with me, please.

I have created a loop to instantiate tiles on a board. In the following example, "Gametiles" is an arr开发者_开发技巧ay containing objects of class "Tile" which is a class that extends MovieClip. "Game" is a MC that I added to the stage in the flash developing environment.

for(var i:uint=0;i < Gametiles.length;i++){
    var pulledTile = Gametiles[i];
    var tilename:String = "I_Tile_" + pulledTile.grid_y + "_" + pulledTile.grid_x;
    var createdTile = new InteractiveTile();
    pulledTile.addAnims(createdTile);
    Game.addChildAt(pulledTile, 0);
    Game.getChildAt(0).name = tilename;
}

The above code works - but with a tricky problem. If I did something like the following:

trace(Game.I_Tile_1_3.x);

I get "TypeError: Error #1010: A term is undefined and has no properties." However, I am able to access theses children in the following manner:

var testing = Game.getChildByName("I_Tile_1_3")
trace(testing.x);

This method is a bit cumbersome though. I really don't want to have to create a var and call getChildByName every time I want to interact with these properties or methods. How can I set up these children so that I can access them directly without the extra steps?


use the sqare brackets.

Trace(Game["I_tile_1_3"].x);
0

精彩评论

暂无评论...
验证码 换一张
取 消