Use cocos2d-x v3.1 to develop mini games (Basic Framework)

Source: Internet
Author: User
Composition of games
  • Welcome Page

You need to load the game resources before they are fully loaded, to avoid a black screen time when entering the game.

It can be used to display the game name or developer logo.

  • Start menu interface

It is generally used to display the game name and level selection (or game difficulty selection ).

You can also add some set functions, such as sound switches and help portals.

If there are many game settings, you can use the settings as a separate interface and provide an entry on the Start Menu.

Some games use pop-up menus.

  • Main game interface

The central part of the game, such as the lattice sliding interface of the 2048 game, the clearance interface of the game, and the snake moving interface of the Snake game.

A game can also be regarded as a game with this interface, but no other interface will make the game lack more choices.

The main logic of the game is to complete this interface.

  • Game End Interface

There is usually a score at the end of the game, which is used to show the comparison between the score of this game and the highest score in history.

You can add a play button on this interface to allow players to play it again.

Most games are displayed in a pop-up window.

  • Ranking page (optional)

If the game has scores, it will have rankings.

It can be a standalone ranking or an online ranking (the server needs to store data ).

Rankings can promote players to get higher scores (which also has some disadvantages. If you see that other players play too well, you give up ).

Use of cocos2d-x v3.1 Lua framework
  • Lua code structure
SRC ── Conf. lua # configuration file, some unchanged configurations are saved here ── gameoverscene. lua # Game end interface ── gamescene. lua # helloscene, the main logic interface of the game. lua # Welcome Page. Here we will perform resource update detection ── main. lua # entry to the Lua script for games-mainmenuscene. lua # rankscene, game main menu. lua # ranking page ── util. lua # General function implementation

 

Each xxooscene. Lua file looks like this:
local XXOOScene = {}XXOOScene.newScene = function ()    local scene = cc.Scene:create()    -- do other XXOO things    return sceneendreturn XXOOScene

 

For scene interface jump, a common function is encapsulated in util for interface jump.
Function util. toscene (scene) -- adds a unified UI jump animation scene = cc. transitionslideinr: Create (0.5, scene) If CC. director: getinstance (): getrunningscene () Then CC. director: getinstance (): replacescene (scene) else CC. director: getinstance (): runwithscene (scene) endend

 

Main. Lua user enters the helloscene Interface
  • HelloScene = require("src/HelloScene") local function main()    cc.FileUtils:getInstance():addSearchResolutionsOrder("src")    cc.FileUtils:getInstance():addSearchResolutionsOrder("res")     HelloScene.newScene()end  local status, msg = xpcall(main, __G__TRACKBACK__)if not status then    error(msg)end

     

    Special scene processing for game startup logo

It takes time to load Lua resources. When all resources are loaded, a black screen is displayed when the game startup interface is created. It takes 5 seconds for me to test on a 1g Android machine to access the scene created by Lua.

Solution: Create a startup screen in C ++. Do not create scene in helloscene. Lua, and directly getrunningscene.

  • Appdelegate: applicationdidfinishlaunching (){//... other xxoo things auto scene = scene: Create (); Auto S = Director: getinstance ()-> getwinsize (); Auto layer = layercolor :: create (color4b (255,255,255,255), S. width, S. height); Auto logo = sprite: Create ("Res/logo.png"); layer-> addchild (logo); Logo-> setposition (vec2 (S. width * 0.5, S. height * 0.7); scene-> addchild (layer, 0); ctor-> runwithscene (scene); // special processing, delayed loading of Lua (logoscene created and loaded) auto action = callfunc: Create (startlua); layer-> runaction (action); Return true ;}

     

    Simplified cocos2d-x v3.1 Engine

Cause: after the engine is compiled, it is found that the Lib file is much larger than the 2.x version. Because it is a small game, it will generally not be useful in the engine part, than I did not use the UI editor, so I just want to find a way not to compile the UI editor part.

Steps:

  • Find the MK file: $ find.-Name "android. mk"

  • Modify the MK file and comment out the statements related to the UI editor.

  • Compile and find out the code to be annotated in the compilation error prompt. My major problems are CCB. You need to comment out the CCB code in lua_cocos2dx_extension_manual.cpp.

  • Ccluastack. cpp also needs to comment out several registration functions.

I also simplified the physical engine and used it at the beginning, but I also killed it when I saw a small game I couldn't use (about 1 m ).

  • File generation under Cocos/scripting/LUA-bindings/auto/

After reading README, run the generated command and encounter some problems:

  • Question 1: only the 64-bit libclang. So file of Ubuntu is officially provided. The following error will be reported when running on the 32-bit host.
  • Libclangerror: libclang. So: wrong Elf class: elfclass64. to provide a path to libclang use config. set_library_path () or config. set_library_file ().

  • Solution: download version 4.3 llvm and clang from the llvm official website. Compile libclang. so.3.4. Copy to bindings-Generator/libclang/to overwrite the libclang. So file. The blog compiled by Alibaba Cloud cannot be found now. The steps are as follows:

  • Clang-3.4.src.tar.gz and llvm-3.4.src.tar.gz. Decompress and copy the clang directory to the llvm-3.4/tools. Compile, create a build folder under the same directory of the llvm-3.4, go to build, execute cmake ../llvm-3.4/cmakelists.txt

The text description may be unclear. The directory structure is as follows:

 

. -> build| ->llvm-3.4  -> tools -> clang -> CMakeLists.txt             |-> CMakeLists.txt

 

  • Problem 2: the header file cannot be found, and the error "unkown type name" is reported because the android_headers path officially configured is different from that on my computer, mainly the GCC version, and my version is 4.8, the official configuration is 4.7. As follows:
  • Details = "unknown type name' _ locale_t '"

  • Solution: change the value of android_headers in cocos2dx. ini to/cxx-STL/gnu-libstdc ++/4.7/cxx-STL/gnu-libstdc ++/4.8 /. There are two places to modify. This problem depends on the configuration of your android ndk. Maybe you still have 4.6. You can find your ndk version in the corresponding path of the ndk.

National chrysanthemum burst

APP treasure: http://android.myapp.com/myapp/detail.htm? Apkname = com. hanxi. runtodie

Baidu mobile assistant? Docid = 6678809

Pea pod: http://www.wandoujia.com/apps/com.hanxi.runtodie

Address: https://github.com/hanxi/cocos2d-x-v3.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.