LoginSignup
4
3

More than 5 years have passed since last update.

CCScrollLayerをcocos2d-x v3.0で使ってみる

Last updated at Posted at 2013-12-19

TouchDispatcherが使えなくなっているようで、代わりにEventListenerというものが実装されていました。
下記のようにコードを変更したら動きました。

CCScrollLayer.h
class CCScrollLayer : public Layer
{
protected:

    EventListenerTouch *m_listener;
CCScrollLayer.cpp
bool CCScrollLayer::initWithLayers(Array *layers, int widthOffset)
{   
    if (Layer::init())
    {       
        // Make sure the layer accepts touches
//      Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
        m_listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE);
        m_listener->onTouchBegan = CC_CALLBACK_2(CCScrollLayer::onTouchBegan, this);
        m_listener->onTouchMoved = CC_CALLBACK_2(CCScrollLayer::onTouchMoved, this);
        m_listener->onTouchEnded = CC_CALLBACK_2(CCScrollLayer::onTouchEnded, this);
        m_listener->onTouchCancelled = CC_CALLBACK_2(CCScrollLayer::onTouchCancelled, this);
        EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(m_listener, this);

()
}

void CCScrollLayer::onExit()
{
//  Director::getInstance()->getTouchDispatcher()->removeDelegate(this);
    EventDispatcher::getInstance()->removeEventListener(m_listener);
    Layer::onExit();
}

または、こちらの方法でも動くようです。
Layerの場合だけだと思われますが、こちらのほうが簡単でいいですね。

CCScrollLayer.cpp
bool CCScrollLayer::initWithLayers(Array *layers, int widthOffset)
{   
    if (Layer::init())
    {       
        // Make sure the layer accepts touches
//      Director::getInstance()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
        setTouchEnabled(true);
        setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
4
3
3

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
4
3