A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [AS2] Removing Broken Movieclips from an Array

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    9

    [AS2] Removing Broken Movieclips from an Array

    Hi, my game has an array that I am pushing movieclips into as I attach them from the library. However, when I use removeMovieClip(), the moveclip gets deleted but its number in the array stays in the array and lists the movieclip as "<broken>". The problem is if my game goes on for too long the array would become huge from all of the <broken> movieclips that were not removed. So when I do a for..in loop to test for collision, the game would slow down a lot. How would I be able to remove the <broken> movieclips out of the array with the splice technique?

    this is what my array shows:

    Variable _level0.myArray = [object #3, class 'Array'] [
    0:[movieclip:_level0.movieclip0],
    1:[movieclip:<broken>],
    2:[movieclip:<broken>],
    3:[movieclip:<broken>],
    4:[movieclip:_level0.movieclip0],
    5:[movieclip:_level0.movieclip0],
    6:[movieclip:<broken>],
    7:[movieclip:<broken>],
    8:[movieclip:<broken>],
    9:[movieclip:<broken>],
    10:[movieclip:<broken>],
    11:[movieclip:<broken>],
    12:[movieclip:_level0.movieclip7],
    13:[movieclip:_level0.movieclip8],
    14:[movieclip:_level0.movieclip9],
    15:[movieclip:_level0.movieclip10]
    ]

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Can you post the for..in loop code here?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    this is the code I am using:

    Actionscript Code:
    onClipEvent (enterFrame) {
    for (var i in _root.myArray) {
                if (_parent != _root.myArray[i]) {
                    if (_parent.hitTest(_root.myArray[i]) || _parent.hitTest(_root.surface)) {
                        _parent._y += -5;
                    }
                }
            }
        }

    The game would slow down because each movieclip is performing a hitTest on a giant array of broken movieclips I had removed. How do I remove the broken movieclips so my array only contains the movieclips I am using?

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    if (_parent.hitTest(_root.myArray[i])) {
    	_root.myArray.splice(i,1);
    }

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Quote Originally Posted by dawsonk View Post
    Code:
    if (_parent.hitTest(_root.myArray[i])) {
    	_root.myArray.splice(i,1);
    }
    That code will only remove the current movieclip if the hitTest is successful, correct? The problem is, after the hitTest is successful, I don't want to remove that movieclip from the array yet because I still need it for performing further hitTests with other movieclips. I only want to remove the broken movieclips out of the array when I use the removeMovieClip() command;

  6. #6
    Senior Member
    Join Date
    May 2009
    Posts
    138
    So what's stopping you from splicing it out when you use removeMovieClip?

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Quote Originally Posted by redjag View Post
    So what's stopping you from splicing it out when you use removeMovieClip?
    How can I use splice to remove the exact movie clip in the array that I am using removeMovieClip on?

  8. #8
    Senior Member
    Join Date
    May 2009
    Posts
    138
    Well, if you don't know the index on it you can always loop thru the array using removingMC === _root.myArray[i] to find it. Not very efficient but it'd work.

  9. #9
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Code:
    _root.myArray.splice(_root.myArray.indexOf(removingMC), 1);
    EDIT: Nevermind, you're using deprecated AS2.
    Last edited by ImprisonedPride; 12-19-2010 at 12:57 AM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  10. #10
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Code:
    var i = _root.myArray.length;
    while(--i) {
         if(removingMC === _root.myArray[i]) {
              _root.myArray.splice(i, 1);
              break;
         }
    }
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  11. #11
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    hey sorry to interrupt, I like the splice idea. is there a way to run through the array with a loop to splice anything that has [x][0] = "", like maybe a loop and the number of loops = the number of array indexes. and each loop (starting with 0) would be x, and then every time it checks if [x][0] = "" (say a multidimensional array where 0 is the name), then splice it out and lower the array count by one, and move on to the next one. so if like [0][0] = "i" [0][1] = "i" [0][2] = "" [0][3] = "i" [0][4] = "" [0][5] = "" [0][6] = "i" ... So if he had like 5 movie clips not in order that he wanted to remove from his array and from the stage.. I am thinking that's the way you do it. and splice would be _root.myArray.splice(x, 1);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center