How to add global variables to cocos2d-x Projects

Source: Internet
Author: User

A recent study of how to automatically run a cocos2d-x test project requires a custom global pointer to be added to the testCpp project to point to the currently running test example. Where should we put this global pointer? After studying it, we found that it is best to put it in AppDelegate. In fact, this design was transplanted from iOS and will be very familiar with kids shoes that have been developed for iOS. The Code is as follows:


Class AppDelegate: private cocos2d: Application
{
Public:
AppDelegate ();
Virtual ~ AppDelegate ();


Virtual bool applicationDidFinishLaunching ();
Virtual void applicationDidEnterBackground ();
Virtual void applicationWillEnterForeground ();


BaseTest * getCurrentTest ();
Void setCurrentTest (BaseTest * curTest );
Private:
BaseTest * _ curTest;

};


Of course, this global pointer cannot be directly exposed and accessed using getter/setter.

So how can we call it elsewhere in the program? It is also very simple:

Void BaseTest: onEnter ()
{
Layer: onEnter ();


AppDelegate * app = (AppDelegate *) Application: getInstance ();
App-> setCurrentTest (this );

....

}

GetInstance () is a cocos2d: static function of the Application class, which can be used directly.

Here we use the C ++ Singleton mode.





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.