[Wood cocos2d-x] HelloWorld bottom how to play?

Source: Internet
Author: User


Cocos2D-x HelloWorld bottom how to play?
 
After reading the tutorial, you still need to write HelloWorld on your own to remember it. So before writing HelloWorld, you still need to check others' HelloWorld ~
 
(The following CS-X is short for Cocos2D-X)
 
1. Directors
Use VS to create a new CS-X project, and then help us generate a HelloWorld project, and then look at the AppGelegate. app file, which contains this function:
Bool AppDelegate: applicationDidFinishLaunching ()
{
// Initialize director
CCDirector * pDirector = CCDirector: shareddire ();
PDirector-> setOpenGLView (CCEGLView: Export dopenglview ());
 
// Enable High Resource Mode (2x, such as iphone4) and maintains low resource on other devices.
// PDirector-> enableRetinaDisplay (true );
 
// Turn on display FPS
PDirector-> setDisplayStats (true );
 
// Set FPS. the default value is 1.0/60 if you don't call this
PDirector-> setAnimationInterval (1.0/60 );
 
// Create a scene. it's an autorelease object
CCScene * pScene = HelloWorld: scene ();
 
// Run
PDirector-> runWithScene (pScene );
Return true;
}
Because I am a newbie, I can only understand it on the surface.
 
CCDirector class, is the director class, in the CS-X, to build a game, is to build a world (in fact, I think all the programs are building the world ~ The world is guided and interpreted by the director class, so when the first step of the game is started, the director tells the program:
PDirector-> setDisplayStats (true); // you can view the game efficiency by displaying the FPS value.
PDirector-> setAnimationInterval (1.0/60); // The number of frames the game plays per second (60 frames here)
PDirector-> runWithScene (pScene); // the scenario to be displayed first when you enter the game.
 
As a result, the code is very simple. It is important to note that a class inherits CCScene and can be used as a scenario. Then, the first scenario of the game is run using the runWithScene method of the director class, however, the replaceScene method will be used for switching scenarios in the future.
 
2. Custom scenarios
Now let's take a look at the custom scenario class. Let's first look at the header file HelloWorldScene. h, which is very short and looks comfortable ~
Class HelloWorld: public cocos2d: CCLayer
{
Public:
Virtual bool init ();
Static cocos2d: CCScene * scene ();
 
Void menuCloseCallback (CCObject * pSender );
 
CREATE_FUNC (HelloWorld );
};
I think the menuCloseCallback function can be ignored for the moment. If you forget it, you can't help but handle it. It is a callback function, no more. It is the same as the onClick function in the OnClickListener of Android, but it is a little different. It is bad. I will not explain it, it is messy, this is not the point ~!
 
Scene functions are indispensable. They are CCLayer classes and must be rewritten in HelloWorld. Where will the scene function be used? Remember the previous code? When creating a scenario class:
// Create a scene. it's an autorelease object
CCScene * pScene = HelloWorld: scene ();
 
// Run
PDirector-> runWithScene (pScene );
Okay, that's it.
 
Let's take a look at CREATE_FUNC (HelloWorld). I have been entangled for a long time. CREATE_FUNC is a macro (it should be, I only stay in the primary syntax for C ++ ):
# Define CREATE_FUNC (_ TYPE __)\
Static _ TYPE _ * create ()\
{\
_ TYPE _ * pRet = new _ TYPE __();\
If (pRet & pRet-> init ())\
{\
PRet-> autorelease ();\
Return pRet ;\
}\
Else \
{\
Delete pRet ;\
PRet = NULL ;\
Return NULL ;\
}\
}
CREATE_FUNC is actually the create FUNCTION. To be precise, it is to rewrite the create function in CCLayer. Let's look at the create function of CCLayer:
CCLayer * CCLayer: create ()
{
CCLayer * pRet = new CCLayer ();
If (pRet & pRet-> init ())
{
PRet-> autorelease ();
Return pRet;
}
Else
{
CC_SAFE_DELETE (pRet );
Return NULL;
}
}
Yes. The function of CREATE_FUNC is only to change the return value of create to inherit the actual subclass of CCLayer (a bit dizzy, take a closer look at the logic ~)
 
Okay, I suppose we have understood the role of CREATE_FUNC. No ~! Not yet ~! Look
How to rewrite the scene function in HelloWorldScene. cpp:
CCScene * HelloWorld: scene ()
{
CCScene * scene = NULL;
Do
{
// 'Scene 'is an autorelease object
Scene = CCScene: create ();
CC_BREAK_IF (! Scene );
 
// 'Player' is an autorelisted object
HelloWorld * layer = HelloWorld: create ();
CC_BREAK_IF (! Layer );
 
// Add layer as a child to scene
Scene-> addChild (layer );
} While (0 );
 
// Return the scene
Return scene;
}
 
So we found that although HelloWorld claimed to be a scene class, the director didn't want it ~! The Director still wants a real CCScene class. If you don't believe it, see:
Scene = CCScene: create ();
A real CCScene class is created here.
 
Not enough. Check again:
HelloWorld * layer = HelloWorld: create ();
Scene-> addChild (layer );
HelloWorld is only used as a layer. It is added to the CCScene class just created. Well, the reality is cruel. Despite the basic scenario class of HelloWorld, it is still not a real scenario ~!
 
Okay. Then, don't relax. Do you remember the words in the create FUNCTION?
If (pRet & pRet-> init ())
{
PRet-> autorelease ();
Return pRet;
}
 
OK, I know what you remember, so do you remember it after just two sentences?
HelloWorld * layer = HelloWorld: create ();
Scene-> addChild (layer );
 
Yes, that is, the init function of the scenario class will be called.
Therefore, this indicates that the CCScene class must be inherited by these three guys:
Public:
Virtual bool init ();
Static cocos2d: CCScene * scene ();
CREATE_FUNC (HelloWorld );
};
 
3. The final conclusion is, of course, a summary.
I am confused, and I am also confused. I can't forgive myself for my ability to express myself.
1) The HelloWorld project mainly teaches us how to create a scenario class.
2). AppDelegate. cpp will create the first displayed scene class. The method used at this time is runWithScene.
3) to create a new scenario class, you must have the following three functions:
Virtual bool init (); // initialization, where you can create an genie

Static cocos2d: CCScene * scene (); // create a CCScene class here, and add the custom scenario class to the CCScene class as a CCLayer.

CREATE_FUNC (HelloWorld); // It is equivalent to rewriting the create FUNCTION, so that the create function returns the subclass object.
4. Then, that's it.
 
Well, the above is what I can find from the HelloWorld project. There will certainly be a lot of wrong ideas, but I still cannot correct them. So, it is for reference only ~

Related Article

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.