Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: AS 3.0 - Removing a child

(149 views • 11 replies)

This topic is 1 page long.

<< < > >>
None

RageVI

Reply To Post Reply & Quote

Posted at: 4/25/08 05:49 PM

RageVI NEUTRAL LEVEL 44

Sign-Up: 10/02/01

Posts: 4,895

Hey all...

I'm trying to script a really simple celluar automation program like this. I am not new to programming at all, but this is my first experience with Actionscript.

Background:

The stage is 400x400 pixels. I have a 20x20 2D array (Named "grid") that stores 0s and 1s. The 2D array is traversed with two nested for loops. A black square (A movie clip with the class name "Block") will be created and placed depending on its indexes in the array (Offset by 20 pixels for each index value). Each child is given the name "tile"+i+"x"+j. For example, if a block is being displayed in positon (5,3) on the grid, the child's name would be "tile5x3"

The problem:

I can create the blocks no problem (Thanks for the help jmtb02), but I've no idea how to remove them. Since these blocks will be coming and going in this program, that's a big problem.

The code snippet:

I believe this has everything you need to see. Let me know if you need to see more. Keep in mind that this code works just fine. I just don't have a clue on how to take this next step. I see functions floating around like "getChildByName" but I'm not sure how to use it properly.

function createBlocks():void {
	var currBlock:MovieClip;
	for (var i:int=0; i<20; i++) {
		for (var j:int = 0; j<20; j++) {
			if (grid[j][i]==1) { // If there is a 1 in the array right here, place the object
				currBlock = new Block();
				currBlock.x = 20*i;
				currBlock.y = 20*j;
				currBlock.name = "tile"+i+"x"+j;
				//trace(currBlock.name);
				addChild(currBlock);
			}
			else{// If there's a 0 here, remove any object that would be here.
				//How do I remove it?
			}
		}
	}
}

Here is a screenshot of the program working so far.

AS 3.0 - Removing a child

~ A Newgrounds Moderator ~ PM-eMail-AIM

BBS Signature

None

RageVI

Reply To Post Reply & Quote

Posted at: 4/25/08 05:57 PM

RageVI NEUTRAL LEVEL 44

Sign-Up: 10/02/01

Posts: 4,895

Whoops ... Messed up my first link

And to clarify, if I had a child named "tile5x3" created in the context I showed above, isn't there a way to remove it by its name? I'm pretty sure there's a really easy solution to this.

~ A Newgrounds Moderator ~ PM-eMail-AIM

BBS Signature

None

souled

Reply To Post Reply & Quote

Posted at: 4/25/08 06:00 PM

souled NEUTRAL LEVEL 14

Sign-Up: 09/18/06

Posts: 1,527

At 4/25/08 05:57 PM, RageVI wrote: Whoops ... Messed up my first link

It's the same...

And sorry I would help but I only know AS2.


None

RageVI

Reply To Post Reply & Quote

Posted at: 4/25/08 06:03 PM

RageVI NEUTRAL LEVEL 44

Sign-Up: 10/02/01

Posts: 4,895

At 4/25/08 06:00 PM, souled wrote:
At 4/25/08 05:57 PM, RageVI wrote: Whoops ... Messed up my first link
It's the same...

And sorry I would help but I only know AS2.

Yeah, I just noticed. The BBS must be borking up the link somehow. Just Google "Conway's game of life" and it'll be the first result.

~ A Newgrounds Moderator ~ PM-eMail-AIM

BBS Signature

None

Woadraiders

Reply To Post Reply & Quote

Posted at: 4/25/08 06:05 PM

Woadraiders DARK LEVEL 09

Sign-Up: 11/11/07

Posts: 359

removeChild(childReferenceName);

My blog-ish thing.
Workin' on games. For Whom the Bell Tolls.. It Tolls for Thee

BBS Signature

None

Woadraiders

Reply To Post Reply & Quote

Posted at: 4/25/08 06:07 PM

Woadraiders DARK LEVEL 09

