Add sprite after scale also get scaled - cocos2d-x

I have to Sprites A and B ( A is human body and B is head ). I want to add sprite B to sprite A and to move together and I done this by
A->addChild(B) and it works. Problem is when I want to scale only body and then add head to small body ( head should be normal size ) and I done this like
A->setScaleY(0.3);
A->addChild(B);
but I also get B to be scaled. How to scale only body and then add head of normal size to small body ?

B is child of A so will be scaled with A this is correct... I recommend you to make third Sprite C (skeleton) and add A(body), B(head) sprites to it. After that you can scale A, B independent and move them all with sprite C or scale whole human body with all your parts with C(skeleton) sprite.

Two way to solution this.
Node/Sprite C
Sprite A;
Sprite B;
Sprite C;
A->setScaleY(0.3);
C->addChild(A);
C->addChild(B);
also set B scale.
float scale = 0.3f;
A->setScaleY(scale);
B->setScaleY(1/scale);
A->addChild(B);

Related

Why are there black lines between my sprites?

I have a single pixel sprite. To this sprite I add four sprites, each a quarter of a square. To offset the sprites, all i do is change their anchor points.
For example:
top right square is at anchor: (0,0);
bottom right : (0,1);
bottom left : (1,1);
top left : (1,0);
I expected the sprite edges to meet perfectly so that it looks like one big square. Instead there are black lines between the edges of each square so it looks like I have placed four squares close together.
I use texture packer to create a sprite sheet, containing the various squares.
Is there some setting in cocos2d-x or some code I must change to get the sprites to align perfectly ?
Edit: This is for cocos2d-x 3.1.1 and higher. Changing the anchor point is necessary and unavoidable.
EDIT: I use sprite frames from a sprite sheet created using TexturePacker. This was the problem. See my answer below.
The problem has something to do with using a sprite sheet (created using TexturePacker) to hold the pieces together. When you place the frames from the sprite sheet together to form a complete image the lines appear.
You can make the black lines disappear by setting the "Extrude" option in Texturepacker to at least 1.
EDIT: For those of you updating sprite positions based on a physics simulation, black lines can be caused by "sub pixel" positions. Try to either move your objects by complete pixels. Or search for answers with "sub pixel" for other solutions.
casting positions calculations to int type didn't help ?
Generally after certain float calculations like multiplying and divide and then complier auto demoting to to int may result in variation of 1px.
for example 26.500123 can be treated as pixel 26 or 27, depending to your casting methodology.
Test Case:
Are you saying you did this ?
auto testNode = Node::create();
auto s1 = Sprite::create("Images/1.png");
s1->cocos2d::Node::setAnchorPoint(Point(1,0));
auto s2 = Sprite::create("Images/2.png");
s2->cocos2d::Node::setAnchorPoint(Point(0,0));
auto s3 = Sprite::create("Images/3.png");
s3->cocos2d::Node::setAnchorPoint(Point(1,1));
auto s4 = Sprite::create("Images/4.png");
s4->cocos2d::Node::setAnchorPoint(Point(0,1));
testNode->addChild(s1);
testNode->addChild(s2);
testNode->addChild(s3);
testNode->addChild(s4);
testNode->setPosition(Point(screenSize.width/2,screenSize.height/2));
this->addChild(testNode);
and you got 1px gap ? i did that same with cocos2dx 3.1
i got this fine lady
Don't change anchorPoint, you'll regret it later. Calculate the correct position for each sprite.
Make sure the position x/y are on pixel boundaries. Casting to int will do the trick though Retina devices allow for 0.5 positions as well due to pixel density being 2x the point resolution.
Point #2 is also why you shouldn't use anchorPoint because you can't cast the position to integers when offsetting the texture with the anchorPoint.

ActionScript 3: Zoom into movieclip while not scaling its childrens

I've included a zoom functionality similar to the one explained at this website:
http://www.flashandmath.com/howtos/zoom/
This works perfectly on my background image(a map, that is), but I want to keep the symbols on my map the same size while zooming in.
I probably could work this out by changing all the children's size when calling the zoom-function, but I am hoping there is some kind of easy code adapt in my children class to make the size of the instances unchangable. Is there?
Thanks!
One crude way, so you don't have to calculate the symbols scale, would be to remove the symbols from the mapDisplayObject so they're no longer a child and instead put symbol placeholders. Then match each symbol's x and y to each place holder, using localToGlobal...
If your children are not scaled or skewed or rotated you can iterate all of them and set transformation matrix to 1/parentScale. Something like:
for each (var child:DisplayObject in parent) {
var matrix:Matrix = child.transform.matrix;
matrix.a = 1/parentScale;
matrix.d = 1/parentScale;
child.transform.matrix = marix;
}

how to create some dynamic sprites falling from top to bottom

