00:00
00:00
Newgrounds Background Image Theme

Proximothegr81 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Add back elements to containers?

562 Views | 19 Replies
New Topic Respond to this Topic

Add back elements to containers? 2014-09-01 23:03:23


Say I have a container (mc) that I add to my game that contains a certain type of collectable (lets just say coins). I add that to my level container. I create an array for the coin container. Every time the player touches a coin I removeChild the coin from the container and remove it from the array as well.

All of this works fine. But say the player beats the game and they click replay. All of the coins are no longer in the coin container because they were removed from previous game session (through removeChild).

Is there a way around this without having to add all the coins back into the container manually? Seems a bit extreme to do it this way if say there were 100 coins in the container.

Would repeating the code , for example, coinContainer = new CoinContainer when the replay button is clicked fix this?

Any feedback would be greatly appreciated. I'm still relatively new to using arrays and loops.

Response to Add back elements to containers? 2014-09-01 23:51:53


I'm guessing that you're not addChild-ing the coins to start with. You're probably just putting them on the stage and collecting then in an array.

The solution is to addChild them to the screen. I guess you could check if they've been removed and only readd them then.

But this is a #FlashIDE problem. Most programmers avoid using the Flash IDE because things beside your code are involved. If you were using pure AS3 we could look at your code an point out an issue. But to fix this we'd have to understand how exactly your fla is laid out. And understand how Flash likes to handle code on frames.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Add back elements to containers? 2014-09-01 23:54:38


At 9/1/14 11:51 PM, MintPaw wrote:
The solution is to addChild them to the screen. I guess you could check if they've been removed and only readd them then.

Is that what you guys do? addChild every single item to the level? Seems like a ton of code if say there were 100 items in the level.

Response to Add back elements to containers? 2014-09-02 00:09:17


At 9/1/14 11:54 PM, Hero101 wrote: Is that what you guys do? addChild every single item to the level? Seems like a ton of code if say there were 100 items in the level.
for (coin in coins) container.addChild(coin);
for (plat in platforms) container.addChild(plat);
for (item in items) container.addChild(item);

Could be a thousand objects, loops are your friends.

Response to Add back elements to containers? 2014-09-02 01:09:36


At 9/2/14 12:09 AM, MSGhero wrote:
At 9/1/14 11:54 PM, Hero101 wrote: Is that what you guys do? addChild every single item to the level? Seems like a ton of code if say there were 100 items in the level.
for (coin in coins) container.addChild(coin);
for (plat in platforms) container.addChild(plat);
for (item in items) container.addChild(item);

Could be a thousand objects, loops are your friends.

I haven't used a for...in loop before so I will have to practice around with them. I'm assuming the "coins" in your code above is an array that the coins are stored in. But using your code above how would I position each coin after addChild-ing them? Looking at your example it would just addChild them all to the same location in the container...

Response to Add back elements to containers? 2014-09-02 01:25:29


When you removeChild something it doesn't magically lose it's properties.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Add back elements to containers? 2014-09-02 01:37:59


At 9/2/14 01:25 AM, MintPaw wrote: When you removeChild something it doesn't magically lose it's properties.

I understand the issue with not hardcoding everything. If I have a mc container that has all of the coins placed around in it - it doesn't have their locations saved anywhere in code... thus it doesn't have it's location properties since I didn't hardcode each and every one of them in a class...

Unless I'm missing something...

Response to Add back elements to containers? 2014-09-02 08:53:24


At 9/2/14 01:37 AM, Hero101 wrote: Unless I'm missing something...

You hard-coded their positions by placing them in flash pro. When you add a symbol to a thing, you're addChilding it and setting its position. Don't worry about for in loops, that was pseudocode.

Response to Add back elements to containers? 2014-09-02 17:12:31


Properties are properties, if myClip.x is crated because you placed the clip in the FlashIDE, then you removeChild() it, it will still retain the properties that it had even though it was set by the IDE. I know it's confusing, these are some weird meta-programming things you should really be having to think about, I hope soon you switch out of the IDE, it's important in learning to program.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Add back elements to containers? 2014-09-02 20:57:00


At 9/2/14 05:12 PM, MintPaw wrote: Properties are properties, if myClip.x is crated because you placed the clip in the FlashIDE, then you removeChild() it, it will still retain the properties that it had even though it was set by the IDE. I know it's confusing, these are some weird meta-programming things you should really be having to think about, I hope soon you switch out of the IDE, it's important in learning to program.

Ok thank you guys for all of your input. I do plan on making the switch out of the IDE very soon. I was planning to go to Flash Develop first, but recently my PC went down. Until I can get it repaired I have been working on my Mac. So I will have to try out Flash Builder. Hopefully that is just as good as Flash Developer.

Response to Add back elements to containers? 2014-09-02 21:09:09


At 9/2/14 08:53 AM, MSGhero wrote:
At 9/2/14 01:37 AM, Hero101 wrote: Unless I'm missing something...
You hard-coded their positions by placing them in flash pro. When you add a symbol to a thing, you're addChilding it and setting its position. Don't worry about for in loops, that was pseudocode.

Gotcha. So a regular for loop would work just fine then instead of a for...in loop? I've read the AS3 API documentation on all the types of loops and while I understand when using a for, while, do loop would be necessary, I don't quite understand when I would need to use a for...in loop (same goes the for...each loop).

