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

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.

Related

Avoid transformation for child objects in easel js container

I am using easel js for working with a drawing application and have functionality like resize rotate etc. For a task I need to write a letter on a image and used a container and added image and text as children.
Now the issue if I transform the container I don't need to reverse the letter only image.
I am using the following link for transformation:
https://github.com/senocular/TransformTool
Attach: https://www.dropbox.com/s/vxg84liohd24knh/Untitled.png?dl=0
If you are able to target the sub-content, you can un-transform it simply using:
// Simple un-flip
clip.subClip.scaleX = 1/clip.scaleX;
If you need more complex un-transforms, you might need to spend some time on the math to do that for each property, or look into Matrix transformations.
The easiest way is to decouple the element from the container. Have it be a sibling that updates its position to match. This gives you much better control over the display.

Foreground for Flash games

I've noticed that most Flash games (and other SWFs) have foregrounds incorporated into them. The reason being to cover up graphic you're not supposed to see upon entering full screen (graphic outside the boundaries of the stage).
The foreground is simply a piece of graphic with a stage-sized hole in the middle, where the stage will show through. (Or that is what it seems to be, anyway).
What would be the simplest way to incorporate something like this into a Flash game? (In terms of highest childIndex etc). All help appreciated.
Masking is the best solution for this. You have to simply create a shape (rectangle or even circle or a complex set of drawing) and then setting it as mask. Then The masked viewport only shows the area of the mask's shape.
Take a look at this
http://www.republicofcode.com/tutorials/flash/as3masking/
stage.addChildAt(myMC,stage.numChildren-2);
This will add myMC to the stage at the z level that is just below the highest z-order (which should be where you put your border).
To explain further, numChildren returns the number of children that the DisplayObject it is called on has. But remember, the z-order index is "zero based" which means that if the parent display object has 12 children, the numChildren method will return 12, but the highest z-order index will be 11 and the first will be 0. So the highest z-order child will have an index of 'numChildren-1(this could be where the mask/border) and the highest level below that would benumChildren-2`.

Can I colorTransform a MovieClip but exclude specific sub-components?

Scenario:
I have floating islands that move around in odd paths. There are torches (nested movieClips) mounted on some of the islands. Sometimes a weather effect will change the color and brightness of the islands. Is there a way to change the brightness of the islands (parent movieClips) without also changing the brightness of the torches (child movieClips)?
===================================================
One solution that I can conceive is independently color adjusting all the child objects of each movieClip, instead of adjusting the parent object. This seems highly inconvenient and unnecessary though, so any thoughts would be appreciated.
No, if you need to exclude some parts of a movie clip (or other DisplayObjectContainer) from alteration applied to their parent, you need to rework your container structure. You can try applying a reverse operation to selected children (say, inverse color transformation), but note that not every transformation can be correctly inversed.

cocos2d, reorder children in different batch node

I am creating a fighting game. I've got a spritesheet for the hero and a spritesheet for a monster. Since both will be able to do multiple attacks I would like to avoid to merge them in a single spritesheet. And I am intending to create more characters.
Now I am creating a SpriteBatchNode for each spritesheet and add them to layer.
CCLayer* stage = CCLayer::create();
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("character_hero.plist");
this->characterHeroBatchNode = CCSpriteBatchNode::create("character_hero.pvr.ccz");
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("character_monster.plist");
this->characterMonsterBatchNode = CCSpriteBatchNode::create("character_monster.pvr.ccz");
stage->addChild(characterHeroBatchNode);
stage->addChild(characterMonsterBatchNode);
Now I have one sprite in characterHeroBatchNode and multiple in characterMonsterBatchNode.
How can I reorder the monsters and the heros z-Order based on their PositionY attribute.
For example>
monster1->setPositionY(10); // In monster batch node
hero->setPositionY(24); // In hero batch node
monster2->setPositionY(43); // In monster batch node
I want the monster 1 behind the hero. And the hero behind monster2.
In the past, I've had to create games where I wanted some sprites in the foreground and others in the background. The sprites were in different sheets, like you have them.
To get them in the order I wanted, I put the "front" sprites into one CCLayer and the "back" sprites into another CCLayer. I added the layers to the scene in the order I wanted them to appear. I also manipulated each the respective layers to make them fade in/out as needed. So you could have the hero in the scene and then have the enemies appear behind him.
If you want to mix and match, you can have 3 layers, one for the "middle ground", one for "front", and one for "back", and dynamically move the sprites between the layers.
Was this what you were looking for?
If I understand what you're asking, the answer is, you can't. Consider each CCSpriteBatchNode to be its own container. You can adjust the Z order of the sprites inside the CCSpriteBatchNode, but when you add the batch to the CCLayer the whole CCSpriteBatchNode is applied to the CCLayer in whatever Z order you added it at. So in order to do what you want you would need another batch of monsters. Or use CCSprites and add/adjust them on the CCLayer in the way you want, but then you obviously lose the CCSpriteBatchNode benefits.

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.