[Cocos2dX (2.x)

Source: Internet
Author: User
This website consists of www.himigame.comlua-2985.html? Click to subscribe? The latest developments in this blog! Notify you of the latest blog in time! This article is relatively tired, please refer to the latest article [COCOS2DX-LUA Script Development 4] Use tolua ++ to compile pkg, so as to create a custom class let Lua

This site articles are Li huaming Himi [Black Rice GameDev block] http://www.himigame.com/lua-2/985.html? Click to subscribe? The latest developments in this blog! Notify you of the latest blog in time! This article is relatively tired, please refer to the latest article [COCOS2DX-LUA Script Development 4] Use tolua ++ to compile pkg, so as to create a custom class let Lua

All articles on this site areLi huaming Himi
[Heimi GameDev block]Http://www.himigame.com/lua-2/985.html

? Click to subscribe?
The latest developments in this blog! Notify you of the latest blog in time!


This article is relatively tired, please refer to the latest article [COCOS2DX-LUA Script Development 4] Use tolua ++ to compile pkg, so as to create a custom class for Lua script to use

This article may cause the following problems in the latest cocos2dx version:

1

2

LUA ERROR: ...24F82-1230-41FE-8A04-C445FB7D1BAB/mtet.app/hello.lua:35:

error in function 'addChild'. argument #2 is 'MySprite'; 'CCNode' expected.


Himi hasn't updated any blog posts recently. In fact, I am hesitant to write a cocos2d (x) engine book in preparation, and the Directory draft has been completed, the detailed production process and source code of the cocos2d/x Action editor that everyone is most interested in! However, it is a pity that Himi still cannot write in time;

Another reason to give up writing is because of the support of my shoes, said in July to give you a series of tutorials on cocos2dx-lua, but has been due to time and other problems until now, if Himi really wants to prepare a book, it is estimated that it will be difficult to have time to update the blog within half a year. Of course, considering the company's projects, it will eventually give up;(Give up temporarily. If possible, it will be written)

By the way, for Himi9 technology groups, whether it is cocos2d-iphone, cocos2dx, android or the Unity3D group that will be published later, periodic Himi and administrators will clean up regularly (everything is for New Kids shoes that want to learn more). I hope you can join the group and discuss it actively.Well, nonsense will not say much, starting from today Himi for kids shoes out of cocos2dx-lua series development tutorial, hope you still have to support; 3Q

The current Himi development tool and other versions are as follows:

Mac: 10.8 xcode: 4.4.1 cocos2dx: cocos2d-2.0-rc2-x-2.0.1

This article introduces two knowledge points: 1. Lua basics 2. Use our custom genie class in the lua script

I. lua Basics

About Lua in fact long ago Himi wrote a basic blog about cocos2dx-Lua, but it is cocos2dx 1. x version, for not very familiar with the children's shoes, Himi is recommended to take a look, connection: [iOS-cocos2d-X Game Development 8] In the Cocos2dX game using Lua script for game development (basic) and introduces the detailed usage of the script in the game!The Lua series updated after Himi are based on Cocos2dx 2.x.

II: Use our custom genie class in the lua script

First create a cocos2dx-lua project and then add our custom sprite class in the project: here the Himi class name is: HSprite

HSprite. h:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//

// HSprite.h

// cocos2dx_lua_tests_by_Himi

//

// Created by Himi on 12-8-30.

//

//

#ifndef cocos2dx_lua_tests_by_Himi_HSprite_h

#define cocos2dx_lua_tests_by_Himi_HSprite_h

#include "cocos2d.h"

using namespace cocos2d;

class HSprite : public cocos2d::CCSprite{

public:

static HSprite* createHSprite(const char* _name);

void hspriteInit();

};

#endif

HSprite. cpp:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//

// HSprite.cpp

// cocos2dx_lua_tests_by_Himi

//

// Created by Himi on 12-8-30.

//

//

#import "HSprite.h"

HSprite* HSprite::createHSprite(const char* _name){

HSprite* sp = new HSprite();

if(sp && sp->initWithFile(_name)){

sp->hspriteInit();

sp->autorelease();

return sp;

}

CC_SAFE_DELETE(sp);

return NULL;

}

void HSprite::hspriteInit(){

CCMessageBox("create HSprite success", "Himi_Lua");

}

The above code is not explained. It is very simple. It inherits CCSprite and adds a createHSprite and a custom initialization function (hspriteInit)

Next we open the luaco cos2d. cpp class, which is in the libs/lua/cocos2dx_support directory of the project, for example:

Then, add the custom genie class so that the Lua script can understand it;

The steps are divided into three steps:

1. register our custom class:

Search for the "tolua_reg_types" function in the luaco cos2d. cpp class and register it in it:

1

tolua_usertype(tolua_S,"HSprite");

As shown in:

Step 2: declare the functions of the custom class:

Search for the "tolua_Cocos2d_open" function and add the following code to it:

1

2

3

4

tolua_cclass(tolua_S, "HSprite", "HSprite", "CCSprite", NULL);

tolua_beginmodule(tolua_S,"HSprite");

tolua_function(tolua_S,"createHSprite",tolua_Himi_HSprite_createHSrpite00);

tolua_endmodule(tolua_S);

For example:

Here we will start to explain:

First, define the class functions that can be recognized by the script, as follows:

A) tolua_cclass (tolua_S, "HSprite", "HSprite", "CCSprite", NULL );

