次はcocos2d-xにLevelHelperを組み込みましょう。
cocos2d-xで使うにはLevelHelper用のソースファイルを書き出す必要があります。
Supporting Codeタブを選択して、書きだすコードの種類を選択してください。


File→GenerateCode→Cocos2d-xを選択してください。


これで書き出しは完了です。
書き出すタイミング
毎回書き出す必要はありません。
・新しいタグを追加した時
・新しい独自クラスを追加した時
・LevelHelperの最新版を指定した時
それ以外は書き出す事はありません。
画像を動かしたら、LevelHelperを保存するだけで反映されます。
Xcodeに追加していく
Xcodeを起動して、今までサンプルで使用していた「GaomarTest」プロジェクトを選択してください。
ResourcesフォルダにLevelHelperファイルとSpriteHelperファイルを追加していきます。


ソースの編集
HelloWorldScene.hとHelloWorldScene.cppファイルを編集します。
LevelHelperLoader.hをインクルードして、変数を指定します。
#ifndef __HELLO_WORLD_H__
#define __HELLO_WORLD_H__
// When you import this file, you import all the cocos2d classes
#include "cocos2d.h"
#include "Box2D.h"
#include "LevelHelperLoader.h"
class HelloWorld : public cocos2d::CCLayer {
public:
~HelloWorld();
HelloWorld();
// returns a Scene that contains the HelloWorld as the only child
static cocos2d::CCScene* scene();
void initPhysics();
virtual void draw();
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void update(float dt);
private:
b2World* world;
LevelHelperLoader* lh;
void keyBackClicked();
void keyMenuClicked();
void twitterTapped();
};
#endif // __HELLO_WORLD_H__
//
// HelloWorldScene.cpp
// gaomarTest
//
// Created by hirophilip on 2013/01/12.
// Copyright __MyCompanyName__ 2013年. All rights reserved.
//
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
// Android Includes
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "JNICalls/InterfaceJNI.h"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "ObjCCalls.h"
#endif
#include "LHSettings.h"
using namespace cocos2d;
using namespace CocosDenshion;
#define PTM_RATIO 32
enum {
kTagParentNode = 1,
};
HelloWorld::HelloWorld()
{
setTouchEnabled( true );
setKeypadEnabled( true );
CCSize s = CCDirector::sharedDirector()->getWinSize();
// init physics
this->initPhysics();
scheduleUpdate();
}
HelloWorld::~HelloWorld()
{
delete lh;
lh = NULL;
delete world;
world = NULL;
//delete m_debugDraw;
}
void HelloWorld::initPhysics()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
world = new b2World(gravity);
// Do we want to let bodies sleep?
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
// LevelHelper初期化
lh = new LevelHelperLoader("GaomarTest.plhs");
LHSettings::sharedInstance()->setLhPtmRatio(PTM_RATIO);
lh->addObjectsToWorld(world, this);
if(lh->hasPhysicBoundaries())
lh->createPhysicBoundaries(world);
if(!lh->isGravityZero())
lh->createGravity(world);
}
void HelloWorld::draw()
{
//
// IMPORTANT:
// This is only for debug purposes
// It is recommend to disable it
//
CCLayer::draw();
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
world->DrawDebugData();
kmGLPopMatrix();
}
void HelloWorld::update(float dt)
{
//It is recommended that a fixed time step is used with Box2D for stability
//of the simulation, however, we are using a variable time step here.
//You need to make an informed choice, the following URL is useful
//http://gafferongames.com/game-physics/fix-your-timestep/
int velocityIterations = 8;
int positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite* myActor = (CCSprite*)b->GetUserData();
myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );
}
}
}
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
//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);
}
}
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// add layer as a child to scene
CCLayer* layer = new HelloWorld();
scene->addChild(layer);
layer->release();
return scene;
}
41行目で解放処理をしてます
HelloWorld::~HelloWorld()
{
delete lh;
lh = NULL;
LevelHelperのファイルを指定して初期化しています。
65行目はおまじないです。今の最新版は書かなくても良さげですが、
Box2d周りでこの1行が無かったら結構苦労したんで、書いておきます。
69行目は見えない壁です。
70行目は重力再設定です。LevelHelperで指定した重力に設定し直します。
// LevelHelper初期化
lh = new LevelHelperLoader("GaomarTest.plhs");
LHSettings::sharedInstance()->setLhPtmRatio(PTM_RATIO);
lh->addObjectsToWorld(world, this);
if(lh->hasPhysicBoundaries())
lh->createPhysicBoundaries(world);
if(!lh->isGravityZero())
lh->createGravity(world);
これでビルドして実行すればLevelHelperファイルが読み込まれて、
設置したキャラクターが表示されると思います。

結構簡単に導入出来ますよ〜
色々LevelHelperを触って試してみましょう!
スポンサーサイト