Skip navigation
Currently Being Moderated

Trouble with removing objects using ActionScript

Apr 9, 2012 9:03 PM

Tags: #problem #flash #action_script_3 #event #ac3 #game

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.

 
Replies
  • Currently Being Moderated
    Apr 10, 2012 4:20 AM   in reply to Razzi_A

    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. 

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2012 12:01 PM   in reply to Razzi_A

    If the objects are already on the stage, then what are you using to try to remove them, and where are you trying to do it?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2012 2:15 PM   in reply to Razzi_A

    If you are getting the 1009 error, then go into your Flash Publish Settings and select the option to Permit Debugging.  That should help by including the line number where the problem arises.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2012 2:49 PM   in reply to Razzi_A

    You should use something like an array to store references to the objects and use that array to target them for anyting you do with them.  As you remove objects, remove them from the array so that they will not be targeted again

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2012 5:09 AM   in reply to Razzi_A

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points