AS3 MovieClip name ambiguity

79 views Asked by At

Summary: I create instances of various MovieClips via AS3, using MovieClip class objects that are defined in the library.

As each MC is instantiated, I push it into an array for later reference.

Finally I create an XML file that contains data related to each MC, including its name. This is the problematic part – the name has to be able to identify the respective MC when the XML is read back in. I don’t want “instance17” etc, which I assume will be meaningless in another session.

Background: I am not a career OO programmer and this is a temporary assignment, forming only a very small part of my long-term interests. It will probably be a couple of years before my next Flash project.

Create instance

Library object Type: MovieClip, linkage _brakepipe

Instantiation

var brakepipe: _brakepipe = new _brakepipe();
shapes.push(brakepipe);

Then later

var clip: MovieClip = shapes(i);
Trace (clip);

This yields

[object _breakpipe]

So it is giving me the class name, not the MC instance name. What property or method of MC would yield “breakpipe”? (Or even "_breakpipe" - without the "object" prefix?)

2

There are 2 answers

3
Neal Davis On BEST ANSWER

You can use an associative array. It could look like this:

var shapes:Array = new Array();

and then

shapes.push({item:_brakepipe,_name:"brakepipe"};

Essentially the curly brackets create an Object instance and the name before the colon (:) is the name you create that you want associated with the value after the colon.

so now you can do this in a loop

trace(shapes[i]._name+"\n"+shapes[i].item);
// output: 
// brakepipe
// [object _brakepipe]

The nice thing about this method is you can extend it for any number of properties you want to associate with your array element, like this:

shapes.push({item:_brakepipe,_name:"brakepipe",urlLink:"http://www.sierra.com",_status:"used",_flagged:"false"};

and now

shapes[i]._status

would return the string "used". And you could change that value at runtime to "new" by doing

shapes[i]._status = "new";
0
tatactic On

The Instantiation / Then later / This yields... Seems to be unclear for me, but you may try this and change the code...

Because I'm not sure not sure about the instance name you want to store...

In your loop you may do this if clip is a MovieClip! :

var clip: MovieClip = shapes(i);
clip.name = "breakpipe_" + i
trace (clip.name);

// will output : breakpipe_1 - > breakpipe_n...

You may deal with the clip.name later by removing the extra "_number" if you want.

If i == 13

var clip: MovieClip = new MovieClip();
clip.name = "breakpipe_" + 13
trace(clip.name);
// output breakpipe_13
var pattern:RegExp = /_\d*/g;
trace(clip.name.replace(pattern,""));
//output :
//breakpipe

So here, you may push your Array or Vector with the instance name. Am I wrong?