Skip to content

Instantly share code, notes, and snippets.

@takashi1975
Last active August 29, 2015 14:06
Show Gist options
  • Save takashi1975/733264f72ade6981797a to your computer and use it in GitHub Desktop.
Save takashi1975/733264f72ade6981797a to your computer and use it in GitHub Desktop.
Cocos2d-x v3.2 マルチタップ(cocos2d_tests の MultiTouchTest からコピる!)
#include "HelloWorldScene.h"
USING_NS_CC;
#pragma mark - ### マルチタッチ対応 ###
//デバッグ表示用 カラー
static const Color3B* s_TouchColors[EventTouch::MAX_TOUCHES] = {
&Color3B::YELLOW,
&Color3B::BLUE,
&Color3B::GREEN,
&Color3B::RED,
&Color3B::MAGENTA
};
//マルチタッチの状態管理用(onTouchBegan[生成] -> onTouchMoved[更新] -> onTouchEnded[解放])
class TouchPoint : public Node
{
public:
TouchPoint()
{
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
}
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
DrawPrimitives::setDrawColor4B(_touchColor.r, _touchColor.g, _touchColor.b, 255);
glLineWidth(10);
DrawPrimitives::drawLine( Vec2(0, _touchPoint.y), Vec2(getContentSize().width, _touchPoint.y) );
DrawPrimitives::drawLine( Vec2(_touchPoint.x, 0), Vec2(_touchPoint.x, getContentSize().height) );
glLineWidth(1);
DrawPrimitives::setPointSize(30);
DrawPrimitives::drawPoint(_touchPoint);
}
void setTouchPos(const Vec2& pt)
{
_touchPoint = pt;
}
void setTouchColor(Color3B color)
{
_touchColor = color;
}
static TouchPoint* touchPointWithParent(Node* pParent)
{
auto pRet = new TouchPoint();
pRet->setContentSize(pParent->getContentSize());
pRet->setAnchorPoint(Vec2(0.0f, 0.0f));
pRet->autorelease();
return pRet;
}
private:
Vec2 _touchPoint;
Color3B _touchColor;
};
#pragma mark - === HelloWorld ===
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
bool result = false;
do
{
//////////////////////////////
if ( !Layer::init() )
{
break;
}
//////////////////////////////
//マルチタッチ
{
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);
listener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
//ココまで来たら正常終了
result = true;
} while (0);
return result;
}
#pragma mark - ### マルチタップ 対応 ###
//タップの状態保持
static Map<int, TouchPoint *> s_map;
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)
{
//CCLOG("%s touches count:%u", __PRETTY_FUNCTION__, touches.size());
for (auto &item : touches)
{
auto touch = item;
auto location = touch->getLocation();
auto touchPoint = TouchPoint::touchPointWithParent(this);
{
touchPoint->setTouchPos(location);
touchPoint->setTouchColor(*s_TouchColors[touch->getID()]);
}
this->addChild(touchPoint);
s_map.insert(touch->getID(), touchPoint);
}
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event)
{
//CCLOG("%s touches count:%u", __PRETTY_FUNCTION__, touches.size());
for (auto &item : touches)
{
auto touch = item;
auto location = touch->getLocation();
//TouchPoint
auto touchPoint = s_map.at(touch->getID());
{
touchPoint->setTouchPos(location);
}
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)
{
//CCLOG("%s touches count:%u", __PRETTY_FUNCTION__, touches.size());
for (auto &item : touches)
{
auto touch = item;
auto touchPoint = s_map.at(touch->getID());
this->removeChild(touchPoint, true);
s_map.erase(touch->getID());
}
}
void HelloWorld::onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event)
{
//CCLOG("%s touches count:%u", __PRETTY_FUNCTION__, touches.size());
this->onTouchesEnded(touches, unused_event);
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
CREATE_FUNC(HelloWorld);
protected:
virtual bool init();
#pragma mark - タッチイベント(マルチタッチ)
protected:
virtual void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
virtual void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
virtual void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
virtual void onTouchesCancelled(const std::vector<cocos2d::Touch*>&touches, cocos2d::Event *unused_event);
};
#endif
@takashi1975
Copy link
Author

iOS ... AppController.mm の - (BOOL)application:didFinishLaunchingWithOptions: で、マルチタップ用のフラグを設定が必要。

//マルチタップ
[eaglView setMultipleTouchEnabled:true];

...意外と忘れる... ^^;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment