Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411722 Posts in 69402 Topics- by 58450 Members - Latest Member: FezzikTheGiant

May 22, 2024, 12:49:51 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)The supplied DisplayObject must be a child of the caller error :/
Pages: [1]
Print
Author Topic: The supplied DisplayObject must be a child of the caller error :/  (Read 3968 times)
davidp
Level 6
*



View Profile WWW
« on: August 22, 2010, 02:39:21 PM »

So i've been messing around with my game lately and hit a wall with this error:

[Fault] exception, information=ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

I know what it means but I'm totally lost what might be causing it :S

here is the code for child creation:

Code:
effect1 = new Effects1("smoke", 0, 0);
addChild(effect1);
effect1Array.push(effect1);

OR

Code:
effect1 = new Effects1("fire", 0, 4);
addChild(effect1);
effect1Array.push(effect1);

child removal:

Code:
for each(effect1 in effect1Array)
{
  effect1.enterFrame();

  if (effect1.die)
  {
    effect1Array.splice(effect1, 1);
    removeChild(effect1);
  }
}

now, Effects1.as:

Code:
 blabla
public function Effects1(inType:String, inDir:int, inSpeed:int):void
{
  type = inType;
  my_dir = getRandom(361);
  speed = inSpeed;

  smokeFramesArray = new Array();
  var row:int;
  var col:int;
  if (type == "smoke")
    row = 0;
  if (type == "fire")
    row = 1;

  for (var i:int = 0; i < 4; i++)   
  {
    var frame:BitmapData = new BitmapData(wid,hei);                                     
    var frameRect:Rectangle = new Rectangle(i*wid,hei*row,wid,hei);
    frame.copyPixels(smokeFireSPR, frameRect, frame.rect.topLeft);         
    smokeFramesArray.push(frame);   
  }

  var random:int = getRandom(5)
  bitmapData = smokeFramesArray[random];
}

public function enterFrame():void
{
  if (!pause)
  {
    x+= speed*Math.cos(my_dir*(Math.PI/180));
    y-= speed*Math.sin(my_dir*(Math.PI/180));

    if (countdown > 0)
    {
      if ((countdown % 2) == 0)
        alpha -= 0.1;

      countdown -= 1;
    }
    else
      die = true;
  }
}

i dont have a slightest idea what might be the problem WTF
Logged

agersant
Level 4
****



View Profile
« Reply #1 on: August 22, 2010, 02:49:05 PM »

Hello there.

I didn't read all of your code but your error probably occur when you call removeChild on an object which is not a child of the DisplayObjectContainer you're removing it from.
Logged
Nix
Guest
« Reply #2 on: August 22, 2010, 04:04:22 PM »

Are you reusing the effect1 variable each time you add an effect?

Edit: More complete source might be helpful.
Logged
voidSkipper
Level 2
**


View Profile
« Reply #3 on: August 22, 2010, 09:26:17 PM »

Agersant is correct, this error is due to your effect1 not being in the display list.

I have a funny feeling that this is happening because your splice command is not splicing properly, so when your loop gets called again, objects are in the array but not in the display list and are therefore still trying to be removed.

Try this for me:
Code:
var i:uint = 0
var effect1:Effects1;
while(i < effect1Array.length){
  effect1 = effect1Array[i];
  effect1.enterFrame();

  if (effect1.die)
  {
    effect1Array.splice(i, 1);
    removeChild(effect1);
  }
  i++;
}

EDIT: AS is such an easy language to debug, you should really make use of trace more often. Trace out the effect1Array array after each splice and check that your object is being removed properly, etc.
Logged
davidp
Level 6
*



View Profile WWW
« Reply #4 on: August 23, 2010, 12:04:08 AM »

well, i've been thinking about array not splicing correctly for some reason.

i've also used trace for displaying which child i was removing (type, its countdown and die boolean, but that wasn't really useful, i should've been using it in connection with array, yea).

