Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is it possible to use swapChildren() with a child and a child's child? Every time I try, I get this error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

Why can I not compare a child with a child's child? How can I?

share|improve this question
    
You're not comparing anything. You're trying to swap children in different parents. That's not what swapchildren does. It only looks at the containing children of said parent. –  Sidar Feb 24 '13 at 17:12

3 Answers 3

up vote 4 down vote accepted

Note, that DisplayObjectContainer has 3 main methods: addChild, removeChild and getChildAt. All other mehtods (addChildAt, contains, getChildIndex, removeChildAt, setChildIndex, swapChildren, swapChildrenAt) are redundant (unnecessary) and can be implemented using 3 main methods above. Adobe added them for convenience, but it doesn't do quite what you're expecting.

Why don't you implement that "weird swapChildren" by yourself, using those 3 methods? Like so:

function weirdSwapChildren(childA:DisplayObject, childB:DisplayObject):void
{
    var p1:DisplayObjectContainer = childA.parent;
    var p2:DisplayObjectContainer = childB.parent;
    var i1:int = p1.getChildIndex(childA);
    var i2:int = p2.getChildIndex(childB);
    p1.removeChild(childA);
    p2.removeChild(childB);
    p1.addChildAt(childB, i1);
    p2.addChildAt(childA, i2);
}
share|improve this answer
2  
Good answer. I took the liberty to improve your code a bit by introducing proper AS3 syntax and removed the need for container parameters (you can get these using the parent property on the child). –  bummzack Feb 24 '13 at 10:40
1  
Just as a note: You don't need to remove a child when adding it to a different parent. The property "parent" automatically changes. Thus calling addchild on Y to add K while K being child of X, K is automatically "unregistered" from X. Also if(childA.parent) en if(childB.parent) are needed incase the parent property is null. –  Sidar Feb 24 '13 at 17:08
    
I know that parents are "swapped" automatically and one DisplayObject can not have multiple parents, but it may look confusing without removeChild. –  Ivan Kuckir Feb 24 '13 at 17:41
    
Thanks for your answer it just came to me there are many things adobe forgot in actionscript that i can make with intense functions. –  user25191 Feb 25 '13 at 13:30

From the documentation: swapChildren: Swaps the z-order (front-to-back order) of the two specified child objects.

A DisplayObject's z (or index) and it's position in the display object hierarchy are two different things. The z describes in what order the display objects are rendered within the container. A handy way of deciding what should be on top of what. swapChildren is designed for changing this property.

The DisplayObjects' position in the hierarchy also affects the rendering order but it's not the main thing. Children inherit the location, visibility, filters and a lot more.

Enabling one method to do both z swapping and changing the hierarchy is against good practice as they are two very different things. https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-you-drop

share|improve this answer
    
The asker said nothing about z-index. I don't see how this answers the question. –  Anko Feb 24 '13 at 12:01
4  
The question was about swapChildren, why it doesn't do what he would ike it to do. From AS3 documentation, "swapChildren: Swaps the z-order (front-to-back order) of the two specified child objects." So the z is relevant. –  JohannesA Feb 24 '13 at 12:06

I did not until just this moment even know swapChildren() exists. Reading the description it seems pretty pointless to me. For what you want I would just use calls like object1.parent and parent.addChild(object2)

share|improve this answer
    
I don't see how it's pointless. It's simply a build in function that swaps two DisplayObjects in the SAME parent. It's not meant to swap a child from a parent to another. Unless you mean it's pointless to use it in the context of how Abe is trying to use it, then I agree. –  Sidar Feb 24 '13 at 17:30
    
Well it looks pointless to me since that functionality is so easy to do with other calls. But that's just me; clearly Adobe disagrees so whatever. –  jhocking Feb 24 '13 at 19:54
    
And you can achieve other things differently as well. This function is a convenient and probably optimized way of doing it( after all it's their API). Adobe flash is also geared towards designers rather than just pure programmers. –  Sidar Feb 24 '13 at 21:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.