Sign-Up: 11/11/07

Posts: 359

You should look at the help docs before asking for help, you can find any function you want from there, and its very organized, type in addChild, click the word and press f1. It directs you to information about the stage class and there's a list of all the methods that the class carries, it's very useful.

My blog-ish thing.
Workin' on games. For Whom the Bell Tolls.. It Tolls for Thee

BBS Signature

None

RageVI

Reply To Post Reply & Quote

Posted at: 4/25/08 06:12 PM

RageVI NEUTRAL LEVEL 44

Sign-Up: 10/02/01

Posts: 4,895

At 4/25/08 06:07 PM, Woadraiders wrote: You should look at the help docs before asking for help

Trust me when I say I know a lot better than to post before looking for the solution myself. By "child reference name", do you mean the .name property? Trying this:

removeChild("tile"+i+"x"+j);

Returns this compilation error:

1067: Implicit coercion of a value of type String to an unrelated type flash.display:DisplayObject

I also tried numerous different variants of that. Evidently removeChild doesn't refer to an actual object or something.

~ A Newgrounds Moderator ~ PM-eMail-AIM

BBS Signature

Elated

LCurtis

Reply To Post Reply & Quote

Posted at: 4/25/08 06:21 PM

LCurtis NEUTRAL LEVEL 17

Sign-Up: 05/23/07

Posts: 1,075

AS3 Lang. Ref. Stage.removeChild(child:DisplayObject)

You should be passing the variable that references the child that you want to remove.

Like:

var myMC:MovieClip = new MovieClip();
stage.addChild(myMC);
stage.removeChild(myMC);

If you want to remove based on its "name" property you need to use getChildByName to get a reference to the child. Then pass that reference to removeChild(). You can find info on getChildByName through the link I posted.

support the dream ...

BBS Signature

None

Cojones893

Reply To Post Reply & Quote

Posted at: 4/25/08 06:21 PM

Cojones893 EVIL LEVEL 21

Sign-Up: 03/09/03

Posts: 2,246

Normally I'd try my code before posting but I'm leaving work right now.

try

removeChild(MovieClip("tile"+i+"x"+j));

If that doesn't work let me know and I'll try and fix it when I get home.


None

RageVI

Reply To Post Reply & Quote

Posted at: 4/25/08 06:30 PM

RageVI NEUTRAL LEVEL 44

Sign-Up: 10/02/01

Posts: 4,895

At 4/25/08 06:21 PM, LCurtis wrote: AS3 Lang. Ref. Stage.removeChild(child:DisplayObject)

You should be passing the variable that references the child that you want to remove.

Like:

var myMC:MovieClip = new MovieClip();
stage.addChild(myMC);
stage.removeChild(myMC);

If you want to remove based on its "name" property you need to use getChildByName to get a reference to the child. Then pass that reference to removeChild(). You can find info on getChildByName through the link I posted.

Awesome. I somehow passed that up, but that's exactly what I needed to know.

So I have this:

var target:DisplayObject = getChildByName("tile"+i+"x"+j);
if(target){// test for null
	removeChild(target);
}

It seems to work just fine, but I somehow end up with some Blocks in places where they shouldn't be (in an index 'slot' that's currently 0 in the array). That might be something else though. I'll see.

Thanks a bunch.

~ A Newgrounds Moderator ~ PM-eMail-AIM

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 4/26/08 02:52 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 9,569

Don't look the children up by name. Store them in an array.

haXe AliceML Box2dLite Learn AS
#ngprogramming at irc.freenode.net
OVER NINE THOUSAAAAND!!!


None

fourpixels

Reply To Post Reply & Quote

Posted at: 4/26/08 10:46 AM

fourpixels NEUTRAL LEVEL 06

Sign-Up: 04/26/08

Posts: 36

removeChild(getChildByName( name here as string ["tile"+i+"x"+j], or whatever));


All times are Eastern Daylight Time (GMT -4) | Current Time: 08:34 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!