Cocos2d-x 3.2-2048-Article 1

Source: Internet
Author: User

2048 development cocos2d-x 3.2 win7 + vs2012 tutorial

* *********************************** Please specify the source: bytes ******************************************



2048 this game has been around for a while and has always wanted to be a game.

However, the cocos2d-x engine is learning, and there are other things busy, until now.

Okay, I have to do it now ~.~


This is the first article, which mainly describes:

-- Screen Adaptation

-- Main Interface layout and design


In the preface, I have already shown 2048,

The content of this tutorial will show that the most basic 2048 production,

Scenario, button, and Shenma, add as you like ~

O, and, my environment is win7 + vs2012 + cocos2d-x 3.2

At the end of each article, the code for the current chapter will be loaded,

I also wrote a little, and I wrote one,

If any error occurs, please note that O (∩ _ ∩) O thank you!



Start, go!


Step 1: screen adaptation

Previously, in the article on screen adaptation,

You can use 320*480 or 480*800,

Here, the display is convenient, and 320*480 is used,

In appdelegate. cpp:

 if(!glview) {        glview = GLView::create("My Game");        director->setOpenGLView(glview);glview -> setFrameSize( 320 , 480 );    }glview -> setDesignResolutionSize(320,480,ResolutionPolicy::EXACT_FIT);

Well, the adaptation problem is like this,

Let's talk about the structure of this game:

-- Three game interfaces (main interface, game interface, and game end interface)

-- A macro definition class (mainly including the macro definition of game content)

-- A digital block class (that is, the number we need to slide)


Yes, only these classes are available ~.~

Does it make your mind clearer?


First, let's look at the main interface,

The original helloworldscene was removed directly,

Right-click Project-> Add-> new item, enter the class name, and select a classes folder.



Add the mainscene. h and. cpp files,

Add the following content in mainscene. h:

# Ifndef _ test2048_mainscene_h __# DEFINE _ test2048_mainscene_h __# include "cocos2d. H "using_ns_cc; Class mainscene: Public layer {public: // create scenario static scene * createscene (); // initialize the function bool Init (); create_func (mainscene );}; # endif

Then, in. cpp, set the function defined by. h:

# Include "mainscene. H "using_ns_cc; // create scene * mainscene: createscene () {auto scene = scene: Create (); Auto layer = mainscene: Create (); scene-> addchild (layer); Return scene;} // initialization function bool mainscene: Init () {// if the parent class fails to be initialized, falseif (! Layer: Init () {return false;} return true ;}

Then, add some related content in this scenario,

For example: Game name, start game button, exit game button


Before setting these things, you must first set some macro definitions related to games,

For example, the screen width and height,

Create a gamedefine. h:

#ifndef __test2048_GameDefine_H__#define __test2048_GameDefine_H__#define Game_Screen_Width 320#define Game_Screen_Height 480#endif

This class stores some macro definitions of games,

The back is also, sliding up, down, left, right (Enumeration type ),

Set several rows and several columns and the length of each column in each row.


Of course, if this macro is not defined, the screen size can also be called through the function:

Size visibleSize = Director::getInstance()->getVisibleSize();


To get down to business, add some buttons, which can be image buttons or text buttons,

Start the game with a text button,

Exit with the image button.


Add the game name. I used the font file (. TTF) found on the Internet to create it,

Load TTF first, and then use label: createwithttf to create:

// Load the TTF file and set the ttfconfig ("hellokitty. TTF ", 60); // display the game name auto labelgame = label: createwithttf (config," 2048 "); labelgame-> setposition (point (game_screen_width/2, game_screen_height * 2/3); this-> addchild (labelgame); labelgame-> setscale (1.5 );

Then, add the exit button, and use the original image, which is placed in the lower right corner:

auto closeItem = MenuItemImage::create(                                           "CloseNormal.png",                                           "CloseSelected.png",                                           CC_CALLBACK_1(MainScene::menuCloseCallback, this));    closeItem->setPosition(Point( GAME_SCREEN_WIDTH - closeItem->getContentSize().width/2 , closeItem->getContentSize().height/2 ) );    auto menu = Menu::create(closeItem, NULL);    menu->setPosition(Vec2::ZERO);    this->addChild(menu);

Of course, you need to add the callback function of THiS button in the. h file:

void menuCloseCallback( Ref* pSender );
Then, add the following in. cpp:

void MainScene::menuCloseCallback( Ref * pSender ){Director::getInstance()->end();}


PS: Let's explain why we set this location,

We need to put the close button in the lower right corner, and the lower left corner of the screen is,

The image's anchor is at the 0.5, 0.5, and central locations,

Getcontentsize is used to obtain the size of the current object,

What should I do with soy sauce? ~



In this way, you can run the command to check the effect,

Before running, add mainscene. h to appdelegate. cpp and change the running scenario to mainscene:

// create a scene. it's an autorelease objectauto scene = MainScene::createScene();    // run    director->runWithScene(scene);

The effect is as follows:



Then, add the text button for starting the game:

auto startItem = MenuItemFont::create(" Start ",CC_CALLBACK_1(MainScene::menuStartCallback,this));startItem -> setPosition( Point( GAME_SCREEN_WIDTH/2,GAME_SCREEN_HEIGHT/2));auto menu = Menu::create(closeItem,startItem, NULL);    menu->setPosition(Vec2::ZERO);    this->addChild(menu);

Then, follow the previous method to create a class, gamescene,

. H and. cpp are still the same.

Add the start button callback,

Void mainscene: menustartcallback (ref * psender) {auto scene = gamescene: createscene (); // Add an animation director: getinstance ()-> replacescene (transitionfadedown :: create (0.5f, scene ));}

Come on, run it:



The following is a bit blank. You can add anything, such as your information.


The first article is here first,

The second article mainly describes how to plot the lattice of the game interface ~

Download this Code:


* *********************************** Please specify the source: bytes ******************************************

Cocos2d-x 3.2-2048-Article 1

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.