Error while using removeChild() and accessing members of array

51 views Asked by At

I am stuck doing this even though I know it's very simple. Yet, I am getting errors.

What I have:

I have 3 arrays.

  1. 1st Array contains objects of UpgradeButton class.
  2. 2nd Array contains objects of BuyButtonclass.
  3. 3rd Array named newCostlyShops contains Numbers.

BuyButton class and UpgradeButton class, both have a shopCode member which is a number; the number which I'm trying to equate.

What I'm trying to do:

My goal is to first look for BuyButton and UpgradeButton objects in the respective arrays which have shopCodes same as those in newCostlyShops.

After that, I removeChild() that object and splice it out from the array.

My Code:

Array 3:

var newCostlyShops:Array = new Array();
newCostlyShops = Object(root).WorkScreen_mc.returnCostlyShops();
trace(newCostlyShops); // this is tracing out the exact shopCodes I want and is working fine.

Deletion and Splicing codes:

for (looper = 0; looper < upgradeButtonsArray.length; looper++) {
  for (var secondLooper: int = 0; secondLooper < newCostlyShops.length; secondLooper++) {
    if (upgradeButtonsArray[looper].shopCode == newCostlyShops[secondLooper]) {
      trace(looper);
      trace(upgradeButtonsArray[looper]);
      removeChild(upgradeButtonsArray[looper]);
      upgradeButtonsArray.splice(looper, 1);

    }
  }
}
for (looper = 0; looper < buyButtonsArray.length; looper++) {
  for (secondLooper = 0; secondLooper < newCostlyShops.length; secondLooper++) {
    if (buyButtonsArray[looper].shopCode == newCostlyShops[secondLooper]) {
      trace(looper);
      trace(buyButtonsArray[looper]);
      removeChild(buyButtonsArray[looper]);
      buyButtonsArray.splice(looper, 1);

    }
  }
}

What's wrong with this Code:

I keep getting error

TypeError: Error #1010: A term is undefined and has no properties.

This error comes only after the 1st time this code is run and not the first time it is run. When I remove the removeChild and splice , this traces out objects that are not null, ever. Even after this whole function is called 100 times, the error is not shown. Only when I removeChild and use splice this occurs.

Is there something wrong with what I'm doing? How to avoid this error? This is throwing the whole program haywire. If there is any other alternative to this method, I'm open to take those methods as well as long as I don't get errors and my goal is reached.

1

There are 1 answers

0
3vilguy On

It might sounds funny, but.... try to decrement looper after splicing.

  trace(looper);
  trace(upgradeButtonsArray[looper]);
  removeChild(upgradeButtonsArray[looper]);
  upgradeButtonsArray.splice(looper, 1);
  looper--;

I think after splicing the array all item's are being shifted and you're skipping next one.

Also, you should get some more information with this error, like which class/line is throwing it. Maybe you need to enable "permit debugging" or something?

Bonus suggestion: For newCostlyShops use Dictionary instead of Array so you won't have to nest for inside for...