制作飞机大战项目,用scrollView做了关卡选择器。
部分代码如下:
…
//用MenuItemImage添加地图
auto backGroundOne = MenuItemImage::create(“GREEN_MAP.png”,“GREEN_MAP.png”,
CC_CALLBACK_1(ChooseMap::chooseCallback, this));
backGroundOne->setScale(1.5);
auto backGroundTwo = MenuItemImage::create(“BULE_MAP.png”,“BULE_MAP.png”,
CC_CALLBACK_1(ChooseMap::chooseCallback, this));
backGroundTwo->setScale(1.5);
auto backGroundThree = MenuItemImage::create(“SKY_MAP.png”,“SKY_MAP.png”,
CC_CALLBACK_1(ChooseMap::chooseCallback, this));
backGroundThree->setScale(1.5);
//_spriteContainer是存放Sprite的容器
_spriteContainer->addChild(backGroundOne);
backGroundOne->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
_spriteContainer->addChild(backGroundTwo);
backGroundTwo->setPosition(Point(visibleSize.width/2+visibleSize.width/2,visibleSize.height/2));
_spriteContainer->addChild(backGroundThree);
backGroundThree->setPosition(Point(visibleSize.width/2+visibleSize.width,visibleSize.height/2));
_spriteContainer->setPosition(Point::ZERO);
_spriteContainer->setContentSize(Size(visibleSize.width * MAP_COUNT,visibleSize.height));
//加入scrollView
scrollView = ScrollView::create();
scrollView->setContainer(_spriteContainer);
scrollView->setDirection(ScrollView::Direction::HORIZONTAL);
scrollView->setTouchEnabled(false);
scrollView->setPosition(Point::ZERO);
scrollView->setViewSize(visibleSize);
scrollView->setContentOffset(Point::ZERO,true);
scrollView->setContentSize(Size(visibleSize.width * MAP_COUNT,visibleSize.height));
scrollView->setDelegate(this);
scrollView->setBounceable(true);
…
其他功能都没有问题,就是CC_CALLBACK_1无法响应,按不下去,请问有没有解决方案啊,谢谢。
已经解决
scrollView->setClippingToBounds(true);
然后再添加
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(false);
listener->onTouchBegan=CC_CALLBACK_2(ChooseMap::onTouchBegan, this);
listener->onTouchMoved=CC_CALLBACK_2(ChooseMap::onTouchMoved, this);
listener->onTouchEnded=CC_CALLBACK_2(ChooseMap::onTouchEnded,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);