cocococos 2 d-xではtolua++を使って自分で作成したC++クラスを導入します。


cococococos 2 d-xバージョン:2.2.1
システム:os x 10.8.5
コンパイラ:xcode 5.02
まず自分でクラスを新規作成します。
h.h
class TestClass
{
    int a_, b_;
public:
    static TestClass* create(); // tolua++          new  ,     delete  
    void SetA(int a);
    void SetB(int b);
    int GetSum();
};
.cpp
void TestClass::SetA(int a)
{
    a_ = a;
}
void TestClass::SetB(int b)
{
    b_ = b;
}

int TestClass::GetSum()
{
    return a_ + b_;
}

TestClass* TestClass::create()
{
    return new TestClass();
}
tolua++を使う前に、クラスの説明ファイル(pkg)を作成したいです。以下は作成規則です。
  1)enum keeps the same    //エニュメレート・タイプは変更されません。
   2)remove CC_DLL for the class defines,pay atention to multi inheites      //CCを使わないでくださいDLL、マルチ継承に切り替えます。
   3)remove inline keyword for declaration and implemention               //内蔵変数を削除しますか?
   4)remove public protect and privte    //訪問限定語を使わないでください。
   5)remove the decaration of class member variable  //メンバー変数(publicの変数は保持できます。)
   6)keep static keyword        //静的キーワードを保持する
   7)remove meber functions that declead as privte or protected/非publicの関数はすべて削除します。
そしてTestClass.pkgを作成します。
class TestClass
{
    void SetA(int a);
    void SetB(int b);
    int GetSum();
    static TestClass* create(); //        
};
導入類のpkgファイルを作成する以外に、TestClass.pkgファイルをCococos 2 d.pkgファイルに配置する必要がありますが、その前にCocos 2 d.pkgのファイルを見てみます。
$#include "LuaCocos2d.h"

$pfile "matrix.pkg"
$pfile "ccTypes.pkg"
$pfile "CCObject.pkg"
$pfile "CCCommon.pkg"
$pfile "CCGeometry.pkg"
$pfile "CCArray.pkg"
はヘッダファイルを導入するための空腹であることが分かります。pfileはpkgファイルを導入するためのものですので、追加が必要です。
$#include "TestClass.h"
$pfile "TestClass.pkg"
〓の後のものは変えないでLuacos 2 d.cppの中に複製することができて、しかし$pfileはどのようにするのですか?私は本当に知りませんでした。。。。。。。。。
Cocos 2 d.pkgの中で、今からtolua++を使ってC++類のLuaを自動的に生成します。
でも、build.shの中のtolua++のパスを配置してください。私のtolua++はbuild.shと同じディレクトリにありますので、設定は以下の通りです。
#!/bin/bash
#
# Invoked build.xml, overriding the lolua++ property

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
TOLUA="./tolua++"
if [ -z "${TOLUA}" ]; then
    TOLUA=`tolua++`
fi

if [ -z "${TOLUA}" ]; then
    echo "Unable to find tolua++ (or tolua++5.1) in your PATH."
    exit 1
fi

cd ${SCRIPT_DIR}
${TOLUA} -L basic.lua -o ../../scripting/lua/cocos2dx_support/LuaCocos2d.cpp Cocos2d.pkg
は今直接運行できます。
./build.sh