マルチタッチ::cocos2d-x v3.1.1
Cocos2d-x 3.0 beta2 シングルタッチ&マルチタッチイベント取得まとめ
大体はここを見ればできるのですが、v3.1.1ではいくつか記法が異なっている部分がありました。
まずはヘッダファイルに
最後に、ios/AppController.mmを編集して、マルチタッチを有効にします。
まずはヘッダファイルに
public:
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);
そして、cppファイルの,HelloWorld::init()に
auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);と書いて、リスナーを設定します。そして、関数を追加。
void HelloWorld::onTouchesBegan(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event){
std::vector<cocos2d::Touch*>::const_iterator iterator = touches.begin();
while (iterator != touches.end()) {
Touch* touch = (Touch*)(*iterator);
auto location = touch->getLocation();
iterator++;
}
return;
}
void HelloWorld::onTouchesMoved(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event){
std::vector<cocos2d::Touch*>::const_iterator iterator = touches.begin();
while (iterator != touches.end()) {
Touch* touch = (Touch*)(*iterator);
auto location = touch->getLocation();
iterator++;
}
return;
}
void HelloWorld::onTouchesEnded(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event){
std::vector<cocos2d::Touch*>::const_iterator iterator = touches.begin();
while (iterator != touches.end()) {
Touch* touch = (Touch*)(*iterator);
auto location = touch->getLocation();
iterator++;
}
return;
}
locationはタッチ位置です。最後に、ios/AppController.mmを編集して、マルチタッチを有効にします。
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
//ここに以下の一行を追加
[eaglView setMultipleTouchEnabled:YES];
コメント