Can I add several sprite children with the same index? - actionscript-3

I want 2 sprites on the one index position. The sprites have different coordinates (x attributes), so, each of them are visible when the child position is active.
Clarification, the code:
addChildAt( sprite1, 1 );
addChildAt( sprite2, 1 );
Are both of the sprites visible?

The documentation gives you the answer of what will happen with your snippet.
index:int — The index position to which the child is added. If you specify a currently occupied index position, the child object that exists at that position and all higher positions are moved up one position in the child list.
Two objects can't share the same index because it wouldn't make sense. How would you display two overlapping objects at the same index?

No, you can't. But you can add extra sprite between parent and children to easily control visibility or whatever.
Sample:
parentSprite.addChild(extra)
extra.addChildAt( sprite1, 1 );
extra.addChildAt( sprite2, 2 );

Related

parent-child depth will change in actionscript

Please have a look at this project, in this project I have two symbol, one of them create and add another symbol as child.
Child symbol is drawn over parent as I expected. Parent symbol has two frames. When parent goes to second frame, child symbol drawn under parent. I mean depth order will change.
Can somebody help me!!?? Sorry for poor language.
Download link:
https://drive.google.com/file/d/0B-KCX3wxRH-cOUk5YU1OUzNFN3M/view?usp=sharing
Yellow shape from the frame 1 move to the new layer.
Hide Layer 3 and go to the second frame.
Select and remove the yellow bitmap image.
Show Layer 3.
Another solution:
Add next code after gotoAndStop(2);
swapChildren(btselect,getChildAt(numChildren - 1));
You misunderstand completely how the Flash rendering works so here's a couple of pointers:
"Child symbol is drawn over parent as I expected", nope, that doesn't happen. A parent has content which can be pictures, other symbols ect ... and they are all inside the parent so no child are drawn over the parent, they have a display hierarchy that's all.
"child symbol drawn under parent." well once again nope, a child is not drawn under it's parent, it is inside the parent and under other objects.
When a MovieClip with multiple frame displays the content of a frame it adds those content to it's display list (inside itself). After that if you add another child in it it will be on top. Then you move to another frame let's say frame 2. Once again the MovieClip adds the content of frame 2 inside itself, but since the child you added in the previous frame is already there all the new content is added on top of it.

Can LibGDX do relative rendering when a sprite is a child of another?

With frameworks such as Cocos2d and SpriteKit, it is possible to add a sprite/label/node as a child of another. If a node is rotated, then the rendering of its children will be affected by such rotation and so on throughout the node tree.
Can this be achieved in LibGDX?
Yes this can be achieved using Scene2d.
Wiki Entry:
Rotation and scale of a group is applied to all child actors. Child actors always work in their own coordinate system, parent transformations are applied transparently.
What you are looking for is a scene Graph and this is provided as mentioned in the Scene2d api. What you do is, you create a group, add your sprite, e.g. a tank, add a label and other stuff. Then you can rotate the group and all children will be rotated with it. If you want just rotate the tank this is still possible but in most cases you just transform the root node, a Group.
The sprite itself is leave and if you won't to transform some sprites together you just put them in the same group and apply the transform their.
If you wanna create several 100 Sprites with a label together you could do this: Implement a RefNode which references a Group. Create a group where you add a sprite and a label. (if you wanna change the label text you would have to store it separately and update it before you actually render the referenced group) Then you can apply all the transformation to the RefNode.

actionscript3: removeChild and addChild

I add a child to stage, and i need my code trace if this child present in the stage and remove this child from this position to add it in another position in the stage
Please help me
I THINK you're asking how to move a child from one place in the display list to another. If so, you do not need to remove the child first; When you addChild to the new 'location' the child will be removed from the old location (a child can only exist in one place on the display list). That's assuming you don't need the child to disappear for some period of time before reappearing in the new location.
Alternatively you can check if a display object contains another with this.contains(referenceToChild);
A child is on the stage if its parent property is not null:
trace( child.parent !== null ); // True if the child is on the stage.
For positioning you just need to adjust x and y.

as3-manipulating z-index dynamically

So I have trouble with the z-index,
all of my objects have z-index of 0, and as new objects are created they come above the ones that need to be in front.
I know about setting z index commands, but if I have 50 objects, I have to write and manually set z-index for each one, witch is kinda lame.
How can I fix this? This is probably simple but Im new to AS3.
If by z-index you mean the z value of a DisplayObject, that doesn't affect the layering of them. Depth ordering is handled by the display list of its parent.
You can make a DisplayObject go all the way to the back by using container.addChildAt(displayObject, 0); or container.setChildIndex(displayObject,0); if it's already added to that parent's display list.
You don't have to change the indexes of all other children of the same parent.
If you want something to be layered right behind another DisplayObject, first find out what the index of that child is:
var i:uint = container.getChildIndex(theOneToHideBehind);
and then set the index of your DisplayObject to that value:
container.setChildIndex(myDisplayObject, i);

as3: Making an animation

I am playing with animation in AS3 and flex4, and I've come into a problem. My application is a game board (like a chess board), where each field is a border container added to some position.
Also I am adding a child element (shape), to this container on mouse click. What I want to achieve is to be able to move shapes smoothly from one field to another. But it appears that the shape goes behind the neighbor field this way http://screencast.com/t/iZ3DCdobs.
I believe this happens because shape is a child of specific border container, and to make it visible over every other container, I would need to use layers somehow....
I would be happy if anybody could suggest a solution
Yes you're right on that. You should add the movable objects to a different layer.
As there are no typical layers in AS, you could try to drop the fields in one sprite and any other objects to a different an than place them on each other, so that when you will move a object it won't go behind other objects.
If you place both sprites in the same position you will still have accurate x,y positions between movable objects and fields.
You have two options:
First one is to have different layers for your DisplayObjects: as an example, the bottom layer would hold all the boards, and the upper layer would hold all the pieces.
Second option is to manipulate the index of the objects with swapChildren(), swapChildrenAt(), and setChildIndex(). So to bring a MovieClip to the topmost front, you would do MovieClip(parent).setChildIndex(this, 0);
If the situation is that always the shape object gets hidden behind the next ( right side ) grid container, the I suggest you create your grid in reverse.
Suppose you are creating a chess grid. that is a 8x8 grid. Normally you would create your grid using 2 for loops, looping from 0 to 8 with say the x and y points starting at 0,0 for the first grid and going on till the end. What I suggest you to do is to create from 8,8 to 0,0.
Display objects in flash are stacked on top of each other based on their child index.
For example: If you create two objects. Rectangle and Circle as follows
var rect:Rectangle = new Rectangle();
this.addChild(rect);
var circ:Circle = new Circle();
this.addChild(circ);
The circle will always be on top of the rectangle in this scenario because the circle was added after the rectangle to the display list.
So if you reverse the order of creation of your grid, the right grid cell will be added to the display list first and so the grid cells to the left will always be on top of the right ones. Hence, the problem that you are facing will not occur.