我在我的cocos2d-x项目(版本2.2.2)的CCScrollLayer处使用CCScrollLayer文件。

我对其进行了调整,使其可以垂直滚动。我有一个问题:在图层中添加两个CCScrollLayer时,我只能滚动最后添加的CCScrollLayer。

这是我的代码:

我将包含两个CCScrollLayer的图层添加到场景中

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


我在一个图层中添加了SlideCharacter1和SlideCharacter2(chooseCharacterLayer),但是只有SlideCharacter2可以滚动

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;
}

最佳答案

您可以从SlideCharacter1的触摸方法中手动调用SlideCharacter2的触摸方法(开始,取消,移动,结束)以模拟触摸

10-06 15:54