So after debugging a bit more I figured out that the size of the physics body wasn’t a problem as it wasn’t changing but had something to do with the anchor point. See if the images are different in size the physics boy would be moving with the anchor point that was changing because the different sizes.
I solved my problem with either one of two solutions.
Solution A was changing the physics body from Box to circle.
Solution B (the one I am using) its to assign anchor point 0,0 to the SpriteFrames given by SpriteFrameCache.
The sprites i’ve been using for the animation arent one another followed in the name by numbers or anything so I took them one after the other from SpriteFrameCache and added them to a Vector<SpriteFrame*>
Here is some code:
auto physicsBody = PhysicsBody::createBox(Size(this->getContentSize().width,this->getContentSize().height),PhysicsMaterial(0.1f, 1.0f, 0.0f));
//Add the physics body
this->addComponent(physicsBody);
//Create an animation for the character for jumping
//We will do a 4 frames animation as follows : stand => ready => stand => jump and keep jump until touching something
char buff[100] = {0};
//Generate the frames using the spritnf to change between female(genderNum=2) an male(genderNum=1)
//Stand
snprintf(buff, sizeof(buff), "bunny%d_stand.png", genderNum);
auto frameStand = SpriteFrameCache::getInstance()->getSpriteFrameByName(buff);
//Ready
snprintf(buff, sizeof(buff), "bunny%d_ready.png", genderNum);
auto frameReady = SpriteFrameCache::getInstance()->getSpriteFrameByName(buff);
//Jump
snprintf(buff, sizeof(buff), "bunny%d_jump.png", genderNum);
auto frameJump = SpriteFrameCache::getInstance()->getSpriteFrameByName(buff);
/*
* Fix for the weird physics animation behaviour with SpriteFrameCache.
**/
//SpriteFrameCache does something really weird with the physic body when animating. We move the anchor point to 0,0 to avoid
//the physics body from moving if the rect of the animated sprites have not the same size.
frameStand->setAnchorPoint(Vec2(0, 0));
frameReady->setAnchorPoint(Vec2(0, 0));
frameJump->setAnchorPoint(Vec2(0, 0));
//Now we just need to add them to animJump Frames
Vector<SpriteFrame*> animJumpFrames;
animJumpFrames.reserve(4);
//Add the frames
animJumpFrames.pushBack(frameStand);
animJumpFrames.pushBack(frameReady);
animJumpFrames.pushBack(frameStand);
animJumpFrames.pushBack(frameJump);
//Create the animation that we will play
jumpAnim = Animation::createWithSpriteFrames(animJumpFrames, 0.05f);
Then after this when I call my jump() function I just call :
this->runAction(Animate::create(jumpAnim));
The key part of code that fixed my problem was changing the frames I created from SpriteFrameCache to have an anchor point set to 0,0 like this:
frameStand->setAnchorPoint(Vec2(0, 0));
frameReady->setAnchorPoint(Vec2(0, 0));
frameJump->setAnchorPoint(Vec2(0, 0));
Hope this is useful in case someone stumbles upon the same problem trying to animate with actions an sprite with a physics body attached.