CCSequence call Class Functions

I have a 4 animation function in my Vehicle Class

How do I run my accelerate action animation and then call another class function like so

//ANIMATION
void Vehicle::drive()//DRIVE
{
	for (int i = 0; i < 2; i++)
	{
		auto frame = SpriteFrame::create("Ship/VehicleOne/drive.png", Rect(90 * i / TIscale, 0 / TIscale, 90 / TIscale, 30 / TIscale));
		animationFrames.pushBack(frame);
	}
	auto animation = Animation::createWithSpriteFrames(animationFrames, 0.1f);
	auto animate = Animate::create(animation);
	sprite->runAction(RepeatForever::create(animate));
}
void Vehicle::accelerate()//ACCELERATE
{
	for (int i = 0; i < 6; i++)
	{
		auto frame = SpriteFrame::create("Ship/VehicleOne/accelerate.png", Rect(90 * i / TIscale, 0 / TIscale, 90 / TIscale, 30 / TIscale));
		animationFrames.pushBack(frame);
	}
	auto animation = Animation::createWithSpriteFrames(animationFrames, 0.1f);
	auto animate = Animate::create(animation);
	sprite->runAction(Repeat::create(animate, 1));

	sprite->runAction(CCSequence::create(cocos2d::Repeat::create(animate, 1),  drive(), NULL));
}

Paying attention to the last part

sprite->runAction(CCSequence::create(cocos2d::Repeat::create(animate, 1), drive(), NULL));

Thanks everyone

Look into the various CallFunc (CCActionInstant.h) actions, and associated macros CC_CALLBACK_[N] for usage with class methods.

// replace `drive()` with:
CC_CALLBACK_0(Vehicle::drive, this)

You can also use a lambda instead of requiring an actual class method. Depends on if call method more than once, as well as personal preference based on how easy to debug each, etc.

CallFunc::create([this]() { drive(); });
1 Like

Your the man ? Fancy solving another small issue ?
:slight_smile: