I'm making a game where objects are sorted, and after they are put into one of the bins, I would like to remove the object so that the program doesn't get bogged down by tons of items that are no longer in use. When I try to do this using the removeChild() method, I get "TypeError: Error #1009: Cannot access a property or method of a null object reference."
Is there a good way to remove the object and its event listeners so that I can put new objects into play without fear of the game slowing down, or should I just make the old objects invisible and ignore them? I've tried moving to a new frame using "nextFrame();" where all of my old objects are no longer there, but I get a similar error, and as the background and collectors remain the same between levels, it might be unnecessary to start over completely.
The 1009 error indicates that the objects you are trying to target do not exist in the eyes of the code. Since you say there are tons of items, are they created dynamically? If so, then you need to show how you create them, how you store references to them, and what code you use to remove them.
The first set of items start out already placed on the stage. Would it be best to add them at the start rather than having them already in their starting points on the stage?
If so, what would the code look like? I'm assuming it would use stage.addChild() but as the object wouldn't be on the stage, it wouldn't have an instance name, so what would I put in the addChild() instead? Also, how do I specify coordinates for where it would be added?
Thanks for the help!
Part of my code is the function update, that starts running when the "start" button is pressed. I have 6 objects that need to be sorted, and I have the following code to get rid of the old 6 items once they are properly sorted:
if (correct > 5){
description.alpha = 1;
description.text = "Success! Click anywhere to continue.";
addChild(nextButton);
nextButton.addEventListener(MouseEvent.CLICK, advance)
function advance(e:MouseEvent){
//get rid of old things that have been sorted
removeChild(waterbottle);
removeChild(plasticbottle);
//add new things to sort
addChild(bananapeel);
addChild(lightbulb);
}
}
I suppose after the items have been added and the next stage starts, I'd want to remove "nextButton" and its event listener.
All of my code and objects are in frame 1.
Earlier in the code I used removeChild to "hide" the nextButton.
The problem arises in an earlier part of the update function:
plasticbottle.update();
plasticbottle is in a particle-like class that falls according to gravity, and it seems that it doesn't want to be removed since the plasticbottle.update(); is still running.
The update function, which starts when the start button is pressed, works like this (pseudocode):
{
Update all the items(gravity, picking up, dropping, etc)
Count how many items have been sorted correctly
If all of the items have been correctly sorted, create a button to go to the next level
When the button is clicked, (this is where I'm having trouble) remove the old objects and add new ones)
}
The error is because once I remove the old objects, it still wants to update them, but they're no longer on the stage.
Hopefully this illustrates what I'd like to do.
That sounds like a good solution, and I'll see if I can figure out arrays. One last question then: Is it best to start with all the objects on the stage and add them all to an array (I believe the command is "push(object)") or do I keep all of the items off the stage and add them using something like "var waterbottle = new Bottle();"? And if they both work, which is preferable?
Thanks for your advice.
I would go with the = new Bottle() approach, especially if there are new bottles being added that way anyways. THere can be differences when dealing with manually-added versus dynamically-added objects, so dealing with them all one way will allow you to retain a consiustent set of commands.
North America
Europe, Middle East and Africa
Asia Pacific