好きなクラスを作成出来て、対象スプライトに定義することができます。
■使用例
・キャラクターの名前
・キャラクターのヒットポイント
・保持しているスコア
こんな感じでデフォルトで何か値を保持したい時に便利です。
もちろんGetter/Setterなので値の取得/変更も可能です。
LevelHelperを開いてCustom Propertiesタグから
カスタムクラスを新規で作成していきましょう。

設定するスプライトにカスタムクラスをあてがいます。

カスタムクラスを追加したので、
ソースの出力を行なってください。

ソースの出力を行うと「CustomClasses」フォルダにカスタムクラスが生成されています
XCodeを開いて、作成されたカスタムクラスをADDしておきます。

今回のサンプルはANIMALタグで取得して、
画面をタッチした所にスプライトがあれば取得する。
その取得したスプライトのカスタムクラスを取得しようと思います。
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
//Add a new body/atlas sprite at the touched location
CCSetIterator it;
CCTouch* touch;
for( it = touches->begin(); it != touches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
// ANIMALタグを全て取得
CCArray* spritesWithTag = lh->spritesWithTag(ANIMAL);
CCObject* data = NULL;
CCARRAY_FOREACH(spritesWithTag, data) {
LHSprite* spr = (LHSprite*)data;
// タッチした所にスプライトがあるかどうか確認
if (spr->isTouchedAtPoint(location)) {
// カスタムクラスをuserInfoで取得する
LHAnimalClass* cl = (LHAnimalClass*) spr->userInfo();
if (cl != NULL) {
// 定義したプロパティ名で取得する事が出来ます
CCLog("name=%s", cl->getName().c_str());
CCLog("hp=%f", cl->getHp());
}
}
}
}
}
スポンサーサイト