cheka.jp 超不定期で更新する写真達。お口直しにどうぞ。

Cocos2d-x 3.0 RC1とRC2の違いに悩まされた

C++で実装にチャレンジ中です。
が、RC1とRC2だけでも変化が激しいのでしばらく見送りが良さそうです。

Cocos2d

悩んだこと

3.0から物理演算用のクラスが準備されて簡単(直感的に)に利用できるようになりました。
よく出てくるサンプルはだいたいこんな感じです。

auto scene = Scene::createWithPhysics();    
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    
Vect gravity;
gravity.setPoint(0, -9.8);
scene->getPhysicsWorld()->setGravity(gravity);
scene->getPhysicsWorld()->setSpeed(6.0f);

で、衝突判定がこんな感じ

auto listener = EventListenerPhysicsContact::create();
listener->onContactBegin = CC_CALLBACK_2(GameScene::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

bool GameScene::onContactBegin(PhysicsContact& contact)
{
CCLOG("-- onContactBegin -- ");
}

ここでハマりました。
RC2からCC_CALLBACK_2ではなく、CC_CALLBACK_1を利用します。
さらに衝突を判定するために各オブジェクトに判定用のマスク処理を行う必要があります。

取り敢えず2つのオブジェクトだけで判定してみるとこんな感じです。
setContactTestBitmaskで判定ビットを設定します。

Sprite* sprite = Sprite::createWithSpriteFrame(SpriteFrame::create("spritesheets.png",rect));

//重力の設定
auto pb = PhysicsBody::createBox(Size(sprite->getContentSize().width, sprite->getContentSize().height));
pb->setDynamic(false);
pb->setEnable(true);
//これが必要!
pb->setContactTestBitmask(1);
sprite->setPhysicsBody(pb);

・・2つ目省略
pb2->setContactTestBitmask(1);

・・ごにょごにょ
auto listener = EventListenerPhysicsContact::create();
listener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

//コールバック
bool GameScene::onContactBegin(PhysicsContact& contact)
{
}

タッチイベントについてはjavascriptでやった方法と同じです。
*正確にはc++の方法にjavascriptを合わせていると思いますが・・

//イベントの追加 画面タッチ
this->listener = EventListenerTouchOneByOne::create();
this->listener->setSwallowTouches(true);
this->listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
this->listener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this);
this->listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this);
this->listener->onTouchCancelled = CC_CALLBACK_2(GameScene::onTouchCancelled, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(this->listener, this);

で、実際作ってみたら面白く無いのでアイデアを練り直し中です。

Add a Comment

メールアドレスが公開されることはありません。