1

I'm using CCScrollLayer files at here CCScrollLayer for my cocos2d-x project (version 2.2.2).

I adapted them to make it can scroll vertically. And I got a problem: when I add two CCScrollLayer in a layer, I just can only scroll the last CCScrollLayer that are added.

Here is my code:

I add a layer that contains the two CCScrollLayer to a Scene

void ChooseMapScene::addSlidingLayers()
{
  mChooseCharacterLayer = createChooseCharaterLayer();
  mChooseCharacterLayer->setPosition(CCPointZero);
  mChooseCharacterLayer->setTouchEnabled(true);      
  this->addChild(mChooseCharacterLayer, GR_FOREGROUND);
}

I add SlideCharacter1 and SlideCharacter2 in a layer (chooseCharacterLayer) but just the SlideCharacter2 can scroll

CCLayer* ChooseMapScene::createChooseCharaterLayer()
{
  CCLayer* chooseCharacterLayer = CCLayer::create();
  CCArray* characterArr1 = createCharactersArray(CHARACTER_LEFT_LAYER_POS);
  CCArray* characterArr2 = createCharactersArray(CHARACTER_RIGHT_LAYER_POS);

  mSlideCharacter1 = CCScrollLayerVertical::nodeWithLayers(characterArr1, 0);
  chooseCharacterLayer->addChild(mSlideCharacter1, GR_FOREGROUND);

  mSlideCharacter2 = CCScrollLayerVertical::nodeWithLayers(characterArr2, 0);
  chooseCharacterLayer->addChild(mSlideCharacter2, GR_FOREGROUND);

  // I add SlideCharacter1 and SlideCharacter2 in a layer (chooseCharacterLayer) but just the SlideCharacter2 can scroll

  return chooseCharacterLayer;
}

.

CCArray* ChooseMapScene::createCharactersArray(CCPoint pPos)
{
  CCArray* characterArr = CCArray::createWithCapacity(NUMBER_CHARACTERS);
  for (int i = 1; i <= NUMBER_CHARACTERS; ++i)
  {
    CCLayer* characterLayer = CCLayer::create();

    CCSprite* character = CCSprite::create(CCString::createWithFormat("Images/Game/Object/c%i.png", i)->getCString());
    character->setPosition(pPos);
    characterLayer->addChild(character, GR_FOREGROUND, i);
    characterArr->addObject(characterLayer);
  }
  return characterArr;
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
nvg58
  • 891
  • 1
  • 8
  • 17
  • Either of Scroll Layer scrolling when you swipe your finger or try to scroll? – Renaissance Apr 28 '14 at 07:57
  • I can't do anything(swipe or scroll) with SlideCharacter1, but with SlideCharacter2 i can scroll or swipe – nvg58 Apr 28 '14 at 09:05
  • Yes, it is possible that SlideCharacter1's touch delegate is not being called – Renaissance Apr 28 '14 at 09:10
  • So, how can i fix it? Any suggestion? P/s: I think it *may be* SlideCharacter2 has overlap the SlideCharacter1, so i can't interact with SlideCharacter1. Is it correct? – nvg58 Apr 28 '14 at 09:14
  • check for swallowTouch bool in your touch dispatcher. It might be possible that swallowTouch value is set to FALSE. Can you please share CCScrollLayerVertical file? – Renaissance Apr 28 '14 at 09:24
  • yes, of course.It's here [CCScrollLayerVertical.h](https://github.com/tranvictor/landlord/blob/horizontal-master/Classes/CCScrollLayerVertical.h) [CCScrollLayerVertical.cpp](https://github.com/tranvictor/landlord/blob/horizontal-master/Classes/CCScrollLayerVertical.cpp) – nvg58 Apr 28 '14 at 13:47
  • Yes, Swallow touch value is set to false so it will not propagate touch to other touch delegate methods. So Enable swallowTouch in registerWithTouchDispatcher method. – Renaissance Apr 29 '14 at 05:06
  • So, I change the swallowTouch to true as below `void CCScrollLayerVertical::registerWithTouchDispatcher() { CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, true); } ` Then i cant touch anything in the screen. What happened? – nvg58 Apr 29 '14 at 06:01

1 Answers1

0

You can manually call SlideCharacter1's touch methods (began, cancelled, moved, ended) from SlideCharacter2's touch methods to simulate the touches

stnguyenvn
  • 21
  • 2
  • Can you be more specific? I found that CCScrollLayer touch's method are all protected. Can you give an example? – nvg58 Apr 29 '14 at 08:15