user1203605
user1203605

Reputation: 83

AS3 Can't access MC on Stage

I have 54 MC's on stage, generated dynamically. Now I want to get their x and y positions when rolling over but I am having problems getting the path correct e.g.

function copyFlightCellData():void {

var r; var s; 
var cellData:Array = new Array ();
for (r = 0; r < 54; r++){
    //var copyCellData = new MovieClip();
    cellData[r] = Object(root).mc85.name; //["mc"+r+r];
    trace("$$$$$$$$$$$$$$$$$$$$$" + cellData[r]);
}

}

I used the list objects in debug and they are listed in _level0 e.g.

Movie Clip: Frame=1 Target="_level0.mc85"

Not sure why I can't access their properties.

This is the code that created the MC's

        // Create copies of flightCell for board grid
        var my_mc = new flightCell();
        my_mc.name = "mc" + i + j;
        trace("^^^^^^^^^^^^^^****************" + my_mc.name);
        addChild(my_mc);

Upvotes: 0

Views: 337

Answers (2)

Taurayi
Taurayi

Reputation: 3207

Answer is pretty simple, use the DisplayObjectContainer object's, in this case root, getChildByName() method, for example:

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
addChild(sprite1);
trace((root as DisplayObjectContainer).getChildByName("sprite1").name); // output : sprite1

It's probably a better idea to store the movieclips you have on your stage in an array to begin with.

Upvotes: 1

Ricardo Souza
Ricardo Souza

Reputation: 16446

To access it by name you have to assign a name to them when you create them.

mc85.name = "mc85";

As an alternative that I recommend, you can use getChildAt(index) : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#getChildAt()

Also, I highly recomend you to create an empty movieclip or sprite and add all of this mcs to them instead of the root.

Upvotes: 0

Related Questions