Response to Add back elements to containers? 2014-09-02 21:42:57


At 9/2/14 09:09 PM, Hero101 wrote: Gotcha. So a regular for loop would work just fine then instead of a for...in loop? I've read the AS3 API documentation on all the types of loops and while I understand when using a for, while, do loop would be necessary, I don't quite understand when I would need to use a for...in loop (same goes the for...each loop).

I have no idea if for in loops work on arrays. I was writing haxe because the concepts matter more than the exact code I type out.

Response to Add back elements to containers? 2014-09-02 23:34:00


Also realized that in the block of code that fills the array with the items I could just re-add the new instance of the container containing the items before refilling the array like so:

function fillBricks():void //Fill array with bricks that are in brick container (brickCon)
		{
			var a:Number = 0;
			for (a = 0; a < brickCon.numChildren; a++)
			{
				bricks.push(brickCon.getChildAt(a)); //Add bricks to an array
			}
			if (bricks.length < brickConAmount) //If brick array is less than starting amount of bricks in container
				{
					removeChild(brickCon);  //THESE NEXT 3 LINES IS WHAT I WAS TALKING ABOUT
					brickCon = new BrickCon();
					addChild(brickCon);
					for (a = 0; a < brickConAmount; a++)
					{
						bricks.push(brickCon.getChildAt(a));
					}
				}
		}

Not sure if removing item container, declaring a new one, and adding it back is the proper way to go about what I'm trying to do (get items back on stage in case level is replayed again) but it works...

Response to Add back elements to containers? 2014-09-03 01:39:29


At 9/2/14 11:34 PM, Hero101 wrote: Not sure if removing item container, declaring a new one, and adding it back is the proper way to go about what I'm trying to do (get items back on stage in case level is replayed again) but it works...

Just add back the old ones...you're destroying a brick then putting an identical one in its place.
Save all bricks.
Duplicate that array if you want to splice or mess with its length at all, or don't change the length but make them visible=false.
addChild everything or visible=true from the full array when you restart the level.

Response to Add back elements to containers? 2014-09-03 01:51:34


At 9/3/14 01:39 AM, MSGhero wrote:
At 9/2/14 11:34 PM, Hero101 wrote:
Just add back the old ones...you're destroying a brick then putting an identical one in its place.

I'm trying to figure out how to do that but have been unsuccessful so far.... I don't know how far off the mark I am with the code I posted above (minus what I did with removing and then re-adding a new instance of the item container).

Save all bricks.

What exactly do you mean by saving them? Like store each item in the container in a variable (if so, how?)?

Response to Add back elements to containers? 2014-09-03 11:13:26


At 9/3/14 01:51 AM, Hero101 wrote:
At 9/3/14 01:39 AM, MSGhero wrote:
At 9/2/14 11:34 PM, Hero101 wrote:
Just add back the old ones...you're destroying a brick then putting an identical one in its place.
I'm trying to figure out how to do that but have been unsuccessful so far.... I don't know how far off the mark I am with the code I posted above (minus what I did with removing and then re-adding a new instance of the item container).

Save all bricks.
What exactly do you mean by saving them? Like store each item in the container in a variable (if so, how?)?

Loop through your bricks.
Push them to one array.
Push them to another array.
Manipulate the first array however you want.
Use the second array to refresh the first one and refresh the container.

You have an array, you don't need to save bricks in a variable. You save them in the array by pushing them and not removing them.

Response to Add back elements to containers? 2014-09-03 16:02:07


At 9/3/14 11:13 AM, MSGhero wrote:
At 9/3/14 01:51 AM, Hero101 wrote:
At 9/3/14 01:39 AM, MSGhero wrote:
At 9/2/14 11:34 PM, Hero101 wrote:
Loop through your bricks.
Push them to one array.
Push them to another array.
Manipulate the first array however you want.
Use the second array to refresh the first one and refresh the container.

You have an array, you don't need to save bricks in a variable. You save them in the array by pushing them and not removing them.

Oh how interesting. Never thought of trying that. Just to be clear when you say push them into another array - you are just talking about the bricks, right?

Or do you mean pushing the array containing the bricks into another array? Although I don't know the purpose of storing an array in another right just yet...

Response to Add back elements to containers? 2014-09-03 16:13:51


At 9/3/14 04:02 PM, Hero101 wrote: Oh how interesting. Never thought of trying that. Just to be clear when you say push them into another array - you are just talking about the bricks, right?

Yes, storing arrays in other arrays doesn't make sense here.

Response to Add back elements to containers? 2014-09-03 16:15:13


At 9/3/14 04:13 PM, MSGhero wrote:
At 9/3/14 04:02 PM, Hero101 wrote: Oh how interesting. Never thought of trying that. Just to be clear when you say push them into another array - you are just talking about the bricks, right?
Yes, storing arrays in other arrays doesn't make sense here.

Does it every make sense to store an array inside of another array? If so, could you give me a scenario where one would consider doing that?

Response to Add back elements to containers? 2014-09-03 16:19:08


At 9/3/14 04:15 PM, Hero101 wrote: Does it every make sense to store an array inside of another array? If so, could you give me a scenario where one would consider doing that?

If you have a grid, you store a row (array) inside each node of a column (array). aka 2D arrays.