[Cocos2D-x 3.5 practice] tank war (2) Game start interface, cocos2d-x3.5

Source: Internet
Author: User

[Cocos2D-x 3.5 practice] tank war (2) Game start interface, cocos2d-x3.5
 

The materials about the game are collected everywhere on the Internet, and then I use the second-stream ps technology to repair, modify, and modify the materials. Therefore, it may be difficult to mix the materials together (there is no way, no artist Orz ..

The project has been created, and we need to put the downloaded materialsResourcesFolder. (After the game is complete, I will upload the material package .. I have not completed searching)

Set screen size:

 

Open the created Project in vs and open the AppDelagate. cpp file.

Add code in AppDelegate: applicationDidFinishLaunching ()

If (! Glview) {glview = GLViewImpl: create ("Battle City"); // The window title glview-> setFrameSize (1280,720); // set the size of the created window here, you do not need to set it on the phone because the phone has a fixed screen director-> setOpenGLView (glview );}

 

In this methoddirector->setAnimationInterval(1.0 / 60);This line of code is used to set the number of frames for the game and is called by the game engine.OpenGLThe time required to draw each graph. The parameter indicatesTime/number of images. Here we do not modify

 

director->setDisplayStats(true);This line of code is used to display Game Information in the lower left corner of your game, such as the number of frames, drawing, and other data. During debugging, the parameter is settrueBut is usually setfalse,

 

Game start background:

 

Open the HelloWorldScene. cpp file.

In the init method, we can see that

// add "HelloWorld" splash screen"auto sprite = Sprite::create("HelloWorld.png");// position the sprite on the center of the screensprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

 

This is the code for creating the background and setting the coordinates of the background (Cocos2d and OpenGL coordinates are the same, the origin is in the lower left corner of the screen, x to the right, and y to the top .)

Here, the concept of genie is used. The following is a Chinese wiki about cocos2d genie.

Cocos2d's genie are similar to those in other game engines. They can be moved, rotated, scaled, animated, and converted. The Sprite of Cocos2dx is composed of Texure, frame, and animation, and is rendered by openes. The main class relationship is as follows: a simple process can be described as: Using Texture2D to load images, you can use Texture2D to generate the corresponding SpriteFrame (SpriteFrame) and add SpriteFrame to Animation to generate Animation data, use Animation to generate an Animation (that is, the final Animation action), and use Sprite to execute this action.

 

Delete and add the original code

// Start background auto startBackground = Sprite: create ("ui/start_background.png"); // set the position of the wizard relative to the screen (absolute Coordinate System) startBackground-> setPosition (Vec2 (visibleSize. width/2 + origin. x, 0); // set the anchorpoint startBackground-> setAnchorPoint (Vec2 (0.5, 0); // set the genie Tag, and then you can find the genie through the Tag, and perform startBackground-> setTag (START_BACKGROUD) on this genie; // Add the background genie to the layer. The second parameter 0 sets the visible priority. The smaller the value, the smaller the priority. If the priority of another genie is larger than that of others, it will be blocked by this-> addChild (startBackground, 0 );

 

Add const int START_BACKGROUD = 101 in helloworldscene. h;

 

Add a game menu:

Declare two public callback methods in the helloworld class in HelloWorldScene. h. This function is triggered when you click the menu.

void menuCloseCallback(cocos2d::Ref* pSender);void menuStartCallback(cocos2d::Ref* pSender);

 

Add the function implementation method to HelloWorldScene. cpp, and use the original close method (the specific function is not written for the time being)

// Start the game and do not write the specific content void HelloWorld: menuStartCallback (Ref * pSender ){}

 

Now you can add a menu in the init method.

// Start Menu. The first two parameters are the image auto startItem = MenuItemImage: create ("ui/game_start.png", "ui/game_start.png ", CC_CALLBACK_1 (HelloWorld: menuStartCallback, this); startItem-> setPosition (Vec2 (origin. x + visibleSize. width/4, origin. y + visibleSize. height/3*2); auto closeItem = MenuItemImage: create ("ui/game_exit.png", "ui/game_exit.png", CC_CALLBACK_1 (HelloWorld: menuCloseCallback, this )); closeItem-> setPosition (Vec2 (origin. x + visibleSize. width/4, origin. y + visibleSize. height/3*2-startItem-> getContentSize (). height-20); // Add the menu item to the Genie. auto Menu = menu: create (closeItem, startItem, NULL ); // Add the menu to the current menu-> setPosition (Vec2: ZERO); this-> addChild (menu, 1 );

 

Run it

Well, the starting interface of the game is basically ready .. Now it's a bit monotonous. Add other functions later ..

Link: http://www.bugcoding.com/entry/8

 

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.