0

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?)

Colin
  • 5
  • 1
  • Do you mean `shapes[i]`? – Neal Davis Oct 05 '16 at 08:13
  • You simply want the string "breakpipe"? Tatactics method looks promising if that's the case. I'll post an idea I have as well – Neal Davis Oct 05 '16 at 08:19
  • Why does the name even matter? As l long as you create another instance with the same properties (like position, for example) when you read the XML, you do not have to care about the name (unless of course that's another property you want to restore, but from your code, that doesn't seem be the case) – null Oct 05 '16 at 09:52
  • To make this more explicit: the Animate program that generates the XML shares the same library as the program that reads it. The XML provides pre-calculated data points that determine how to progressively fill the shapes. Consequently the name in XML must match exactly the instantiated name or class name. Yes I know I can use string manipulation to extract the class name, but I wondered if there's a more elegant way to do it. – Colin Oct 05 '16 at 22:47
  • 1
    @Colin "*must match exactly the instantiated name or class name*" that's apples and oranges. It looks like you are confusing names of concrete instances of a class and the name of the class. If you have a proper serialisation pipeline setup, you simply convert an object to XML and back. – null Oct 06 '16 at 00:08

2 Answers2

1

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";
Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • another option is using an `Object` instead of `Array` : `shapes['brakepipe'] = _brakepipe` – www0z0k Oct 05 '16 at 09:45
  • I agree with @Neal Davis but I avoid to use the Object Class as www0z0k suggested you to store your datas in an Object. This may contains everything you wanna store and is less efficient than the Vector class. So I upvote the answer of Neal Davis! But why not? Object are easy to handle, and you can store whatever you want in it, because Objects are dynamics. If you want only store Strings I suggest you to use something like : var instanceStore:Vector. = new Vector.(); instead. – tatactic Oct 05 '16 at 12:19
  • @Neal Davis, I'm planning to use a variation of that now. I still only need one element per array slot, but it won't be the instance name. It will be another attribute that I create on the MC. – Colin Oct 07 '16 at 02:54
0

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?

tatactic
  • 1,379
  • 1
  • 9
  • 18