LoginSignup
5
5

More than 5 years have passed since last update.

マルチタッチ

Last updated at Posted at 2014-09-05

マルチタッチイベントを処理するには、onTouchesBegan(), onTouchesMoved(), onTouchesEnded() をオーバライドし、setTouchEnabled(true); でタッチイベントを有効にします。

ヘッダファイル:

void onTouchesBegan(const std::vector <cocos2d::Touch*>& touches, cocos2d::Event event);
void onTouchesMoved(const std::vector<cocos2d::Touch
>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch
*>& touches, cocos2d::Event *event);

実装ファイル:

bool GameLayer::init()
{

  setTouchEnabled(true);

}
void GameLayer::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event)
{

}

サンプルのコードを読むと、タッチイベントリスナーを作り、それをディスパッチャーに登録していますが、単に setTouchEnabled(true); をコールするだけでも大丈夫のようです。

■ 追記

上記コードは 3.2 で動作することを確認しましたが、非推奨とのコメントを頂きましたので、タッチイベントリスナーを作りそれをディスパッチャーに登録するコードも以下に記述しておきます。

bool GameLayer::init()
{
 :
 //setTouchEnabled(true); 非推奨
 // タッチイベントリスナー
 auto listener = EventListenerTouchAllAtOnce::create();
 listener->onTouchesBegan = CC_CALLBACK_2(GameLayer::onTouchesBegan, this);
 listener->onTouchesMoved = CC_CALLBACK_2(GameLayer::onTouchesMoved, this);
 listener->onTouchesEnded = CC_CALLBACK_2(GameLayer::onTouchesEnded, this);
 this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
 :
}

5
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5