Development of a flying and shooting mobile game similar to thunder fighter-the entrance of the game, thunder fighter
Game portal AppDelegate
After the game is started, the class AppDelegate is first instantiated. In this class, we need to modify two functions: applicationDidFinishLaunching and applicationDidEnterBackground.
ApplicationDidFinishLaunching () is introduced first. After the game is started, this method is first entered. Here, we can set the game resolution, frame rate, and third-party SDK initialization, and the first scenario.
1 bool AppDelegate: applicationDidFinishLaunching () 2 {3 auto director ctor = Director: getInstance (); 4 auto glview = director-> getOpenGLView (); 5 if (! Glview) {6 // defines the size of the window running in Windows 7 glview = GLViewImpl: createWithRect ("Raiden", 8 Rect (0, 0, CONSTANT: DESIGN_RES_WIDTH * CONSTANT:: RES_RATIO, CONSTANT: DESIGN_RES_HEIGHT * CONSTANT: RES_RATIO); 9 director-> setOpenGLView (glview ); 10} 11 12 // select different scaling modes for the horizontal and vertical screens 13 auto frameSize = glview-> getFrameSize (); 14 if (frameSize. width> = frameSize. height) 15 {16 ctor-> getOpenGLView ()-> setDesignResolutionSize (CONSTANT: DESIGN_RES_WIDTH, CONSTANT: DESIGN_RES_HEIGHT, ResolutionPolicy: SHOW_ALL ); 17} 18 else19 {20 ctor-> getOpenGLView ()-> setDesignResolutionSize (CONSTANT: DESIGN_RES_WIDTH, CONSTANT: DESIGN_RES_HEIGHT, ResolutionPolicy: EXACT_FIT ); 21} 22 23 // set the frame rate to 60 frames/second 24 ctor-> setAnimationInterval (1.0f/60); 25 // set the search path 26 FileUtils: getInstance () -> addSearchPath ("res"); 27 28 // create the first scenario to enter 29 auto scene = LoadingLayer: scene (); 30 31 // enter scenario 32 ctor-> runWithScene (scene); 33 // initialize a third-party SDK34 BmobSDKInit: initialize ("app_id", "app_key"); 35 36 return true; 37}
Pay attention to the following points:
Create a Windows window
glview = GLViewImpl::createWithRect("Raiden", Rect(0, 0, CONSTANT::DESIGN_RES_WIDTH * CONSTANT::RES_RATIO, CONSTANT::DESIGN_RES_HEIGHT * CONSTANT::RES_RATIO));
This is to create a window for running in windows. For debugging convenience, we will compile and debug the game in windows first, and then compile the android version after the debugging in windows is complete. Rect (0, 0, CONSTANT: DESIGN_RES_WIDTH * CONSTANT: RES_RATIO, CONSTANT: DESIGN_RES_HEIGHT * CONSTANT: RES_RATIO) defines the window size. The definitions of several macros are as follows:
Const float DESIGN_RES_WIDTH = 540; // const float DESIGN_RES_HEIGHT = 960; // const float RES_RATIO = 0.75f;
Because my notebook is smaller than 960 in height, RES_RATIO is used here for the same proportion reduction. If you do not narrow down the window here, the game window will not be displayed completely, and more seriously, the problem of inaccurate positioning by clicking the mouse will occur.
Scaling Mode
We use the setDesignResolutionSize function to set the scaling mode. The ResolutionPolicy value range is as follows:
- ResolutionPolicy: SHOW_ALL screen width, height, design resolution width, and height calculate the zoom factor. Smaller ones are used as the zoom factor for width and height. Ensures that all design areas are displayed on the screen, but black edges may exist.
- ResolutionPolicy: EXACT_FIT the screen width to design width ratio is used as the zoom factor in the X direction, and the screen height and design height ratio are used as the zoom factor in the Y direction. Ensures that the design area is fully covered with the screen, but image stretching may occur.
- ResolutionPolicy: NO_BORDER screen width, height, design resolution width, and height calculate the zoom factor. Larger ones are used as the zoom factor for width and height. This ensures that the design area can always be filled with screens in one direction, while the other is usually larger than the screen area.
- ResolutionPolicy: FIXED_HEIGHT and ResolutionPolicy: FIXED_WIDTH both pass in the design resolution in the Internal correction to ensure that the screen resolution to the design resolution is not stretched to fill the screen.
In this program, we know whether the screen is a horizontal screen or a vertical screen based on the width and height of the screen. If it is a portrait screen, use EXACT_FIT to ensure full screen mode without black borders; if it is a landscape screen, use SHOW_ALL to prevent scaling and deformation of the game interface.
Processing logic for Game Transfer to backend
When a user receives a call or presses the home key to bring the game to the background, the applicationDidEnterBackground () function is triggered.
Void AppDelegate: applicationDidEnterBackground () {Director: getInstance ()-> stopAnimation (); // determines whether the current game is in progress. If yes, this causes the game to pause PlaneLayer * pause = dynamic_cast <PlaneLayer *> (GameLayer: Pause (); HubLayer * pHubLayer = dynamic_cast <HubLayer *> (GameLayer: getHubLayer ()); if (pHubLayer & pPlaneLayer &&! PPlaneLayer-> isPause () {pHubLayer-> menuPauseCallback (nullptr );}}
Here we only need to know that we have paused the game, that is, menuPauseCallback. At this time, the game will pop up the pause scenario. Some classes and methods in the above functions will be discussed later.
Download source code
Next, we will go to the LoadingLayer class to explore the game resource Loading Mechanism