Arrangement of cocos2d-x and lua usage

Source: Internet
Author: User

Cocos2d-x 2.20 and later versions do not have to create a template, the method to create instead of Python to create, the method is as follows:

Python create_project.py-project HelloWorld-package com. Panda. Game-language cpp

To create a lua project, change the ending cpp to lua.


Then open the created sln project file and run it directly to find that it is a finished product. There are pictures, sound effects, and animations =

Examples/K/c/C0tG + rdC0wcu9xbG + examples/examples + CjwvcD4KPHA + PGJyPgo8L3A + examples/examples + bxloyw1%0g "PandaWu"
HelloTable = {name = "Panda", IQ = 129}

Function helloAdd (num1, num2, num3)
Return (num1 + num2 + num3)
End


Initialize, open the lua file, and set the stack pointer:

Lua_State * pL = lua_open ();
Luaopen_base (pL );
Luaopen_math (pL );
Luaopen_string (pL );

Int err = luaL_dofile (pL, "HelloLua. lua ");
CCLOG ("open: % d", err );

Lua_settop (pL, 0 );


Take the myName variable:

Lua_getglobal (pL, "myName ");
Int isstr = lua_isstring (pL, 1 );
CCLOG ("isstr = % d", isstr );
If (isstr! = 0)
{
Const char * str = lua_tostring (pL, 1 );
CCLOG ("get string % s", str );
}


Name and IQ In the table:

Lua_getglobal (pL, "helloTable ");
Lua_pushstring (pL, "name ");
Lua_gettable (pL,-2 );
Const char * name = lua_tostring (pL,-1 );
CCLOG ("% s", name );

Lua_getglobal (pL, "helloTable"); // you have to write this sentence.
Lua_pushstring (pL, "IQ"); // or pushstring
Lua_gettable (pL,-2 );
Int IQ = lua_tonumber (pL,-1 );
CCLOG ("% d", IQ );


Call helloAdd in lua:

Lua_getglobal (pL, "helloAdd ");
Lua_pushnumber (pL, 10 );
Lua_pushnumber (pL, 5 );
Lua_pushnumber (pL, 3 );
Lua_call (pL, 3, 1); // number of parameters and number of returned values
Int result = lua_tonumber (pL,-1 );
CCLOG ("result = % d", result );


Last: lua_close (pL );

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.