Quick-cocos2d-x Study Notes [2] -- project structure analysis, create a new scenario, quickcocos2d

Source: Internet
Author: User

Quick-cocos2d-x Study Notes [2] -- project structure analysis, create a new scenario, quickcocos2d

After creating a new project, we can take a simple look at the file structure of this project, with such a File hierarchy


Several proj. * folder is not required. It is the solution of the corresponding platform. res stores our game resources, scripts stores our lua code, and sources contains the AppDelegate class we are familiar, we usually use two folders, res and scripts (I am not talking nonsense)


Okay, open the scripts folder and check again,

This file contains main. lua, which is the Startup File of The lua script.

Function _ g1_trackback _ (errorMessage) print ("----------------------------------------") print ("lua error :".. tostring (errorMessage ).. "\ n") print (debug. traceback ("", 2) print ("----------------------------------------") endrequire ("app. myApp "). new (): run () -- execute the MyApp script after startup


After startup, execute the MyApp script and call the run function.

require("config")require("framework.init")local MyApp = class("MyApp", cc.mvc.AppBase)function MyApp:ctor()    MyApp.super.ctor(self)endfunction MyApp:run()    CCFileUtils:sharedFileUtils():addSearchPath("res/")    self:enterScene("MainScene")endreturn MyApp

In the run function, first set the search path of the file resource, set it to the res folder, and then enter the first scenario, which is also the MainScene provided by the script for me. lua, before entering the game screen, the program will initialize some things, you can see that the first line of code introduces config. lua, we can continue to open config. lua File

-- 0-No debugging information is output; 1-Basic debugging information is output, 2-output detailed debugging information DEBUG = 1 -- set whether to display rendering frame rate and other information in the screen DEBUG_FPS = true -- set whether to output memory usage information, true: DEBUG_MEM every 10 seconds = false -- whether to load obsolete API definitions LOAD_DEPRECATED_API = false -- whether to load short code apiload_inclucodes_api = true -- CONFIG_SCREEN_ORIENTATION = "landscape" -- Design resolution size CONFIG_SCREEN_WIDTH = 960CONFIG_SCREEN_HEIGHT = 640 -- Auto Scaling: CONFIG_SCREEN_AUTOSCALE = "FIXED_WIDTH"

Here, we mainly configure the debug information, FPS display, screen direction, design screen size, and screen adaptation solution of the game.

With these initialization, we will enter the first scenario. Let's look at MainScene. lua.

local MainScene = class("MainScene", function()    return display.newScene("MainScene")end)function MainScene:ctor()    ui.newTTFLabel({text = "Hello, World", size = 64, align = ui.TEXT_ALIGN_CENTER})        :pos(display.cx, display.cy)        :addTo(self)endfunction MainScene:onEnter()endfunction MainScene:onExit()endreturn MainScene

The ctor of MainScene is a constructor. Once we create an object instance, The ctor is called, so it is a must. In this case, the interface layout of some scenarios is mainly used, in MainScene, the engine only draws one text above, which is the Hello World seen in the previous section.


In quick, it weakens the position of the layer, but enhances the position of the scene, we generally create a new layer in the cocos2d-x, the elements of the game to add to this layer, in quick, we often add them directly to scene for simplicity. So you will see in samples that it is basically added to scene.


Now, let's create a scenario.

Create a new MyScene in the scenes folder, Which is modeled after MainScene. lua,

local MyScene = class("MyScene", function ()return display.newScene("myscene")end)function MyScene:ctor()endreturn MyScene

So even if the new scene, let's add some warm screen, show the classic cocos2d-x screen, in the next note to explain the addition of these genie text.

Copy the helloworld.png image to the res folder in the original cocos2d-xproject.

The complete code is as follows,

local MyScene = class("MyScene", function ()return display.newScene("myscene")end)function MyScene:ctor()display.newSprite("HelloWorld.png", display.cx, display.cy):addTo(self)ui.newTTFLabel({text = "Hello, World", align = ui.TEXT_ALIGN_CENTER, x = display.cx, y = display.height*0.9}):addTo(self)endreturn MyScene

Then we modify the first scenario of startup in MyApp. in lua, modify self: enterScene ("myscene") and remember that the strings in the preceding scenario are the strings entered in the previous scenario. If they are inconsistent, they cannot be found in this scenario. Finally, we can run the player simulator to see the effect.



Haha, it's classic enough. It's warm enough, but the button is not added. Let's talk about it later. Let's try again.

In case of any errors, please criticize and point out.




Related Article

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.