I am making a game in cocos2dx(c++).Here i have to make dynamic sprites that are falling top to down and on touch i have to kill them.I mean the sprites are of insects and they will be killed on touch
The problem is that I am unable to get the idea on how to implement them as on increasing the level the insects will be falling more and with more speed.
I have made for one insect.But don't know how to handle for multiple insects
Suppose, your 1st Sprite is coming from y height and it fell to -y/2 with a velocity of 10.
Then, you have to make several sprites with differnt velocities and different positions of x.
You can move a sprite in cocos2dx with the help CCMoveTo()
You can use this custome method to move sprite
void GameLayer::_mov(CCSprite *cp, float x, float y)
{
cp->setPositionX(cp->getPositionX()-x);
cp->setPositionY(cp->getPositionY()-y);
}
In this method we passing sprite which we have to move and x is how much change in sprite position and y is how much change in sprite position. In this I subtract x and y from the current position of sprite but you can add also according to your requirement.
You have to call this method in your update method.

adding transparent sprite over another sprite

I need to place a transparent sprite over another sprite. The overlaying sprite will acept some mouse events. When a user move mouse over upper sprite a curve will be drawn. After it'll be processed it will be drawn on the base sprite (and erased on upper).
The idea I have now is to place the sprite, draw a rectange of size equal to sizes of sprite and set alpha to 0.
The question is a bit dump: maybe the proposed solution is not the best. Is there a better way to set width and height (as far a I understand Sprite.width = w; will not help)?
Thank you in advance!
You can't set dimensions directly, while you can draw over that Sprite. So you can do like this:
graphics.beginFill(0,0); // zero alpha fill
graphics.lineStyle(0,0,0); // invisible lines
graphics.drawRect(0,0,width,height);
graphics.endFill();
This way your Sprite can have its alpha remaining at 1, to not hide anything that's its child. Then, whatever curve you would decide to draw in that Sprite, you can draw within a child Shape object, via graphics.moveTo and graphics.lineTo.
UPDATE: According to comments below, setting alpha to 0 won't work with newer Flash player versions, so alpha should be set to a nonzero amount for the events to register on the overlapping sprite.
graphics.beginFill(0x808080,0.01); // almost zero alpha fill

MovieClip resizes, but its children's height and width are not changed?

Changing the width and height of the parent MovieClip does not bring change in the width and height of the inner MovieClip. The parent MovieClip is placed at Stage and is resized manually. When I assign the dimension of the parent MovieClip to the inner MovieClip through code, the parent MovieClip dimension is changed. I want both MovieClip to be of same width and height at runtime. However, parent MovieClip dimension is changed at design time by me.
Example:
There are two MovieClip, one inside another. Now parent MovieClip is placed at Stage at design time and its dimension is (50,50) and the child MovieClip which is inside the parent MovieClip has also same dimensions (50,50). Now, I manually change the parent MovieClip dimension by pressing Q and stretching it with mouse, the dimension of the parent MovieClip is now (100,150) or whatever I like. Now double-click on parent MovieClip and check that inner MovieClip dimension remains same i.e. (50,50)
Now in AS3 code, I change the width and height of inner MovieClip like this:
saveheight = parentmc.height;
savewidth = parentmc.width;
now I change the child MovieClip according to the dimensions of the parent MovieClip like this:
parentmc.inner_mc.width = parentmc.width;
parentmc.inner_mc.height = parentmc.height;
but this brings change in parentmc also so I reassign value to parentmc like this:
parentmc.height = saveheight;
parentmc.width = savewidth;
In above case, parentmc and inne_rmc dimension should be same i.e (100 ,150). With swapping the values as above, I get parentmc and inner_mc to be of same dimension, but object size is never (100, 150), I have checked it with pixel-perfect air app.
In your code, you are neglecting to account for the transformation to the parent that you did with the 'Q' tool in the authoring environment. The childmc's width and height are expressed in terms of parentmc's transformed coordinate space. If you wish to scale the inner clip to a specific size in stage coordinate space you need to account for the scale of the parent that resulted from your transform:
parentmc.inner_mc.width = parentmc.width/parentmc.scaleX;
parentmc.inner_mc.height= parentmc.height/parentmc.scaleY;
Also, if the clips aren't aligned (e.g. registered by their upper left corner and with the child at 0,0), enlarging the child could push out the boundaries of the parent.
You can also use the parent's transform matrix if you prefer that over using scaleX and scaleY.
UPDATE 4.8.11: Were you perhaps asking to do this (runtime removal of the authoring-time transform)?
saveheight = parentmc.height;
savewidth = parentmc.width;
parentmc.scaleX = 1.0; // Undo authoring-time scale transform
parentmc.scaleY = 1.0; // Undo authoring-time scale transform
parentmc.inner_mc.width = savewidth;
parentmc.inner_mc.height = saveheight;
parentmc.width = savewidth;
parentmc.height = saveheight;
Note: I'm not at a computer set up with Flash to test this, so please leave me a comment if this does not do what you are expecting, and I will happily check my work and follow up.
it doesn't quite work like that,
you need to multiply the children by the scale of the parent.
the other thing you can do is use getBounds and then you can get the bounding rectangle of any child (and child's children etc) relative to the parent
I honestly don't understand what you're asking, because what you describe should work!
If we have a parentMC and a childMC both with a 50 height and 50 width, if you change the parentMC.scaleX = 2; it will apear to be 100 in width. The same goes for the childMC.
Could you please provide an example of what you're trying to do here? - or some code.