Refer to the "occasionally enet thing" [cocos2d-x entry practice] aircraft war cocos2dx 2.0 version, occasionally enet thing he wrote very detailed, everything, everyone is very necessary to see. You can jump through the following link:
Getting started with cocos2d-x
Here I'm Going To [cocos2d-x entry into practice] aircraft war as the blueprint, with cocos2dx 3.0rc1 version. I will not talk about the installation environment or anything. I can find it on the Internet. I will talk about it directly from the game start interface.
One thing you have to do is to create a helloworld project for cocos2dx3. rc1.
The following is a download of image resources for the aircraft war:
Image Resources
Playing a plane is a sport that requires foreplay. Therefore, it is necessary to add a welcome interface or something to bring up the foreplay atmosphere.
Let's finish the foreplay and what we should do:
1. Game start Interface
I. The first step is to display the welcome page.
Here we have implemented a simple static interface and a cool dynamic graph, although only three seconds !, I am using the resources of the occasional enet event. Forgive me for upgrading your plane to version 3.0. If you are not feeling well, please come and hit me ~ Okay. Hello, jian .....
II. Introduction to the initial project
Assume that you have created a project named "PlayThePlane", your solution will be as follows:
The project is executed from main. cpp:
Int APIENTRY _ tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {timeout (hPrevInstance); // UNREFERENCED_PARAMETER tells the compiler that this variable has been used and no warning is required! UNREFERENCED_PARAMETER (lpCmdLine); // if this parameter is not added, the "warning C4100:" lpCmdLine ": unreferenced formal parameter will appear. "// create the application instance to create the Application instance AppDelegate app; return application: getInstance ()-> run (); // cocos2dx AppDelegate Program officially started to run}Application: getInstance ()-> run () what exactly is running in it? You just need to jump in and see it. It's not a trap. It's a treasure stack. I will only tell you that it calls applicationDidFinishLaunching () in AppDelegate. h. Let's take a look at this time.
Bool AppDelegate: applicationDidFinishLaunching () {// initialize director auto director = Director: getInstance (); // form framework auto glview = director-> getOpenGLView (); if (! Glview) {glview = GLView: createWithRect ("PlayerThePlane", Rect (0, 0,480,800); // form name + wide/High specifications ctor-> setOpenGLView (glview ); // 1. LOOK this function will automatically adapt to the mobile phone screen according to the design width and zoom mode, set the game resolution (design width, design height, zoom mode ). Glview-> setDesignResolutionSize (480,800, kResolutionNoBorder);} // turn on display FPS print frame rate, do not comment out the three rows in the lower left corner or set false ctor-> setDisplayStats (true); // set FPS. the default value is 1.0/60 if you don't call this 60 frames director-> setAnimationInterval (1.0/60); // create a scene. it's an autorelease object creation scenario: auto scene = HelloWorld: createScene (); // run the director to start the scenario to run director-> runWithScene (scene); return true ;}
Here we modify and add the following items:
Glview = GLView: createWithRect ("PlayerThePlane", Rect (0, 0,480,800); we set our airplane name and Space
Glview-> setDesignResolutionSize (480,800, kResolutionNoBorder );
The annotations are clear and I will not explain them again.
Auto scene = HelloWorld: createScene (); this is our initial scenario. auto is a feature of c ++ 11. A good scene can make it impossible to fight the plane, so we need to make the scene shine.
Director-> runWithScene (scene); give the scene to the director for operation.
Iii. Implementation of the game start Interface
Let's take a look at the HelloWorldScene. h code:
# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "class HelloWorld: public cocos2d: Layer {public: // generate a scenario, and then add the Layer class to the Scene to static cocos2d: Scene * createScene (); // Add the sprite element virtual bool init () to the Layer; // callback void menuCloseCallback (cocos2d: Ref * pSender) by the selector callback exit button ); // its implementation is actually HelloWorld: create (). You can see CREATE_FUNC (HelloWorld) in the CREATE_FUNC macro definition; public: void loadingDone (Node * pNode ); // jump from the start interface to the game interface void PreloadMusicAndPicture (); // pre-load music and pictures}; # endif/_ HELLOWORLD_SCENE_H __
Okay, I found that I have commented on it. There is nothing to say. Let's look at the HelloWorldScene. cpp Code directly:
# Include "HelloWorldScene. h "# include" SimpleAudioEngine. h "USING_NS_CC; Scene * HelloWorld: createScene () {// create an auto Release scenario. auto scene = Scene: create (); // create an automatically released layer auto layer = HelloWorld: create (); // Add layer scene-> addChild (layer) to the scenario ); // return scene;} // on "init" you need to initialize your instancebool HelloWorld: init () {// when you want to call the virtual of the parent class, if you want to implement it yourself, write if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director: getInstance () -> getVisibleOrigin (); // create and exit button auto closeItem = MenuItemImage: create ("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1 (HelloWorld: menuCloseCallback, this )); closeItem-> setPosition (Point (origin. x + visibleSize. width-closeItem-> getContentSize (). width/2, origi N. y + closeItem-> getContentSize (). height/2); auto menu = Menu: create (closeItem, NULL); menu-> setPosition (Point: ZERO); this-> addChild (menu, 1 ); /// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label // remove the following code and add your own code // return the Size of the OpenGL view. Size winSize = Director:: getInstance ()-> getWinSize (); // preload the image and music PreloadMusicAndPicture (); // background Figure (genie) auto background = Sprite: createWithSpriteFrame (SpriteFrameCache: getInstance ()-> getSpriteFrameByName ("background.png"); background-> setPosition (Point (winSize. width/2, winSize. height/2); // set the location // Add the background image this-> addChild (background) to the scene; // Add the copyright image (genie) auto copyRight = Sprite :: createWithSpriteFrame (SpriteFrameCache: getInstance ()-> getSpriteFrameByName ("shoot_copyright.png"); copyRight-> setAnch OrPoint (Point (0.5, 0); // stroke copyRight-> setPosition (Point (winSize. width/2, winSize. height/2); this-> addChild (copyRight); // Add the loading image (genie) auto loading = Sprite: createWithSpriteFrame (SpriteFrameCache: getInstance () -> getSpriteFrameByName ("game_loading1.png"); loading-> setPosition (Point (winSize. width/2, winSize. height/2); this-> addChild (loading); // Animation is composed of many sprite frames. You can set the interval and duration, it actually contains a set of data Animati On * animation = Animation: create (); animation-> setDelayPerUnit (0.2f); // interval animation-> addSpriteFrame (SpriteFrameCache: getInstance () -> animation ("game_loading1.png"); animation-> addSpriteFrame (SpriteFrameCache: getInstance ()-> animation ("game_loading2.png"); animation-> addSpriteFrame (Attachment :: getInstance ()-> getSpriteFrameByName ("game_loading3.png"); animation-> addSprit EFrame (SpriteFrameCache: getInstance ()-> getSpriteFrameByName ("game_loading4.png"); // create a frame action through frame data (create a sequence Frame Animation) Animate * animate = Animate :: create (animation); Repeat * repeat = Repeat: create (animate, 3); // number of times that an action is repeated CallFuncN * repeatdone = CallFuncN: create (CC_CALLBACK_1 (HelloWorld :: loadingDone, this); // creates the callback function CC_CALLBACK_1 to represent a parameter Sequence * sequence = Sequence: create (repeat, repeatdone, NULL ); // Let multiple actions be executed one by one in order If the row repeatdone is placed before repeat, the animation loading-> runAction (sequence) that executes three sequence frames will not be played; // execute the above animation this-> setKeypadEnabled (true ); // set buttons for listening to Android, such as the return key, menu key, and Home key. Return true;} void HelloWorld: menuCloseCallback (Ref * pSender) {Director: getInstance ()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0 ); # endif} void HelloWorld: PreloadMusicAndPicture () {// Add png to global cache. plist stores SpriteFrameCache: getInstance ()-> cached ("ui/cached"); SpriteFrameCache:: getInstance ()-> addSpriteFramesWithFile ("ui/shoot. plist "); // sound CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadBackgroundMusic (" sound/background-music1.mp3 "); CocosDenshion: SimpleAudioEngine: getInstance () -> preloadEffect ("sound/bulletask"); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect (" sound/enemyworkflow downgrading "); usage: SimpleAudioEngine: getInstance () -> preloadEffect ("sound/enemy2_downgrading"); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/sound"); cosdenshion: SimpleAudioEngine: getInstance () -> preloadEffect ("sound/game_overfill"); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/get_bombloud"); effect: SimpleAudioEngine: getInstance () -> preloadEffect ("sound/sound"); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/use_bombloud"); CocosDenshion: SimpleAudioEngine: getInstance () -> preloadEffect ("sound/sound"); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/achievementect"); cosdenshion: SimpleAudioEngine: getInstance () -> preloadEffect ("sound/out_porp.mp3"); CocosDenshion: SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/button="); // CocosDenshion: SimpleAudioEngine:: getInstance ()-> playBackgroundMusic ("sound/game_musicloud", true);} void HelloWorld: loadingDone (Node * pNode ){}
Path
The preload path is the Resources folder under the project path.
This is my project resource path: E: \ studyCocos2dx3.0RC1 \ PlayThePlane \ Resources
For example, SpriteFrameCache: getInstance ()-> addSpriteFramesWithFile ("ui/shoot_background.plist ");
It is actually SpriteFrameCache: getInstance ()-> addSpriteFramesWithFile ("E:/studyCocos2dx3.0RC1/PlayThePlane/Resources/ui/shoot_background.plist ");
Image loading
We use the TexturePacker tool to package several images and generate a total png and plist. plist stores all the data in the png image, such as the name size or something. Of course, you can also choose not to integrate the image, so the method of loading the image changes, such as loading the background image:
// Background image (genie) auto background = Sprite: create ("ui/shoot_background/background.png"); background-> setPosition (Point (winSize. width/2, winSize. height/2); // set the location // Add the background image this-> addChild (background) to the scene );
Music Loading
In the pre-load, one of them is not preload, but is directly loaded and enabled:
CocosDenshion: SimpleAudioEngine: getInstance ()-> playBackgroundMusic ("sound/game_musicloud", true); this directly enables background music.
Image animation effects and game start callback
CallFuncN * repeatdone = CallFuncN: create (CC_CALLBACK_1 (HelloWorld: loadingDone, this); // create a callback function CC_CALLBACK_1 representing a parameter Sequence * sequence = Sequence: create (repeat, repeatdone, NULL); // if multiple actions are executed one by one before repeatdone, the animation loading-> runAction (sequence) that executes three sequence frames will not be played ); // execute the above Animation
Sequence * sequence = Sequence: create (repeat, repeatdone, NULL); in my understanding, Sequence stores action queues. The repeat, repeatdone, and NULL actions are executed sequentially. That is to say, the repeat action is executed first (three flights by a plane), and then the repeatdone is executed to trigger the callback function loadingDone (), the beginning of the game is here.
Now, we have finished the interface before the game starts. I don't know what to say next time. Write or say something.
I have read my typography. It is really strange that sometimes the line distance is very close, sometimes it is very far away. And how to set the font? I thought it was a small font at the beginning, instead of writing a paragraph, and then Manually changing it.
If you don't know anything, you can directly ask me (Don't ask too much ~), I also started to learn cocos2dx.