如题 谢谢 大大们 想用scrollView 做个背包
是要动态添加的效果 上下拖拽 就刀塔传奇那样的背包
scrollview = ScrollView::create();
scrollview->setSize(Size(388.0f, 388.0f));
scrollview->setPosition(Vec2(228, 90));
scrollview->setBounceEnabled(true);
this->addChild(scrollview);
auto image = ImageView::create("ui/Status_equip/Character/UI01/_0015_ku01.png");
float innerWidth = scrollview->getSize().width;
float innerHight = scrollview->getSize().height + image->getSize().height;
scrollview->setInnerContainerSize(Size(innerWidth, innerHight));
auto frame = ImageView::create("ui/Status_equip/Character/UI01/_0015_ku01.png");
frame->setName(s1);
frame->addTouchEventListener(CC_CALLBACK_2(UIPack::Image, this));
frame->setPosition(Vec2(image->getSize().width / 2.0f + (image->getSize().width+3.0f) *line, innerHight - image->getSize().height / 2.0f - col*(frame->getSize().height + 3.0f )));
scrollview->addChild(frame);
红色部分是这个添加侦听吗?
我手里有个自己弄的的2.x版本的背包,等我回家之后找一下贴给你
#ifndef __Maxdilis__BagView__ #define __Maxdilis__BagView__ #include #include "cocos2d.h" #include "cocos-ext.h" #include "Defines.h" #include "GameDataModel.h" USING_NS_CC; USING_NS_CC_EXT; using namespace std; class BagView : public CCLayer, public CCScrollViewDelegate, public extension::CCBSelectorResolver, public extension::CCBMemberVariableAssigner { public: CCDictionary *bagDic; int bagType; public: virtual bool init(); virtual void onEnter(); virtual void onExit(); void initBagViewData(CCSize viewSize, int bagIndex, CCObject* target, SEL_MenuHandler gridItemCallback); void setBagViewData(CCDictionary *bagData, int bagType); void refreshBagView(bool isReposition, int selectedTag = -1); CCNode* generateBagItem(); void setGridTouchedMenuSelector(); void gridItemTouchedCallback(CCObject *pSender); void tab1SelectedCallback(); void tab2SelectedCallback(); void tab3SelectedCallback(); void chbxShowAvailbleCallback(CCObject *pSender); void chbxUnknownCallback(CCObject *pSender); CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(BagView, create); virtual SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject * pTarget, const char * pSelectorName); virtual extension::SEL_CCControlHandler onResolveCCBCCControlSelector(CCObject * pTarget, const char * pSelectorName); virtual bool onAssignCCBMemberVariable(CCObject * pTarget, const char * pMemberVariableName, CCNode * pNode); virtual void scrollViewDidScroll(CCScrollView* view); virtual void scrollViewDidZoom(CCScrollView* view); virtual void registerWithTouchDispatcher(void); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); private: CCLayer *contentLayer; CCObject *selectorTarget; SEL_MenuHandler selector; public: CCMenu *checkBoxMenu; CCMenu *tabsMenu; CCScrollView *scrollView; CCLabelTTF *tab1WordNormal; CCLabelTTF *tab1WordSelected; CCLabelTTF *tab2WordNormal; CCLabelTTF *tab2WordSelected; CCLabelTTF *tab3WordNormal; CCLabelTTF *tab3WordSelected; }; class BagViewLoader : public extension::CCNodeLoader{ public: CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(BagViewLoader, loader); protected: CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(BagView); }; class BagViewCreator : public CCNode{ public: CCNode* createBagView(); }; #endif ``` cpp文件// // BagView.cpp // Maxdilis // // Created by Harris on 13-12-21. // // #include "BagView.h" #include "GameManager.h" #include "ScrollMenu.h" bool BagView::init(){ if(!CCLayer::init()){ return false; } // init some background img // //////////////////////// contentLayer = CCLayer::create(); scrollView = CCScrollView::create(); scrollView->setAnchorPoint(CCPointZero); scrollView->setPosition(ccp(21, 64)); scrollView->setContainer(contentLayer); scrollView->setViewSize(CCSizeMake(335, 335)); // scrollView->setContentOffset(ccp(0, 0)); checkBoxMenu = NULL; tabsMenu = NULL; // scrollView = NULL; tab1WordNormal = NULL; tab1WordSelected = NULL; tab2WordNormal = NULL; tab2WordSelected = NULL; tab3WordNormal = NULL; tab3WordSelected = NULL; scrollView->setDirection(kCCScrollViewDirectionVertical); scrollView->setDelegate(this); this->addChild(scrollView, 100); this->setTouchEnabled(true); return true; } void BagView::onEnter(){ CCLayer::onEnter(); //scrollView->setContainer(this->contentLayer); //scrollView->setDelegate(this); //scrollView->setBounceable(true); //scrollView->setTouchEnabled(true); //scrollView->setTouchPriority(-20); } void BagView::onExit(){ CCLayer::onExit(); } void BagView::gridItemTouchedCallback(CCObject *pSender){ CCMenuItem *crntItem = (CCMenuItem*) pSender; if(crntItem->isSelected()){ CCLog("Current item tag is %d", crntItem->getTag()); }else{ CCLog("Cancelled"); } } void BagView::initBagViewData(CCSize viewSize, int bagIndex, CCObject *target, SEL_MenuHandler gridItemCallback){ //itemList->setViewSize(viewSize); //crntBag = (CCArray*) GameManager::getInstance()->bagArray->objectAtIndex(bagIndex); this->selectorTarget = target; this->selector = gridItemCallback; } void BagView::setBagViewData(CCDictionary *bagData, int bagType){ this->bagDic = bagData; this->bagType = bagType; } void BagView::refreshBagView(bool isReposition, int selectedTag){ int equipBagNum = 0; if(bagType == ITEM_TYPE_EQUI){ equipBagNum = GameManager::getInstance()->playerData->bag_equip_num; }else if(bagType == ITEM_TYPE_MATERIAL){ equipBagNum = GameManager::getInstance()->playerData->bag_material_num; }else{ equipBagNum = GameManager::getInstance()->playerData->bag_use_num; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCArray *gridArray = CCArray::create(); // grid item尺寸 // CCSprite *tempItemSprite = CCSprite::create("BagScene/bag_cell.png"); CCSprite *tempItemSprite = CCSprite::create("awesomeFrame/grid_empty.png"); float item_width = tempItemSprite->getContentSize().width; float item_height = tempItemSprite->getContentSize().height; tempItemSprite->release(); // grid item x间距与y间距 float gap_Xwidth = 10; //(winSize.width - item_width*5) / 6; float gap_Ywidth = 10; //(winSize.height - item_height*7) / 8; // 1st item 起始坐标 float stage_begainXPos = gap_Xwidth+item_width/2; float stage_begainYPos = winSize.height - item_height/2;// (do NOT DELETE this:gap_Ywidth+item_height/2); int layerContentHeight = (item_height+gap_Ywidth)* ( (int) ( (float) /*equipBagNum*/bagDic->count()/4 + 0.5) ) + gap_Ywidth; // for(int i=0; iobjectForKey(i)){ BagItemEquipment *crntEqui = (BagItemEquipment *) ele->getObject();// (BagItemModelBase*) bagDic->objectForKey(i); int itemType = crntEqui->item_type; switch (itemType) { case ITEM_TYPE_EQUI:{ // create item image item = CCSprite::createWithSpriteFrameName(crntEqui->imgPath.c_str()); item_copy = CCSprite::createWithSpriteFrameName(crntEqui->imgPath.c_str()); // create partner head image (if current equip is weared by partner) /* TODO: NEW LOGIC BagItemEquipment *itemEquip = (BagItemEquipment *) crntEqui; if(!itemEquip->wearerId.empty()){ string partnerImgPath; CCArray *partner = GameManager::getInstance()->partners; for(int i=0; icount(); i++){ CharactorProperty *crntPartner = (CharactorProperty *) partner->objectAtIndex(i); // if(crntPartner->cId == itemEquip->wearerId){ partnerImgPath = crntPartner->img; break; // } } wearer_headImg = CCSprite::createWithSpriteFrameName(partnerImgPath.c_str()); wearer_headImg_copy = CCSprite::createWithSpriteFrameName(partnerImgPath.c_str()); wearer_headImg->setScale(0.3f); wearer_headImg_copy->setScale(0.3f); wearer_headImg->setPosition(ccp(crnt_gridItem->getContentSize().width - wearer_headImg->getContentSize().width * 0.3f /2, wearer_headImg->getContentSize().height * 0.3f /2)); wearer_headImg_copy->setPosition(ccp(crnt_gridItem_selected->getContentSize().width - wearer_headImg->getContentSize().width * 0.3f/2, wearer_headImg->getContentSize().height * 0.3f/2)); }*/ break; } case 2:{ break; } case 3:{ break; } default:{ CCAssert(itemType, "unexpected item type"); break; } } item->setPosition(ccp(crnt_gridItem->getContentSize().width/2, crnt_gridItem->getContentSize().height/2)); item_copy->setPosition(ccp(crnt_gridItem->getContentSize().width/2, crnt_gridItem->getContentSize().height/2)); crnt_gridItem->addChild(item); crnt_gridItem_selected->addChild(item_copy); if(wearer_headImg != NULL && wearer_headImg_copy != NULL){ crnt_gridItem->addChild(wearer_headImg); crnt_gridItem_selected->addChild(wearer_headImg_copy); } // } CCSprite *selected_frame = CCSprite::create("BagScene/bag_cell_selected.png"); selected_frame->setPosition(ccp(crnt_gridItem_selected->getContentSize().width/2, crnt_gridItem_selected->getContentSize().height/2)); crnt_gridItem_selected->addChild(selected_frame); CCMenuItemSprite *crntItem = CCMenuItemSprite::create(crnt_gridItem, crnt_gridItem_selected, this->selectorTarget, this->selector); crntItem->setPosition(ccp(stage_begainXPos+(item_width+gap_Xwidth)*(i%4), layerContentHeight-44-(item_height+gap_Ywidth)*(i/4))); // since every stage is calling the same callback funtion, // so we need to set a tag, use getTag() to judge which item you chose crntItem->setTag( crntEqui->uniqueId /*i*/); gridArray->addObject(crntItem); //stageLayer->addChild(crntStage, 2, STAGE_BEGAIN_TAG+i); ++i; } // CCDICT_FOREACH END ScrollMenu *gridMenu = ScrollMenu::createWithArray(gridArray); gridMenu->setPosition(CCPointZero); CCPoint scrollViewWorldPos = scrollView->getParent()->convertToWorldSpace(scrollView->getPosition()); CCLog("ScrollView's world position: x: %f, y:%f", scrollViewWorldPos.x, scrollViewWorldPos.y); gridMenu->setTouchableViewSize(CCRectMake(scrollViewWorldPos.x, scrollViewWorldPos.y, 335, 335)); // ScrollMenu的瑕疵. 当强制设置某MenuItem为selected()时, ScrollMenu无法确认该MenuItem为选中状态而导致第一次点击 // selected MenuItem时, ScrollMenu会再次设置其为selected() 而导致视觉上为触摸失效。 // 需手动设置ScrollMenu的last_selectedItemTag(上一次选中的MenuItem Tag) if(selectedTag >= 0){ ((CCMenuItem *) gridMenu->getChildByTag(selectedTag))->selected(); gridMenu->last_selectedItemTag = selectedTag; } contentLayer->removeAllChildrenWithCleanup(true); contentLayer->addChild(gridMenu); if(isReposition){ contentLayer->setPosition(CCPointMake(0, -(layerContentHeight-335))); } contentLayer->setContentSize(CCSizeMake(winSize.width, layerContentHeight)); //scrollView->setContainer(contentLayer); } CCNode* BagView::generateBagItem(){ return NULL; } void BagView::tab1SelectedCallback(){ } void BagView::tab2SelectedCallback(){ } void BagView::tab3SelectedCallback(){ } void BagView::chbxShowAvailbleCallback(CCObject *pSender){ } void BagView::chbxUnknownCallback(CCObject *pSender){ } SEL_MenuHandler BagView::onResolveCCBCCMenuItemSelector(CCObject * pTarget, const char * pSelectorName){ CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "tab1SelectedCallback", BagView::tab1SelectedCallback); CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "tab2SelectedCallback", BagView::tab2SelectedCallback); CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "tab3SelectedCallback", BagView::tab3SelectedCallback); CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "chbxShowAvailbleCallback", BagView::chbxShowAvailbleCallback); CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "chbxUnknownCallback", BagView::chbxUnknownCallback); return NULL; } SEL_CCControlHandler BagView::onResolveCCBCCControlSelector(CCObject * pTarget, const char * pSelectorName){ return NULL; } bool BagView::onAssignCCBMemberVariable(CCObject * pTarget, const char * pMemberVariableName, CCNode * pNode){ CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "label_tab1_nor", CCLabelTTF *, this->tab1WordNormal); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "label_tab1_sle", CCLabelTTF *, this->tab1WordSelected); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "label_tab2_nor", CCLabelTTF *, this->tab2WordNormal); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "label_tab2_sle", CCLabelTTF *, this->tab2WordSelected); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "label_tab3_nor", CCLabelTTF *, this->tab3WordNormal); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "label_tab3_sle", CCLabelTTF *, this->tab3WordSelected); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "menu_checkboxes", CCMenu *, this->checkBoxMenu); CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "tab_menu", CCMenu *, this->tabsMenu); //CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "bagslots_scrollview", CCScrollView *, this->scrollView); return true; } void BagView::scrollViewDidScroll(CCScrollView* view){ } void BagView::scrollViewDidZoom(CCScrollView* view){ } void BagView::registerWithTouchDispatcher(void){ CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -20, false); } bool BagView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){ CCPoint touchPoint = this->convertTouchToNodeSpace(pTouch); return true; } void BagView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){ } void BagView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){ } void BagView::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent){ } CCNode* BagViewCreator::createBagView(){ cocos2d::extension::CCNodeLoaderLibrary * ccNodeLoaderLibrary = cocos2d::extension::CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); ccNodeLoaderLibrary->registerCCNodeLoader("BagView", BagViewLoader::loader()); /* Create an autorelease CCBReader. */ cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary); /* Read a ccbi file. */ CCNode * node = ccbReader->readNodeGraphFromFile("ccbi/bag_view.ccbi", this); ccbReader->release(); return node; } ``` 代码里垃圾比较多,而且后来一阵还改成cocosbuilder读取了, scrollview中的背包内容确实是手动敲出来的。refreshBagView就是核心代码了,主要问题在于 0 背包格子坐标,用小学数学知识解决。 1 scrollview中内嵌CCMenu的话会导致用户touchbegan如果在menuitem上,那么之后就不可以再滑动了(被吃了)。 2 内嵌CCMenu会导致超出scrollview的可视范围仍可点击。 3 刷新背包操作 滚动层能保持之前浏览的位置。 1、2用一个重写了CCMenu的ScrollMenu来实现。若需要的话我再跟帖回吧。 3还是用小学数学知识解决。 贴一张效果图,等我给服务起了去。。。
why are you so diao。。。
嗯 谢谢 看看 还有cocostudio Image_windows是图片控件 button_all_down是Image_windows的子 也是图片控件怎么获得button_all_down的坐标 getPosition()不对啊 Panel_407是绝对布局