0
votes

Argument Error #2025:

The supplied DisplayObject is not a child of the caller

In my program I have a structure of display objects representing enemy units on screen, arranged like this:

0: "enemy handler" ➔ [object EnemyHandler]
  0: "enemy" ➔ [object Enemy1]
    0: "enemy's skin"(MovieClip) ➔ [object EnemySkin1]
      0: "skin image" ➔ [object Shape]

The document class contains one instance of EnemyHandler, and it contains multiple instances of Enemy1, Enemy2 ect.. each of these has a skin MovieClip attatched.

The Enemy class (from which Enemy1/2/3 ect inherits) contains a property expired, which is set to true, from within the Enemy class (enemy.update), when the Enemy has reached a certain point.

In the enemy handler class is a function, and is where the problem lays, that loops through an array of all the Enememy display objects, updating there position and then,

if(tempEnemy.expired)
{
    tempEnemy.destroy();            // removeChild(skin)
    enemyList.splice(tempEnemy);
    removeChild(tempEnemy)
}

when run, if all of the enemies reach the end-point in the order in which they were created, there is no problem, however if one for instance travels faster, and reaches the endpoint before, an error #2025 (The supplied DisplayObject is not a child of the caller) is thrown.

I've narrowed it down to the fact that the program attempts to remove the enemy twice, for reasons that i cannot discern. It loops through and removes the desired enemies, then attempts to remove it again, even though it is spliced from the array (which happens correctly, and the first attempt at removeChild is allways succesfull)

It is probably something fairly simple on my behalf, so forgive me, but any help is appreciated. here's the files;

[code on frame] pastebin.com/vcXzQpr9

[Enemy.as] pastebin.com/RNXgK8Ex

[EnemyHandler.as] pastebin.com/6fytxbMW

[Enemy0.as] & [Enemy1.as] pastebin.com/5bW3Aa0H

[Utils.as] pastebin.com/PQ2LPV0v

[traceDl.as] {debuggnig the display list} pastebin.com/9vQGKcYP

1

1 Answers

1
votes

Array.splice() takes an integer for the position where you want to start splicing, then how many items you want to remove. So you need to use

enemyList.splice(enemyList.indexOf(tempEnemy), 1);

instead of passing the Enemy instance. It may have appeared to work correctly when the order didn't change, because coercing tempEnemy to int (which splice() will have done automatically) produces a zero, so in your destroyEnemy function it was actually just removing the first item in the list each time.