I encountered a problem in today's project.
A is a layer with an action in it. Once a is initialized, this action will be executed.
B is a scenario. I added A to B, but after the action in a is executed, B immediately executes another method.
The problem arises. How can I know in B that the action execution in a is complete, and with the help of my colleagues, I will soon be OK. The principle is to use the delegation mechanism.
Solution:
1. Create a new class C with a virtual function.
class C{public: virtual void animFinished(){}; };
2. Let B inherit C.
Class B: Public c {public: Virtual void animfinished (); // If action in a is complete, B will execute the following method void mdonext ();};
In implementation
Adding a is a member variable added to B, which has been initialized.
Then pass B to. (A must have a member variable C)
a->delegate_=this;void B::animFinished(){ mDoNext();}
3. Implementation of Class
class A:public CCLayer{public: C* delegate_;};
In implementation, after an action in a is executed, we only need to start the method in B in the Action callback function.
void A::mCallBack(){ if (delegate_) { delegate_->animFinished(); } }
OK, that is, B is delegated to a, let a start the method in B. I figured it out.