Main content:
Introduction to the features of C + + grammar
Two, the first HelloWorld scene
Third, create a new scene
Introduction to the features of C + + grammar
1. Declaration and definition of functions separate
For example, functions declared in AppDelegate.h are defined in AppDelegate.cpp
TIPS: Press F12 to jump directly to definition
2. #include预编译
When you need to invoke some of the methods in other classes, you must include their header files
For example, this method cannot be called helloworld::scene () if it does not include "HelloWorld.h".
3. Using namespace namespace
In different namespaces, there are different methods, different classes
4.:: Double colon usage
Scope operator
5. Destructors
The function of the destructor and the constructor function, in contrast, in the constructor, often do some initialization of member variables and so on. Some functions that are not needed are processed in the destructor.
6. Pointers
One type: pointer type, directly manipulating memory address.
For example the definition of a director is a pointer
7.: Single Colon
Special representation of inheritance in C + +, common or private methods
Two, the first HelloWorld scene
1 #ifndef __helloworld_scene_h__2 #define__helloworld_scene_h__3 4#include"cocos2d.h"5 6 classHelloWorld: PublicCocos2d::cclayer7 {8 Public:9 //Here ' s a difference. Method ' init ' in cocos2d-x returns BOOL, instead of returning ' ID ' in Cocos2d-iphoneTen Virtual BOOLinit (); One A //there ' s no ' id ' in CPP, so we recommend returning the class instance pointer - Staticcocos2d::ccscene*scene (); - the //a selector callback - voidMenuclosecallback (ccobject*psender); - - //implement the "Static node ()" Method manually + Create_func (HelloWorld); - }; + A #endif //__helloworld_scene_h__
View Code
You can see that the HelloWorld class inherits from Cclayer.
The 3 methods inside represent the initialization, the scene, the selector's callback function, respectively.
The last Create_fun () represents the automatic recovery mechanism, which is automatically reclaimed when HelloWorld is no longer in use.
The above methods are defined in HelloWorldScene.cpp.
Third, create a new scene
MyScene.h:
1 #pragmaOnce2#include"cocos2d.h"3 4 using namespacecocos2d;5 6 classMyScene: PublicCclayer7 {8 Public:9 MyScene ();Ten~MyScene (); One Virtual BOOLinit (); A Staticccscene*scene (); - Create_func (MyScene); -};
View Code
MyScene.cpp:
1#include"MyScene.h"2 3 4 Myscene::myscene ()5 {6 }7 8 9myscene::~MyScene ()Ten { One } A -ccscene*Myscene::scene () - { the //' Scene ' is an Autorelease object -Ccscene *scene =ccscene::create (); - - //' Layer ' is an Autorelease object +MyScene *layer =myscene::create (); - + //add layer as a child to scene AScene->addChild (layer); at - //return the scene - returnscene; - } - - BOOLMyscene::init () in { - return true; to}
View Code
Because the program is started in AppDelegate.cpp.
Therefore, you need to include "MyScene.h" in AppDelegate.cpp.
However, because the classes created are under the project-level directory, and the previous HelloWorld.h and HelloWorld.cpp are under the Classes folder, this requires some tweaking.
Add a level of directory to the properties of the Testgame Project menu drop-down.
Then make a slight change to the code in AppDelegate.cpp:
Next, you'll see a whole new scene:
Cocos2d-x Learning Notes 2--c++ grammar and scenes