How to use custom classes in cocos2dx-lua and use of tolua ++

Source: Internet
Author: User
Tags zip folder

Monkey original, reprinted. Reprinted Please note: Reprinted from cocos2d Development Network --cocos2dev.com, thank you!

Original address: http://www.cocos2dev.com /? P = 405




Use cocos2dx-lua development, inevitably define their own class, but how to use custom class?


First, let's take a look at how Lua calls C ++:

Lua script code-> use the coocs2dx intermediate parsing Layer Code-> convert it and call the front-end code of cocos2dx C ++

The code at the coocs2dx intermediate parsing layer is in the libs/Lua/cocos2dx_support/luaco cos2d. cpp file. For more information, see this file.


That is to say, you have defined a class, and Lua can call your own defined class. Your custom class must be stated in the parsing file luaco cos2d. cpp.


After reading the luaco cos2d. cpp file, some people may be dizzy and do not know how to declare their defined classes in luaco cos2d. cpp. However, don't worry. cocos2dx already provides the tolua ++ tool to automatically compile and generate a new luaco cos2d. cpp file.


Start to enter the subject.

1. Create a demo project for the coocs2dx-lua and define a class in the class.

Snsprite. h

////  SNSprite.h//  LuaDemo////  Created by LiuYanghui on 13-4-8.////#ifndef __LuaDemo__SNSprite__#define __LuaDemo__SNSprite__#include "cocos2d.h"USING_NS_CC;class  SNSprite : public CCSprite{public:    static SNSprite* create(const char* name);    private:    void initData();};#endif /* defined(__LuaDemo__SNSprite__) */


Snsprite. cpp

////  SNSprite.cpp//  LuaDemo////  Created by LiuYanghui on 13-4-8.////#include "SNSprite.h"SNSprite* SNSprite::create(const char* name){    SNSprite* sprite = new SNSprite();    if(sprite && sprite->initWithFile(name)){        sprite->initData();        sprite->autorelease();        return sprite;    }    CC_SAFE_DELETE(sprite);    return NULL;}void SNSprite::initData(){    CCActionInterval* action = CCSequence::createWithTwoActions(CCMoveTo::create(1.0, ccp(300,300)),                                                                CCMoveTo::create(1.0, ccp(100,100))                                                                );    this->runAction(CCRepeatForever::create(action));}


The above is a simple function for defining a class.


2. Use tolua ++ to add the custom class to the luaco cos2d. cpp file.

The tolua ++ tool is in the/tools/tolua ++/directory.

1. Compile the PKG File

First, we can see that there are a lot of PKG files in it. Therefore, we should first write the PKG file for the custom class. The file writing rules are in readme. I will briefly write the rules:

Writing. PKG files
1) Enum keeps the same // The enumerated type remains unchanged
2) Remove cc_dll for the class defines, pay attention to multi inherites // Delete the class definition of cc_dll and use multiple inheritance instead.
3) Remove inline keyword for declaration and implementation // Delete inline keyword declarations and Implementations
4) remove public protect and private // delete an access restriction
5) Remove the decalration of class member variable // delete a member variable
6) Keep static keyword // retain static keywords
7) Remove memeber functions that declared as private or protected // non-public functions are deleted.

The above are the rules for compiling PKG files.

Write the PKG file of the custom file below. Text only, suffix changed to PKG

Snsprite. PKG

class  SNSprite : public CCSprite{    static SNSprite* create(const char* name);};


Pay attention to comparing the previous header file declaration to learn more about the PKG file writing rules. Put the compiled PKG file in tolua ++ and put it together with all PKG files.


2. register the added PKG file in the cocos2d. PKG file.

Open the cocos2d. PKG file in a text editor and add our registration statement to the last line:

$ Pfile "snsprite. PKG"


3. Configure build. Sh to compile the script

In the toluaw.gov.mac.zip folder, unzip tolua ++ for Mac.

Open the build. Sh file and modify it to the following:

#!/bin/bash## Invoked build.xml, overriding the lolua++ propertySCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)TOLUA=/APP/cocos2d-2.1rc0-x-2.1.2/tools/tolua++/tolua++if [ -z "${TOLUA}" ]; then    TOLUA=`which tolua++5.1`fiif [ -z "${TOLUA}" ]; then    echo "Unable to find tolua++ (or tolua++5.1) in your PATH."    exit 1ficd ${SCRIPT_DIR}${TOLUA} -L basic.lua -o /Users/liuyanghui/Desktop/LuaDemo/LuaDemo/libs/lua/cocos2dx_support/LuaCocos2d.cpp Cocos2d.pkg


Note the following two changes:

A, tolua =/APP/cocos2d-2.1rc0-x-2.1.2/tools/tolua ++ this is your Mac version of tolua ++ address, please change to your own.

B. $ {tolua}-L basic. Lua-O/users/liuyanghui/desktop/luademo/libs/Lua/cocos2dx_support/luaccos2d. cpp cocos2d. PKG

/Users/liuyanghui/desktop/luademo/libs/Lua/cocos2dx_support/luaco cos2d after parameter-o. CPP is to generate a new luaco cos2d. modify the CPP storage path to your own.


4. Execute the compilation script to generate the luaco cos2d. cpp file.

Terminal CD to build. Sh directory. Run:

./Build. Sh

After the execution, a new luaco cos2d. cpp file is generated in the output directory you have defined. The classes we have defined are also added.


3. Call a custom class in Lua

In the demo project of the new cocos2dx-lua just now, add the function that calls the custom class in the hello. Lua file:

Local function createsunnylayer () Local layersunny = cclayer: Create () Local labtips = cclabelttf: Create ("this icon is the custom class used", "Arial", 18) labtips: setposition (CCP (240,280) layersunny: addchild (labtips) Local sp = snsprite: Create ("icon.png") SP: setposition (CCP (100,100) layersunny: addchild (SP) return layersunny end -- play background music, preload effec t


Add to scene:

-- Run local scenegame = ccscene: Create () -- create scenario -- scenegame: addchild (createlayerfarm () -- add farm layer to scenario -- scenegame: addchild (createlayermenu ()) -- add the menu interface layer to the scenario scenegame: addchild (createsunnylayer () ccdirector: shareddirector (): runwithscene (scenegame)


OK. xcode compilation and running will show the effect.








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.