Quick Cocos2dx-lua (V3.3R1) Learning Notes (iii)----don't always show Hello world, we show something else

Source: Internet
Author: User
Tags addchild

OK, the project has been built, run a look at the effect, even cocos2dx so classic background map is gone, no, we have to modify them.

To our Create Project path view

Four folders plus a JSON file

The second Res folder is where we put our resources.

The fourth SCR folder is where our LUA scripts are stored

Let's see what's inside this configuration json file.

It's really the configuration information of our simulator (are you this he is not nonsense,-o-!!! )。

Since we want to display the classic COCOS2DX alien background map, go to the Cocos2dx folder to steal a picture into our res folder (Bah, are home, how can be stolen word it!). )

Now modify the script to achieve the effect we want. Start......

We open the project folder with sublime text (please ignore unregistered information -0-)

See our Res folder with this picture below

I'm going into SRC to see how this structure is

Src/app folder Here's where we're going to write the Lua script.

The Cocos folder is the Lua file for the engine.

The framework folder is where the Quick package Cocos2dx Lua API resides.

There are Mian.lua and Config.lua files under SCR

Open Config.lua

--0-disable Debug Info, 1-less Debug Info, 2-verbose Debug Info--Whether the debug message is displayed debug = The display FPS stats on scree N  --whether to display FPS information debug_fps = true--Dump memory info every seconds  --Output DEBUG memory information per 10s Debug_mem = false--load dep recated API     --whether to import deprecated Apiload_deprecated_api = false--load shortcodes API    --whether to import simple code APILOAD_SHORTCODES_API = true--screen Orientation    --Orientation config_screen_orientation = "Portrait"--   design resolution--designed resolution CONFIG_ Screen_width  = 640config_screen_height = 960--Auto Scale mode      --Adaptive mode Config_screen_autoscale = "Fixed_width"

  

Here is the configuration information (I said nonsense, but do not say, the context to undertake .... Woo-hoo, let me finish. Whoo-hoo. Ah, don't hit the face. )

This Main.lua is equivalent to the main.cpp file we wrote in the Win32 folder of C + + COCOS2DX, look at

Require ("app. MyApp "). New (): Run ()

  

The Myapp.lua file under the app is running

We open this file.

Require ("config") require ("Cocos.init") require ("Framework.init") Local MYAPP = Class ("MyApp", Cc.mvc.AppBase) function Myapp:ctor ()    MyApp.super.ctor (self) endfunction myapp:run ()    cc. Fileutils:getinstance (): Addsearchpath ("res/")    self:enterscene ("Mainscene") Endreturn MyApp

  


Let's just look at the run function, and he's entering the mainscene scene.

We open the Mainscene.lua file under the Scenes folder

Local mainscene = Class ("Mainscene", function ()    return Display.newscene ("Mainscene") end) function Mainscene:ctor ( )    cc.ui.UILabel.new ({            uilabeltype = 2, Text = "Hello, world", size = up})        : Align (display. CENTER, Display.cx, display.cy)        : AddTo (self) endfunction mainscene:onenter () endfunction mainscene:onexit () Endreturn Mainscene

  


OK, see Hello,world words, this is the main modification of the place

Before we make a change, we'll analyze this short code.

Note: Class This function Lua is not, is the engine provides, in the \quick-3.3rc1\quick\framework\functions.lua inside has the prototype, has the interest to go to see oneself

When defining your own class, use the local modifier so that the global variable table is not polluted.

We look at the ctor function, which is the constructor of our Mainscene class, and the function is similar to the INIT function of C + + COCOS2DX, where we initialize the Sprite, UI, and so on.

We first analyzed the first cc.ui.UILabel.new function, the others we are easy to understand, but this uilabeltype=2 in 2 what type, we still see this function prototype, file in G:\quick-3.3rc1\quick\ Framework\cc\ui\uilabel.lua

Local Uilabeluilabel = Class ("UILabel", function (options) if not options thenreturnendif 1 = = options. Uilabeltype thenreturn uilabel.newbmfontlabel_ (options) ElseIf not options. Uilabeltype or 2 = = options. Uilabeltype thenreturn uilabel.newttflabel_ (options) elseprintinfo ("UILabel unkonw uilabeltype") endend) uilabel.label_type_bm= 1uilabel.label_type_ttf = 2

  


Originally this is the TTF way to create a label (so see do not understand, it is best to jump to define where to look, it is clear what this magic number meaning, recommended sublime text add a Quickxdev this plugin, can help you go to function definition)

The Align function is where the label is set, the first parameter is the 23rd parameter of the anchor point is X, Y. In C + + COCOS2DX This we're going to write two lines, here we just have one line OK

The AddTo function is to add the current node as a sub-node to the target.

You can change it into

function Mainscene:ctor () Local label =  cc.ui.UILabel.new ({uilabeltype = 2, Text = "Hello, world", size = up})    Labe L:align (display. CENTER, Display.cx, display.cy)    self:addchild (label) end

  

Switch to Simulator, F5 the simulator, we see the effect is the same


For a long while, haven't done our business, show the background map, and change the text to other

The following is the modified ctor function

function Mainscene:ctor () Local label =  cc.ui.UILabel.new ({uilabeltype = 2, Text = "Hi, yiye3376", size =.)    Labe L:align (display. CENTER, Display.cx, display.height-100)    self:addchild (label)    display.newsprite ("Hello.png", Display.cx, display.cy)    : AddTo (self) End

  

After writing, save

What is display? The display module encapsulates most of the functionality associated with displays and is responsible for calculating the design resolution of the screen based on the resolution defined in the Config.lua.

More functions use the method to view this file.

After the framework is initialized, the display module provides the following properties:-   display.sizeinpixels.width,-   display.sizeInPixels.height Screen pixel resolution-   display.widthinpixels,-   display.heightinpixels Screen pixel resolution-   display.contentscalefactor content scaling factor-   display.size.width,-   display.size.height Screen Design resolution-   display.width,-   display.height Screen Design resolution-   display.cx,-   display.cy The X-and y-coordinates of the center of the screen-   display.left,-   display.top,-   display.right,-   Display.bottom screen four-sided coordinates-   display.c_left,-   display.c_top,-   display.c_right,-   display.c_bottom The coordinates of the sides of the screen when the parent object is in the center of the screen

  

F5 view

Good, see the effect, very good

Quick Cocos2dx-lua (V3.3R1) Learning Notes (iii)----don't always show Hello world, we show something else

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.