いつの間にか、Macに対応しているようでしたので
次回作から【Cocos Studio2】を使用します。
少しだけ触りましたが、画像の配置がとても簡単です。
cocos2d-xを使用してゲームを作成する際に
一番お世話になるであろう”タッチイベント”を
配置した画像(sprite)に対して設定する方法をメモしておきます。
Cocos Studio2で作成したcsbファイルを配置するための宣言
1 2 3 4 5 |
<省略> //.hpp CC_SYNTHESIZE_RETAIN(Node *, _mainScene, MainScene); void eventSprite(const Vec2 position); }; |
csb配置とタッチイベント
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//.cpp // メインシーンノードの読み込み _mainScene = CSLoader::getInstance()->createNode("MainScene.csb"); this->addChild(_mainScene); //タッチイベント #define kSkillModalLayerPriority -1 auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [this](Touch *touch,Event*event)->bool{ Vec2 touchPos = touch->getLocation(); //タッチイベントの範囲チェック処理 this->eventSprite(touchPos); return true; }; auto dip = Director::getInstance()->getEventDispatcher(); dip->addEventListenerWithSceneGraphPriority(listener, this); dip->setPriority(listener, kSkillModalLayerPriority); |
タッチした場所が画像に含まれているかをチェック
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//.cpp void HelloWorldScene::eventSprite(const Vec2 position){ auto children = _mainScene->getChildren(); for (auto hogehoge: children) { bool isCheckPos = hogehoge->getBoundingBox().containsPoint(position); if (isCheckPos) { auto name = hogehoge->getName(); if (name == “イベントを追加したい名前”){ log(“ほげほげ”); } } } } |
⬛︎ざっくり概要
タッチ時のポジションを取得
タッチ時に画面に追加されているcsbファイルの子ノードを読み込む
子ノードの範囲がタッチ時のポジションと重なっていることを確認
子ノードの名前が処理を実施したい名前であることを確認
ほげほげ
ちなみに子ノードの名前はCocos Studio2で設定できます。
あとはTagも設定できるのでそちらも判定条件できます。
もっといい方法があったら教えてください!!!
ブログ書きました。// 【cocos2d-x】spriteにタッチしたかを判断する【Cocos Studio2】 https://t.co/N2mtYxI0wM