Cocos2d-x 3.2 大富翁遊戲項目開發-第四部分 退出對話方塊,cocos2d-x項目開發

來源:互聯網
上載者:User

Cocos2d-x 3.2 大富翁遊戲項目開發-第四部分 退出對話方塊,cocos2d-x項目開發

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">這部分代碼從網上查閱了一下,基本都是以前版本編寫的,需要稍微改動一下即可,效果</span>


首先看看在MenuScene.cpp如是如何調用起對話方塊的void MenuScene::popupLayer(){    // 定義一個彈出層,傳入一張背景圖    PopupLayer* popDialog = PopupLayer::create(DIALOG_BG);    // ContentSize 是可選的設定,可以不設定,如果設定把它當作 9 圖縮放    popDialog->setContentSize(CCSizeMake(Quit_Dialog_Size_Width, Quit_Dialog_Size_Height));     popDialog->setTitle(DIALOG_TITLE);    popDialog->setContentText(DIALOG_CONTENT, 20, 60, 250);    // 設定回呼函數,回調傳回一個 CCNode 以擷取 tag 判斷點擊的按鈕    popDialog->setCallbackFunc(this, callfuncN_selector(MenuScene::quitButtonCallback));    // 添加按鈕,設定圖片,文字,tag 資訊    popDialog->addButton(BUTTON_BG1, BUTTON_BG3, OK, Btn_Quit_OK_TAG);    popDialog->addButton(BUTTON_BG2, BUTTON_BG3, CANCEL, Btn_Quit_Cancel_TAG);    this->addChild(popDialog);// 添加到當前層}

PopupLayer.h 和 PopupLayer.cpp是彈出對話方塊的代碼:

首先看PopupLayer.h
const int Pop_FontSize = 20;//定義字型大小class PopupLayer :public Layer{static PopupLayer * create(const char* backgroundImage);//根據背景圖建立對象void setTitle(const char* title ,int fontsize=Pop_FontSize);//設定對話方塊標題void setContentText(const char* text ,int fontsize=Pop_FontSize ,int padding=50 ,int paddintTop=100);//設定對話方塊常值內容void setCallbackFunc(Object* target, SEL_CallFuncN callfun);//設定按鍵回調方法bool addButton(const char* normalImage, const char* selectedImage, const char* title, int tag=0);//添加對話方塊按鍵,如確認取消virtual void onEnter();//當進入時調用virtual void onExit();//void buttonCallback(CCObject* pSender);    int m_contentPadding;// 文字內容兩邊的空白區距離int m_contentPaddingTop;  //文字上邊空白區距離 CCObject* m_callbackListener;SEL_CallFuncN m_callback;//定義具有retain屬性的變數    CC_SYNTHESIZE_RETAIN(Menu*, m__pMenu, MenuButton);CC_SYNTHESIZE_RETAIN(Sprite*, m__sfBackGround, SpriteBackGround);…………………….};


PopupLayer.cpp部分代碼如下:


PopupLayer::PopupLayer():m__pMenu(NULL), m_contentPadding(0), m_contentPaddingTop(0), m_callbackListener(NULL), m_callback(NULL), m__sfBackGround(NULL), m__s9BackGround(NULL), m__ltContentText(NULL), m__ltTitle(NULL){ }//釋放變數PopupLayer::~PopupLayer(){    CC_SAFE_RELEASE(m__pMenu);    CC_SAFE_RELEASE(m__sfBackGround);    CC_SAFE_RELEASE(m__ltContentText);    CC_SAFE_RELEASE(m__ltTitle);    CC_SAFE_RELEASE(m__s9BackGround);}


void PopupLayer::setCallbackFunc(cocos2d::Object *target, SEL_CallFuncN callfun){//menuItem根據調傳入的對象,會進行方法的回調    m_callbackListener = target;    m_callback = callfun;    }


bool PopupLayer::init(){             // 初始化需要的 Menu,隨後跟進參數向menu中添加Item選項        Menu* menu = Menu::create();        menu->setPosition(CCPointZero);        setMenuButton(menu);setTouchMode(Touch::DispatchMode::ONE_BY_ONE);auto listener = EventListenerTouchOneByOne::create();        listener->setSwallowTouches(true);  listener->onTouchBegan = [](Touch *t,Event *e){CCLog("PopupLayer touch");           return true;    };_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//屏蔽下層事件響應return true;}


根據參數,標題,tag ,圖片效果,添加MenuItem選項bool PopupLayer::addButton(const char *normalImage, const char *selectedImage, const char *title, int tag){Size winSize = CCDirector::getInstance()->getWinSize();    Point pCenter = ccp(winSize.width / 2, winSize.height / 2);    // 建立MenuItem按鈕,並設定回調方法    MenuItemImage* menuImage = MenuItemImage::create(normalImage, selectedImage, this, menu_selector(PopupLayer::buttonCallback));    menuImage->setTag(tag);    menuImage->setPosition(pCenter);    // 給MenuItem添加文字說明並設定在MenuItem中位置Size imenu = menuImage->getContentSize();    LabelTTF* ttf = LabelTTF::create(title, "", 20);    ttf->setColor(ccc3(0, 0, 0));    ttf->setPosition(ccp(imenu.width / 2, imenu.height / 2));    menuImage->addChild(ttf);    getMenuButton()->addChild(menuImage);    return true;

現在來看MenuItem的回調方法buttonCallback


void PopupLayer::buttonCallback(cocos2d::CCObject *pSender){    Node* node = dynamic_cast<Node*>(pSender);    CCLog("touch tag: %d", node->getTag());    if (m_callback && m_callbackListener){        (m_callbackListener->*m_callback)(node);//這會調用setCallbackFunc()方法傳入的MenuScene對象的quitButtonCallback()方法    }    this->removeFromParent();//把對話方塊從父節點移除 }

對話方塊彈出前調用onEnter方法進行介面初始化工作

void PopupLayer::onEnter(){…………………..    Size contentSize;    // 設定對話方塊背景,代碼省略    // 添加按鈕,並設定其位置    this->addChild(getMenuButton());    float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount() + 1);Vector<Node*> vecArray = getMenuButton()->getChildren();int j=0;for(auto it=vecArray.begin();it!=vecArray.end();it++){Node* node = dynamic_cast<Node*>(*it);node->setPosition(Point(winSize.width/2 - contentSize.width/2+btnWidth*(j+1),winSize.height/2-contentSize.height/3));j++;}    // 顯示對話方塊標題內容省略    // 添加對話方塊彈出效果    Action* popupLayer = Sequence::create(ScaleTo::create(0.0, 0.0),                                          ScaleTo::create(0.15, 1.05),                                          ScaleTo::create(0.08, 0.95),    ScaleTo::create(0.08, 1.0),NULL);   this->runAction(popupLayer);}

代碼寫的稍微多了些,為便於測試,我把代碼上傳一下,如下:

http://download.csdn.net/detail/lideguo1979/8263669

未完待續……………………………



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.