Which class function does tolua_cclass declare? The default value of the first State is tolua_S.

The last two parameters are custom class names.

Next is the inherited parent class name.

B) add a parameter to start the Declaration:

Tolua_beginmodule (tolua_S, "HSprite ");

C) add custom functions:

Tolua_function (tolua_S, "createHSprite", tolua_Himi_HSprite_createHSrpite00 );

The first parameter defaults to the second parameter custom class name, and the third parameter implements the conversion between the script and the custom class.

Note: There are multiple functions, so you can continue to write them;

D) End udfs:

Tolua_endmodule (tolua_S );

Step 3: implement the tolua_Himi_HSprite_createHSrpite00 Conversion Function between our scripts

The implementation is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

/* method: create of class HSprite */

#ifndef TOLUA_DISABLE_tolua_Himi_HSprite_createHSrpite00

static int tolua_Himi_HSprite_createHSrpite00(lua_State* tolua_S)

{

#ifndef TOLUA_RELEASE

tolua_Error tolua_err;

if (

!tolua_isusertable(tolua_S,1,"HSprite",0,&tolua_err) ||

!tolua_isstring(tolua_S,2,0,&tolua_err) ||

!tolua_isnoobj(tolua_S,3,&tolua_err)

)

goto tolua_lerror;

else

#endif

{

const char* pszFileName = ((const char*) tolua_tostring(tolua_S,2,0));

{

HSprite* tolua_ret = (HSprite*) HSprite::createHSprite(pszFileName);

int nID = (tolua_ret) ? tolua_ret->m_uID : -1;

int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;

tolua_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"HSprite");

}

}

return 1;

#ifndef TOLUA_RELEASE

tolua_lerror:

tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);

return 0;

#endif

}

#endif //#ifndef TOLUA_DISABLE

As shown in:

Here, Himi explains:

Kids shoes can divide this implementation function into two parts from line 1 # endif,

First, 375 ~ Code between 384:

1

2

3

4

5

6

7

8

9

10

#ifndef TOLUA_RELEASE

tolua_Error tolua_err;

if (

!tolua_isusertable(tolua_S,1,"HSprite",0,&tolua_err) ||

!tolua_isstring(tolua_S,2,0,&tolua_err) ||

!tolua_isnoobj(tolua_S,3,&tolua_err)

)

goto tolua_lerror;

else

#endif

The parameter type is determined here:

Whether tolua_isusertable is the "third parameter" custom type

Whether tolua_isstring is of the string type

Tolua_isnoobj ends (No parameter judgment)

Then there is 386 ~ Code segment between 392:

1

2

3

4

5

6

7

const char* pszFileName = ((const char*) tolua_tostring(tolua_S,2,0));

{

HSprite* tolua_ret = (HSprite*) HSprite::createHSprite(pszFileName);

int nID = (tolua_ret) ? tolua_ret->m_uID : -1;

int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;

tolua_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"HSprite");

}

Here is the parsing of the script code to call the custom function created

Finally, we modify the code in the hello2.lua Script: (create a file that comes with the cocos2dx-lua project by default under Resources)

Modify the code in "-run" at the end of the script as follows:

1

2

3

4

5

6

-- run

local sceneGame = CCScene:create()

-- sceneGame:addChild(createLayerFram())

-- sceneGame:addChild(createLayerMenu())

sceneGame:addChild(createHimiLayer())

CCDirector:sharedDirector():runWithScene(sceneGame)

Here, Himi watched the addition of another layer and added its own Layer.

1

sceneGame:addChild(createHimiLayer())

Then add the Himi custom method to the script. The Code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

local function createHimiLayer()

local layerH = CCLayer:create()

local _font = CCLabelTTF:create("Himi _ (cocos2dx-Lua) Tutorial","Arial",33)

_font:setPosition(230,280)

layerH:addChild(_font)

-- Create a custom type genie

local hsprite = HSprite:createHSprite("himi.png")

hsprite:setPosition(100,100)

hsprite:setScale(1.5)

hsprite:setRotation(45)

layerH:addChild(hsprite)

return layerH

end

Create your own defined genie, and then call zoom, rotate, and set the coordinate function.

OK. The following figure shows the link after running:

Well, this article is here. students feel that the good articles on this site should be shared and broadcasted.

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.