self removeChild inside a container

16 posts

Flag Post

Hey I’m programing my first game on AS3 and I have a big problem :S

I have this code on the first frame

var numbers:MovieClip = new numbers_mc();
empty_numbers.addChild(numbers);
numbers.x=mouseX;
numbers.y=mouseY;

When I click the enemy I spawn an mc inside a container (empty_numbers)

And this is the code inside numbers_mc class

function myEnterFrame(event:Event) {
y-=3;
ctime++;
if (ctime>=maxtime) {
parent.removeChild(this);
}
}

With as2 I could delete the mc with this.removeMovieClip() but every combination I try here give me an error

TypeError: Error #1009: Cannot access a property or method of a null object reference.at numbers_mc/myEnterFrame()

Any help??

 
Flag Post

this.parent.removeChild(this); should do it.

 
Flag Post

I already test that and gives this error

Error #1009: Cannot access a property or method of a null object reference.

 
Flag Post

this.parent.removeChild(this) or even parent.removeChild(this) is correct and should work. Remove your enterframe event listener (and any other listeners you may have created for it) before you remove it. I’m just throwing out guesses though, as we’re not seeing the full set of code. Also, enclose your code in < pre> < /pre> tags to make it easier to look at.

 
Flag Post

hahaha i just fond that solution by myself dont know how xd

if (ctime>=maxtime) {
				removeEventListener(Event.ENTER_FRAME, myEnterFrame);
				parent.removeChild(this);
				delete this;
			}

thx for your help!

 
Flag Post

I still cant find why as3 is better than as2 >.< everything is harder and need more code :S

 
Flag Post
Originally posted by Arkshija:

I still cant find why as3 is better than as2 >.< everything is harder and need more code :S

Getting the error you got is one reason why it’s better.

Lines like these:

delete this;
//or
y-=3;

Don’t make too much sense.

 
Flag Post

y -= 3 makes sense (I’m guessing he wants his displayobject to move upward before vanishing) but yeah, delete this is a huge pile of wtf that isn’t necessary.

 
Flag Post

Things like this are why it is usually better to code in classes rather than on the timeline. Adding and removing display objects should just about always work as you intend unless you do something wrong.

 
Flag Post

the 3 looks like a magic number

 
Flag Post

yes… y-=3 its to make the numbers move upward, that delete was just added testing convinations while googling, as I said this is my first game in as3 im still confused with imports parents privates >.<

 
Flag Post
Originally posted by Arkshija:

im still confused with imports parents privates >.<

import, parent and private are not new elements of As3, they exist in As2 and have (basically speaking) the same functionality.
What do you have trouble with exactly?

 
Flag Post
Originally posted by Arkshija:

yes… y-=3 its to make the numbers move upward, that delete was just added testing convinations while googling, as I said this is my first game in as3 im still confused with imports parents privates >.<

NineFiveThree is right to be wary of that, because using “magic numbers” isn’t really good practice. Let’s say this class was a lot more complex, and somewhere along the line in your development, you decided it would be cool to change the number to alter your scroll speed, or even randomize it—you’d have to pick all of your code just to find that one magic number (3) before you could change it. If you put all your “magic numbers” in the same place, in constants, you can then tweak things to your heart’s content without having to hunt for them in your code at a later date.

 
Flag Post

Im just practicing with new syntax and so, im noob working with listeners and all this new stuff, but ye I usually write constants but this code is very short.

btw im programing with as3 for android at same time but when I publish the game with the option to install the game to a conected device the game is every time uninstalled and installed again so I lost all saved data, you know if this will be the same if I update the game after uploading to the market? because I dont want the players lose their saved data every time I update the game :S

 
Flag Post

Not sure if this is bitching about as3 or..
var numbers:MovieClip = new numbers_mc();
empty_numbers.addChild(numbers);
numbers.x=mouseX;
numbers.y=mouseY;

When I click the enemy I spawn an mc inside a container (empty_numbers)

And this is the code inside numbers_mc class

function myEnterFrame(event:Event) {
y-=3;//where is the instance name for this? you need more.

ctime++;// where is the variable for this?
//make sure u do a check to see if its on stage
if(stage.contains(numbers))
if (ctime>=maxtime) {
//if(stage.contains(numbers))//or here
parent.removeChild(this);
//or
empty_numbers.removeChild(this);
}
}

 
Flag Post
Originally posted by dodico:

function myEnterFrame(event:Event) {
y-=3;//where is the instance name for this? you need more.

}

No, because it is supposed to move itself.

Originally posted by dodico:
//make sure u do a check to see if its on stage
if(stage.contains(numbers))
if (ctime>=maxtime) {
//if(stage.contains(numbers))//or here
parent.removeChild(this);
//or
empty_numbers.removeChild(this);
}
}

contains() has to traverse the display list.
You could simply check if stage or parent are null.

The last approach is not recommended:
empty_numbers.removeChild(this);
because empty_numbers is unknown

Not sure what this has to do with “bitching about As3”.