Delegation mechanism in cocos2d-x

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.