CCMenuItemImage* Objects2dFactory::imageButton(CCNode* scene, CCMenu* menu, string normalImagePath, string selectedImagePath, string disabledImagePath, float positionX, float positionY, float width, float height, SEL_MenuHandler selector, void* selectorArg, AlignX alignX, AlignY alignY, int zOrder) { // Check arguments validity if(scene == NULL || menu == NULL || !selector) return NULL; // Create a button menu item CCMenuItemImage *button = CCMenuItemImage::create( normalImagePath.c_str(), selectedImagePath.c_str(), disabledImagePath.c_str(), scene, selector); if(!button) return NULL; if (selectorArg != NULL) { button->setUserData(selectorArg); } // Place the menu item in the given position button->setPosition(ccp(positionX, positionY)); // Set anchor button->setAnchorPoint(ccp(Constants::getAnchorValue(alignX), Constants::getAnchorValue(alignY))); // Set menu item width float buttonWidth = button->boundingBox().size.width; if (width > 0) // if width is defined, it is setted { button->setScaleX(width / buttonWidth); } // Set menu item height float buttonHeight = button->boundingBox().size.height; if (height > 0) // if height is defined, it is setted { button->setScaleY(height / buttonHeight); } // Add button to the menu menu->addChild(button, zOrder); return button; }
CCMenuItemImage* Objects2dFactory::clickableRectangle(CCNode* scene, CCMenu* menu, ccColor3B color, float positionX, float positionY, float width, float height, SEL_MenuHandler selector, void* selectorArg, AlignX alignX, AlignY alignY, GLubyte opacity, int zOrder) { // Check arguments validity if(scene == NULL || menu == NULL || !selector) return NULL; // Create a button menu item CCMenuItemImage *button = CCMenuItemImage::create("TopQXResources/white.png", "TopQXResources/white.png", "TopQXResources/white.png", scene, selector); if(!button) return NULL; // Set argument if (selectorArg != NULL) { button->setUserData(selectorArg); } // Set color button->setColor(color); // Set opacity button->setOpacity(opacity); // Place the menu item in the given position button->setPosition(ccp(positionX, positionY)); // Set anchor button->setAnchorPoint(ccp(Constants::getAnchorValue(alignX), Constants::getAnchorValue(alignY))); // Set menu item width float buttonWidth = button->boundingBox().size.width; button->setScaleX(width / buttonWidth); // Set menu item height float buttonHeight = button->boundingBox().size.height; button->setScaleY(height / buttonHeight); // Add button to the menu menu->addChild(button, zOrder); return button; }
// on "init" you need to initialize your instance bool Start::init() { mScreenSize = CCDirector::sharedDirector()->getWinSize(); for (int i = 0; i<gbackgroundNum; i++) { CCSprite *pbackGround = CCSprite::create("daybackground.png"); CCRect rcBounding = pbackGround->boundingBox(); pbackGround->setPosition(ccp(rcBounding.size.width*(i*2+1) / 2, mScreenSize.height / 2)); this->addChild(pbackGround); } CCSize groundSize; for (int i = 0; i<ggroundNum; i++) { pGround[i] = CCSprite::create("ground.png"); groundSize = pGround[i]->getContentSize(); pGround[i]->setPosition(ccp(groundSize.width*(i*2+1) / 2, groundSize.height / 2)); this->addChild(pGround[i],1); } mbirdstr.push_back("birdblue"); mbirdstr.push_back("birdred"); mbirdstr.push_back("birdyellow"); mbirdclolor=rand()%100%3; string strplist=mbirdstr[mbirdclolor]+".png"; tbBird = CCSprite::create(strplist.c_str()); tbBird->setPosition(ccp(mScreenSize.width/2,mScreenSize.height/2+10.)); addChild(tbBird,0); initAction(); CCMenuItemImage *tbstart = CCMenuItemImage::create("start.png", "start.png", this, menu_selector(Start::tbstartCallback) ); tbstart->setPosition(ccp(mScreenSize.width/4,groundSize.height + 10 + tbstart->getContentSize().height/2)); CCMenu* pMenu1 =CCMenu::create(tbstart, NULL); pMenu1->setPosition( CCPointZero ); addChild(pMenu1); CCMenuItemImage *tbscore = CCMenuItemImage::create("top.png", "top.png", this, menu_selector(Start::tbtopCallback) ); tbscore->setPosition(ccp(mScreenSize.width*3/4,groundSize.height+10 + tbscore->getContentSize().height/2)); CCMenu* pMenu2 =CCMenu::create(tbscore, NULL); pMenu2->setPosition( CCPointZero ); addChild(pMenu2); CCSprite *m_pFlappyBird = CCSprite::create("flappybird.png"); //CCSprite *m_pFlappyBird = CCSprite::create("sprites.png",CCRectMake(0,680,180,55)); CCRect rcBounding = m_pFlappyBird->boundingBox(); float fFlappyBirdHeight=rcBounding.size.height/2.0f; float xpos=mScreenSize.width/2; float ypos=mScreenSize.height*3/4+20; m_pFlappyBird->setPosition(ccp(xpos, ypos)); // 设置在屏幕中间 this->addChild(m_pFlappyBird); CCSprite*m_pRate = CCSprite::create("rate.png"); rcBounding = m_pRate->boundingBox(); CCSize startSize = m_pRate->getContentSize(); float fRateWidth=rcBounding.size.width/2.0f; float fRateHeight=rcBounding.size.height/2.0f; xpos=mScreenSize.width/2.0f; ypos=tbscore->boundingBox().getMaxY()+fRateHeight+10; m_pRate->setPosition(ccp(xpos, ypos)); // 设置在屏幕中间 this->addChild(m_pRate); scheduleUpdate(); //CCActionInterval* actionTo = CCMoveBy::create(0.5, ccp(0, 10)); //CCActionInterval* actionByBack = actionTo->reverse(); //tbBird->runAction(CCRepeatForever::create(CCSequence::create( actionTo,actionByBack,NULL))); return true; }
// on "init" you need to initialize your instance bool MainMenu::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object //CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("MainMenu-ipadhd.plist"); //_menuBatchNode = CCSpriteBatchNode::create("MainMenu-ipadhd.png"); //this->addChild(_menuBatchNode); auto CCSprite * sprite; sprite = CCSprite::create("mainmenucolor.png"); sprite->setRotation(90.0f); sprite->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); this->addChild(sprite,0); sprite = CCSprite::create("mainmenubull.png"); sprite->setPosition(ccp(visibleSize.width/2-sprite->boundingBox().size.width/2.5,visibleSize.height-sprite->boundingBox().size.height/2)); sprite->setScale(.95f); this->addChild(sprite); //sprite = CCSprite::createWithSpriteFrameName("playButton.png"); //sprite->setPosition(ccp(visibleSize.width/2+sprite->boundingBox().size.width/2,visibleSize.height/2+sprite->boundingBox().size.height/2)); //_menuBatchNode->addChild(sprite); CCMenuItemImage *playbutton = CCMenuItemImage::create("playButton.png","playButton.png",this,menu_selector(MainMenu::playButtonCallback)); playbutton->setPosition(ccp(visibleSize.width/2+playbutton->boundingBox().size.width/2,visibleSize.height/2+playbutton->boundingBox().size.height/2)); CCMenu* playMenu = CCMenu::create(playbutton, NULL); playMenu->setPosition(CCPointZero); this->addChild(playMenu, 1); CCMenuItemImage *quitbutton = CCMenuItemImage::create("quitbutton.png","quitbutton.png",this,menu_selector(MainMenu::menuCloseCallback)); quitbutton->setPosition(ccp(visibleSize.width/2+quitbutton->boundingBox().size.width/2,visibleSize.height/4)); CCMenu* quitMenu = CCMenu::create(quitbutton, NULL); quitMenu->setPosition(CCPointZero); this->addChild(quitMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label return true; }
CCMenuItemImage* Objects2dFactory::textButton(CCNode* scene, CCMenu* menu, string value, string normalImagePath, string selectedImagePath, string disabledImagePath, float positionX, float positionY, float width, float height, SEL_MenuHandler selector, void* selectorArg, AlignX alignX, AlignY alignY, ccColor3B textColor, int zOrder) { // Check arguments validity if(scene == NULL ||menu == NULL || !selector) return NULL; // Create a button menu item CCMenuItemImage *button = CCMenuItemImage::create( normalImagePath.c_str(), selectedImagePath.c_str(), disabledImagePath.c_str(), scene, selector); if(!button) return NULL; if (selectorArg != NULL) { button->setUserData(selectorArg); } // Place the menu item in the given position button->setPosition(ccp(positionX, positionY)); // Set menu item anchor button->setAnchorPoint(ccp(Constants::getAnchorValue(alignX), Constants::getAnchorValue(alignY))); // Set menu item height float buttonHeight = button->boundingBox().size.height; float buttonPosScaleHeight = buttonHeight; if (height > 0) // if height is defined, it is setted { button->setScaleY(height / buttonHeight); buttonPosScaleHeight = height; } // Create a label and initialize with the given string CCLabelTTF* pLabel = CCLabelTTF::create(value.c_str(), "Arial"/*string(Constants::getResourcesPath() + "SOResources/Fonts/AlphaFridgeMagnetsAllCap.ttf").c_str()*/, buttonPosScaleHeight * 0.6f); if(!pLabel) return NULL; // Set color pLabel->setColor(textColor); // Set menu item width float buttonWidth = button->boundingBox().size.width; float buttonPosScaleWidth = buttonWidth; if (width > 0) // if width is defined, it is setted { button->setScaleX(width / buttonWidth); buttonPosScaleWidth = width; } else // otherwise, we will compare the text width with the button width { float textWidth = pLabel->boundingBox().size.width; if (textWidth * 1.25 > buttonWidth) // if the width adapted to the text is bigger than the button width, adapted width must be setted, otherwise, button width must stay as it was { button->setScaleX(textWidth * 1.25f / buttonWidth); buttonPosScaleWidth = textWidth * 1.25f; } } // Place the label; must be in the center of the button pLabel->setPosition(ccp(buttonPosScaleWidth * 0.5f * 1/button->getScaleX(), buttonPosScaleHeight * 0.5f * 1/button->getScaleY())); // Set label centered anchor pLabel->setAnchorPoint(ccp(Constants::getAnchorValue(Centered), Constants::getAnchorValue(Middle))); // Add label to button button->addChild(pLabel); // Set label scale; the inverse of button scale, so label can be as it would be if it was a child of scene pLabel->setScaleY(1/button->getScaleY()); pLabel->setScaleX(1/button->getScaleX()); // If label is larger than button if (pLabel->boundingBox().size.width * button->getScaleX() > button->boundingBox().size.width) { // Label must be rescaled float newScale = button->boundingBox().size.width / (pLabel->boundingBox().size.width * button->getScaleX()); pLabel->setScaleX(pLabel->getScaleX() * newScale); } // Add button to the menu menu->addChild(button, zOrder); return button; }