Working with actions in CCSequence using CCCallBlock

hello,
I am invoking some touch events in my cocos2d-x function , when user touch the screen at some points, the the function CCFadeTO called with one sprite in screen, but after 1 second of time sprite must have to be removed from the parent,
I have cocos2d code here below,

   [sprite runAction:[CCSequence actions:[CCFadeTo actionWithDuration:1.0f opacity:30],[CCCallBlock actionWithBlock:^{ 
            [sprite removeFromParent]; 
                                 }], nil]];

How can i do it in c++?
Thanks…

If you use v3.0, you can do it like this

sprite->runAction(Sequence::create(FadeTo::create(1.0f, 30), 
                                                        CallFunc::create([&sprite]() { sprite->removeFromParent() } ), 
                                                        nullptr 
                            ));

Because on v3.x, we use c++ 11 which supports lambda expression. More about c++ lambda expression please refer to this doc.

Thank you…
But i am using v2.2.3, how can i achieve that in this version, is there any way?

You should modify CallFunc as v3.0 did and enable c++11.

Thank you, i got it,
I will give my first try to use v3.0.

@zhangxm
Edit: its working now I just have to remove & in [&mySprite](){}

Please help me. I have spent a lot of time to get solution for this problem. I’m getting run time error when I use lambda Sequence::create(moveToAction, jumpToAction, Repeat::create(rotateByAction, 2), shakeSequenceAction, CallFunc::create( [&treeSprite]() { treeSprite->setAnchorPoint(Point::ANCHOR_MIDDLE_BOTTOM); } ), nullptr);

I’m using Microsoft Visual Studio Express 2012.
When I click run everything compiled without any errors or warnings. Then the animation that I made played in a sequence but when it comes to CallFunc everything gets broken.
So, I have attached my Classes and Resources folder of my project below.
Call Stack

>	cocosTransAction.exe!<lambda_d946d50cf150b6f2a06611b1bfc025c1>::operator()() Line 78	C++
 	cocosTransAction.exe!std::_Callable_obj<<lambda_d946d50cf150b6f2a06611b1bfc025c1>,0>::_ApplyX<void>() Line 431	C++
 	cocosTransAction.exe!std::_Func_impl<std::_Callable_obj<<lambda_d946d50cf150b6f2a06611b1bfc025c1>,0>,std::allocator<std::_Func_class<void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> >,void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::_Do_call() Line 239	C++
 	cocosTransAction.exe!std::_Func_class<void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::operator()() Line 514	C++
 	cocosTransAction.exe!cocos2d::CallFunc::execute() Line 409	C++
 	cocosTransAction.exe!cocos2d::CallFunc::update(float time) Line 400	C++
 	cocosTransAction.exe!cocos2d::Sequence::update(float t) Line 351	C++
 	cocosTransAction.exe!cocos2d::ActionInterval::step(float dt) Line 124	C++
 	cocosTransAction.exe!cocos2d::ActionManager::update(float dt) Line 361	C++
 	cocosTransAction.exe!<lambda_1a2251fe36a4365aacfd6988cf1fc601>::operator()(float dt) Line 248	C++
 	cocosTransAction.exe!std::_Callable_obj<<lambda_1a2251fe36a4365aacfd6988cf1fc601>,0>::_ApplyX<void,float>(float && _V0) Line 431	C++
 	cocosTransAction.exe!std::_Func_impl<std::_Callable_obj<<lambda_1a2251fe36a4365aacfd6988cf1fc601>,0>,std::allocator<std::_Func_class<void,float,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> >,void,float,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::_Do_call(float && _V0) Line 239	C++
 	cocosTransAction.exe!std::_Func_class<void,float,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::operator()(float _Vx0) Line 515	C++
 	cocosTransAction.exe!cocos2d::Scheduler::update(float dt) Line 838	C++
 	cocosTransAction.exe!cocos2d::Director::drawScene() Line 275	C++
 	cocosTransAction.exe!cocos2d::DisplayLinkDirector::mainLoop() Line 1101	C++
 	cocosTransAction.exe!cocos2d::Application::run() Line 92	C++
 	cocosTransAction.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 17	C++
 	cocosTransAction.exe!__tmainCRTStartup() Line 528	C
 	cocosTransAction.exe!wWinMainCRTStartup() Line 377	C
 	kernel32.dll!7699919f()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	ntdll.dll!771aa8cb()	Unknown
 	ntdll.dll!771aa8a1()	Unknown

Sorry.
Its my fault. Can not use reference here, because the parameter being referenced is destroyed when it is used in CallFunc. So you should use it like this

CallFunc::create([treeSprite]() { ... })   // capture by value, not by reference

Hello zhangxm

What is wrong with this code please

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

Where drive() is a fucntion in this class

Here are the two functions I am using in my Vehicle Class

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));
}

@GaddMaster what’s your issue? I don’t quite understand.

1 Like

sprite->runAction(CCSequence::create(cocos2d::repeat::create(animate, 1), Callfunc::create(CC_CALLBACK_0(Vehicle::Drive,this)), NULL));

1 Like