as a matter of fact, i'm using voidSkipper's method for bullet removal and some others, so i guess i'll try it with this problem too.

the thing is i really don't see why or how for each loop would skip or splice array improperly. i mean, all of the effect types have the same life span (20 cycles) and die as they appeared on screen - 1st which appeared, should be removed first. so when first effect dies (it's countdown reaches zero) it should be first spliced from array and then removed from screen.

or am i missing something on my logic?
Logged

st33d
Guest
« Reply #5 on: August 23, 2010, 01:29:44 AM »

Are you sure this will work?
Code:
for each(effect1 in effect1Array)
{
  effect1.enterFrame();

  if (effect1.die)
  {
    effect1Array.splice(effect1, 1);
    removeChild(effect1);
  }
}

I'm not entirely sure that for each will account for the changing size of the array.

Also - splice's first parameter is the index of the item you wish to delete, not the item.

You could try iterating through the list backwards and splicing as you go. Any item you splice out won't matter to the index count.

Code:
for (var i:int = effect1Array.length - 1; i > -1; i--){)
{
  effect1Array[i].enterFrame();

  if (effect1Array[i].die)
  {
    removeChild(effect1Array[i]);
    effect1Array[i].splice(i, 1);
  }
}
Logged
davidp
Level 6
*



View Profile WWW
« Reply #6 on: August 23, 2010, 01:41:04 AM »

yea, it's obvious now i should be using this method with all removing instances.

my "solution" works in some cases, but obviously not all, so yea... thanks guys, i'll go and unify all my remove child functions to suggested ones.
Logged

voidSkipper
Level 2
**


View Profile
« Reply #7 on: August 23, 2010, 04:56:57 AM »

Also - splice's first parameter is the index of the item you wish to delete, not the item.

I thought that looked odd, but it occurred to me that I'd never looked at the splice language reference and that it might have been overloaded.
Logged
st33d
Guest
« Reply #8 on: August 23, 2010, 06:24:05 AM »

you can't overload in Flash
Logged
voidSkipper
Level 2
**


View Profile
« Reply #9 on: August 23, 2010, 08:05:38 PM »

I'm aware that you can't, but I could have sworn some of the inbuilt functions were overloaded...
Logged
st33d
Guest
« Reply #10 on: August 24, 2010, 12:38:30 AM »

Nope  Durr...?
Logged
bateleur
Level 10
*****



View Profile
« Reply #11 on: August 24, 2010, 03:30:21 AM »

I'm aware that you can't, but I could have sworn some of the inbuilt functions were overloaded...

There are two things which sometimes give this impression:

1) Flash supports optional parameters. You can do this for your own functions too, which is often quite useful for complex things.

2) Flash sometimes type-converts things if it knows how to get to the type it wants from where it is.
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #12 on: August 25, 2010, 10:11:44 AM »

Not to mention you can throw away static typing altogether.
Logged
voidSkipper
Level 2
**


View Profile
« Reply #13 on: August 25, 2010, 03:58:14 PM »

Is it possible to have a static method with the same name but a different signature to a non-static method?

I /swear/ I've seen autocomplete change the type and name of the first variable requirement when toggling through possible constructor arguments before, but I might be on hella crack or something.
Logged
bateleur
Level 10
*****



View Profile
« Reply #14 on: August 25, 2010, 11:08:05 PM »

Is it possible to have a static method with the same name but a different signature to a non-static method?

Yes, but the way you word it is a little misleading, because it's possible to have static and dynamic methods with the same name and the same signature too. (The way it works is that ambiguous calls are assumed to be to the dynamic function if both exist.)
Logged

davidp
Level 6
*



View Profile WWW
« Reply #15 on: August 26, 2010, 03:04:41 AM »

it's a shame Flash don't support function overloading, it's funny how you miss it when it's gone (in a sense where you work with c# everyday at work and then come home to work with flash and have these differences)
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic