PDA

View Full Version : Creating a list of objects on stage



costa
May 21st, 2007, 02:35 AM
Dear all,

I am still confused with all this new methods to handle objects in AS3. I was able to create a container for my movie clips and traverse it using “for in”. Now I want to create a list of all objects on stage which I put manually (no addChild) and find the name of the movieClip on the library, I mean the main object of those mc on stage. I tried using “for in” with stage object but it did not work.

Could you give a clue what I have to do, please?

Thanks in advance

costa
May 21st, 2007, 02:49 AM
Ok, I am getting something with this code:


for (var i:Number =0; i<this.numChildren; i++) {
trace(this.getChildAt(i).name;
}

Result
instance1
instance3
instance5
instance7
instance9
instance11
instance13

This is ok because I have 7 objects on stage. Now i need to know the name of their main object. I will keep trying

senocular
May 21st, 2007, 10:52 AM
main object?

annettegeek
May 21st, 2007, 10:53 AM
I have something similar. but be sure to put the ) after .name

It looks to me like your objects are not named. instancex is what I get when I've forgotten to give my objects a name.

costa
May 21st, 2007, 02:56 PM
main object?

Sorry senocular, I still do not know how to name things in Flash (I am not a programmer) What I try to say is the name of the movie clips in the library which correspond to the instances on stage. I hope I explained myself well

Thanks

Aquilonian
May 21st, 2007, 03:43 PM
Now I want to create a list of all objects on stage which I put manually (no addChild)

You mean, you've draged those from lybrary, isnt it?


and find the name of the movieClip on the library,

There is no way to find the name of things in lybrary using code, that i know.You have to know what you put on it


I mean the main object of those mc on stage.

you mean...you draged all those to inside another?


I tried using “for in” with stage object but it did not work.

Could you give a clue what I have to do, please?

Thanks in advance

costa
May 21st, 2007, 04:02 PM
You mean, you've draged those from lybrary, isnt it?

There is no way to find the name of things in lybrary using code, that i know.You have to know what you put on it

you mean...you draged all those to inside another?

Yes I dragged those objects on stage. According to you there is no way to know the name of things in the library. That is bad news for me. Because I have many objects I put on stage (more than 1000) and I do not want to name each instance. I will keep trying. Thanks

annettegeek
May 21st, 2007, 05:06 PM
Yes I dragged those objects on stage. According to you there is no way to know the name of things in the library. That is bad news for me. Because I have many objects I put on stage (more than 1000) and I do not want to name each instance. I will keep trying. Thanks

Ouch! Manually dragging 1000 objects on stage???
I suppose hindsight is 20-20, but for future reference you can do this dynamically.
For your movie clips in the library, right click and choose properties, export to ActionScript. this will create a class for the object, which will allow you to call them up in a loop:

//in this example, a movie clip in the library is exported to a class named square
for (var m:uint=1; m<4; m++) {
var thisMC:MovieClip = new square();
thisMC.name="square"+m;
thisMC.x=300;
thisMC.y=50*m;
addChild(thisMC);
}

This will create three instances: "square1", "square2", "square3" and you can trace them with your earlier loop.

costa
May 21st, 2007, 05:28 PM
Sorry for not giving you more information. What I am doing is like tile editor in Flash. I have a swf 1920x1440 with a grid of 32x32 (60x45 items). I am populating this document with all tiles I will use in a game with the intention to create a list of items to be loaded dynamically, as you recommend, in the final game. That is why I looking for a way to get the name of the movieclips in the library whose instances are on stage.

Btw I got 20-20

Thanks for taking time to answer my posts

lowdown
May 22nd, 2007, 01:29 AM
Sorry for not giving you more information. What I am doing is like tile editor in Flash. I have a swf 1920x1440 with a grid of 32x32 (60x45 items). I am populating this document with all tiles I will use in a game with the intention to create a list of items to be loaded dynamically, as you recommend, in the final game. That is why I looking for a way to get the name of the movieclips in the library whose instances are on stage.

Btw I got 20-20

Thanks for taking time to answer my posts

You have to set the linkage options and 'export for actionscript' to give each of the library items a class name.

What advantage does dragging all the items onto the stage bring? Functionally (as a tile editor I mean) it doesn't seem very useful.

http://hiddenresource.corewatch.net/archives/78

Something like this is pretty sweet, and useful.

costa
May 22nd, 2007, 01:55 AM
In fact, it is pretty handy "lowdown" from an artist perspective. I already have my map (1990x1440), I can see it how it will look and now I am making a XML file with the information of tiles (transformation matrix). In the game I will load this file to show the tiles.

Thanks for the link, that’s light-years from what I am doing. I am just starting with flash.

lowdown
May 22nd, 2007, 02:22 AM
If you just want to visually see what they will look like, then loading them programatically will solve both of your problems, as suggested above. You'd even be able to create your XML file in the process.

http://www.jessewarden.com/archives/2006/01/diesel_battlefi.html

If you are like me and just starting out with as3 programming, I find that I learn best by utilizing existing code as a learning tool. Perhaps the above project might be of use.

costa
May 22nd, 2007, 02:36 AM
lowdown, you got very nice links. Thanks for sharing!!! AS3 can be scary. Now I am practicing with this tile game in order to learn. So far I understand XML and loading things from library to stage. I am still confused about the display object and all related. But I think the best way to learn is getting hands dirty and with the support of this beautiful forum

Thanks again

costa
May 22nd, 2007, 04:57 PM
Ok. This is what I did to create a XML file with a list of the objects on stage. In my case, those objects were put manually (drag and drop). I think the most important here is how easy is to handle XML with AS3. This is the simple code:


var newMCXML:XML = new XML("<MC></MC>");
for (var i:int =0; i<this.numChildren; i++) {
var nameMC:String=this.getChildAt(i).name;
var newnode:XML = new XML();
newnode = <movie id={i} name={nameMC}/>
newMCXML = newMCXML.appendChild(newnode);
}
trace(newMCXML);

Result

<MC>
<movie id="0" name="photo1"/>
<movie id="1" name="photo1"/>
<movie id="2" name="photo1"/>
<movie id="3" name="photo2"/>
.
.
.
<movie id="979" name="cartoon1"/>
<movie id="980" name="car3"/>
<movie id="981" name="car3"/>
<movie id="982" name="car1"/>
</tile>

And if you want some filters, that is easy


for each (var mc:XML in newMCXML.movie.(@name=="car3")) {
trace (mc.@id);
}

Result

.
.
980
981

As you can see, I am using "{}" to be able to pass a var to XML (that is nice)

Well… I hope someone can find this useful.

Have fun with AS3!!

lowdown
May 22nd, 2007, 11:17 PM
Ah cool. Glad you found the answer to suit your needs. Everything I know about actionscript has been through the eclipse IDE with Flex Builder plugged in. I need to spend some time in cs3 to understand the different methods.