わざわざccTouchesBegan、ccTouchesMoved、ccTouchesEndedなどでダラダラとプログラムを書かなくても良いです。
具体的な実装方法は以下の通り
#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 touchBeganOnSprite(LHTouchInfo* info);
void touchMovedOnSprite(LHTouchInfo* info);
void touchEndedOnSprite(LHTouchInfo* info);
};
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);
this->setupCollisionHandling();
LHSprite* spr = lh->createSpriteWithName("0_0_0", "AnimalSheet", "Animal.pshs");
spr->setPosition(ccp(s.width/2, s.height/2));
// スプライトに対してタッチ処理を定義していきます
spr->registerTouchBeganObserver(this, callfuncO_selector(HelloWorld::touchBeganOnSprite));
spr->registerTouchMovedObserver(this, callfuncO_selector(HelloWorld::touchMovedOnSprite));
spr->registerTouchEndedObserver(this, callfuncO_selector(HelloWorld::touchEndedOnSprite));
}
////////////////////////////////////////////////////////////////
// タッチ開始
void HelloWorld::touchBeganOnSprite(LHTouchInfo *info)
{
if(info->sprite)
CCLog("Touch BEGIN on sprite %s", info->sprite->getUniqueName().c_str());
}
////////////////////////////////////////////////////////////////
// タッチ移動
void HelloWorld::touchMovedOnSprite(LHTouchInfo *info)
{
if(info->sprite)
CCLog("Touch MOVED on sprite %s", info->sprite->getUniqueName().c_str());
}
////////////////////////////////////////////////////////////////
// タッチ離す
void HelloWorld::touchEndedOnSprite(LHTouchInfo *info)
{
if(info->sprite)
CCLog("Touch ENDED on sprite %s", info->sprite->getUniqueName().c_str());
}
かなり簡単にタッチ処理が実装出来ちゃいます♪
スポンサーサイト