Sprite Animation while MoveBy Action

Hi Folks,

Goal:
I am trying to move my sprite(man) to the right continuously while its sprite’s frames is changing. Changing as in loop.

Actual(Current):
The scenario is that the sprite moves to the right but its frames changes at interval. So its effect seems that the man moves but his feet is only moving at some time.

Here is my code so far:

spritecache->addSpriteFramesWithFile("walkRight.plist");

characterSpriteBatchNode=SpriteBatchNode::create("walkRight.png");
characterSpriteBatchNode->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(characterSpriteBatchNode);

characterSprite=Sprite::createWithSpriteFrameName("right1.png");
animation=Animation::create();
for(int i=1;i<=4;i++)
{
	char szName[100]={0};
	sprintf(szName,"right%d.png",i);
	animation->addSpriteFrame(SpriteFrameCache::getInstance()->spriteFrameByName(szName));
}
animation->setDelayPerUnit(5.0f); 
characterSprite->runAction( RepeatForever::create( Spawn::create(Animate::create(animation), MoveBy::create(MOVESPEED, Vec2(MOVE_BY, 0)), nullptr))); 

characterSpriteBatchNode->addChild(characterSprite);

Thanks.

Use a ParallaxNode. See here

and there is a great example of this in cpp-tests

You have to understand that the Spawn action itself only completes when the longest inner acction completes. Only then does it repeat with the RepeatForever. The solution is separate them into two actions run both on the sprite separately.

// length of time that it should take the MoveBy action to complete
auto dur = MOVESPEED;
// offset in pixels to move by
Vec2 offset(MOVE_BY, 0);
characterSprite->runAction(RepeatForever::create(Animate::create(animation)));
characterSprite->runAction(RepeatForever::create(MoveBy::create(dur, offset)));
3 Likes