// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } ///////////////////////////// // 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 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width-45 - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer this->addChild(pLabel, 1); // test code // 배경화면 CCSprite* pSpriteBack = CCSprite::create("test.png"); // position the sprite on the center of the screen pSpriteBack->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer this->addChild(pSpriteBack, 0); // 시작화면 // CCSprite* pSpriteStart = CCSprite::create("q_start.jpg"); // position the sprite on the center of the screen // pSpriteStart->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer // this->addChild(pSpriteStart, 0); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCLog("Origin: (%.3f, %.3f)\n", origin.x, origin.y); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2, origin.y + pCloseItem->getContentSize().height/2)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, ZORDER_UI); // 2. background m_pSpaceDust = CCSprite::create("bg_front_spacedust.png"); m_pSpaceDust->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height)); m_pPlanetSunrise = CCSprite::create("bg_planetsunrise.png"); m_pPlanetSunrise->setPosition(ccp(origin.x + visibleSize.width, origin.y + visibleSize.height*0.9f)); m_pGalaxy = CCSprite::create("bg_galaxy.png"); m_pGalaxy->setPosition(ccp(origin.x + visibleSize.width*0.0f, origin.y + visibleSize.height*0.5f)); //m_pSpacialAnomaly = CCSprite::create("bg_spacialanomaly.png"); //m_pSpacialAnomaly->setPosition(ccp(origin.x + visibleSize.width*0.3f, origin.y + visibleSize.height*0.7f)); m_pSpacialAnomaly2 = CCSprite::create("bg_spacialanomaly2.png"); m_pSpacialAnomaly2->setPosition(ccp(origin.x + visibleSize.width, origin.y + visibleSize.height*0.1f)); this->addChild(m_pSpaceDust, ZORDER_BACKGROUND); this->addChild(m_pPlanetSunrise, ZORDER_BACKGROUND); this->addChild(m_pGalaxy, ZORDER_BACKGROUND); //this->addChild(m_pSpacialAnomaly, ZORDER_BACKGROUND); this->addChild(m_pSpacialAnomaly2, ZORDER_BACKGROUND); // 3. player m_pPlayer = CCSprite::create("spaceman_tiny.png"); m_x = origin.x + visibleSize.width/2; m_y = origin.y + GROUND_HEIGHT; m_pPlayer->setPosition(ccp(m_x, m_y)); this->addChild(m_pPlayer, ZORDER_PLAYER); // 4. planets m_pBatchNode = CCSpriteBatchNode::create("planets.pvr.ccz"); this->addChild(m_pBatchNode, ZORDER_INTERACTIVE); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("planets.plist"); // 5. events this->scheduleUpdate(); this->setTouchEnabled(true); this->schedule(schedule_selector(HelloWorld::gameLogic), 2.0f); // 6. other m_pPlanets = new CCArray; CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("SpaceGame.wav", true); bRet = true; } while (0); return bRet; }
// on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". CCLabelTTF* pLabel = CCLabelTTF::create("Hello 11111", "Arial", 24); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 50)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel, 1); // 3. Add add a splash screen, show the cocos2d splash image. CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen pSprite->setPosition(ccp(size.width/2, size.height/2)); // Add the sprite to HelloWorld layer as a child layer. this->addChild(pSprite, 0); bRet = true; } while (0); return bRet; }
bool ResultLayer::init() { if ( CCLayerColor::initWithColor( ccc4(255, 255, 255, 255) ) ) { CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); // make the labels of the score and the high score and add them on the layer. int score = Score::getScore(); int highScore = Score::getHighScore(); // CCLog("score = %d", score); // CCLog("high score= %d", highScore); CCString* scoreValue = CCString::createWithFormat("Score: %d", score); CCString* highScoreValue = CCString::createWithFormat("High Score: %d", highScore); // set font. CCLabelTTF* pScoreLabel = CCLabelTTF::create(scoreValue->getCString(), "arial", 48.0); // set the font color pScoreLabel->setColor(ccc3(255, 0, 127)); int widthScoreLabel =pScoreLabel->getContentSize().width; // set font. CCLabelTTF* pHighScoreLabel = CCLabelTTF::create(highScoreValue->getCString(), "arial", 48.0); // set the font color. pHighScoreLabel->setColor(ccc3(255, 0, 127)); // get the width and the height of the high score label. int widthHighScoreLabel =pHighScoreLabel->getContentSize().width; int heightHighScoreLabel = pHighScoreLabel->getContentSize().height; // set the positions of the labels. pScoreLabel->setPosition(ccp(origin.x + visibleSize.width / 2 - widthScoreLabel / 2, origin.y + (int)(visibleSize.height * (1.0 - 0.4)))); pHighScoreLabel->setPosition(ccp(origin.x + visibleSize.width / 2 - widthScoreLabel/ 2 + abs(widthHighScoreLabel - widthScoreLabel) / 2, origin.y + (int)(visibleSize.height * (1.0 - 0.4)) - heightHighScoreLabel - 20)); // add the labels to the layer. this->addChild(pScoreLabel, 1); this->addChild(pHighScoreLabel, 1); // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "BackNormal.png", "BackSelected.png", this, menu_selector(ResultLayer::menuBackCallback)); // Place the menu item bottom-right conner. // CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); // CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2, origin.y + pCloseItem->getContentSize().height/2)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); // Add the menu to SecondWorld layer as a child layer. this->addChild(pMenu, 1); return true; } else { return false; } }
bool CGroupInfoScene::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); // 背景 cocos2d::CCSprite *pSpriteBg = CCSprite::create( PATCH_RES_DIR("bg8.png") ); pSpriteBg->setScale(BG_SCALE); pSpriteBg->setAnchorPoint( ccp(0, 0) ); pSpriteBg->setPosition( ccp(0, 0) ); addChild( pSpriteBg, 0 ); // 关闭按钮 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( PATCH_RES_DIR("close2.png"), PATCH_RES_DIR("close1.png"), this, menu_selector(CGroupInfoScene::menuCloseCallback) ); CC_BREAK_IF( !pCloseItem ); pCloseItem->setAnchorPoint( ccp(0, 0) ); float w = visibleSize.width - pCloseItem->getContentSize().width - 5; float h = 5; pCloseItem->setPosition( ccp(w, h) ); CCMenu *pMenu = CCMenu::create( pCloseItem, NULL ); pMenu->setPosition( CCPointZero ); CC_BREAK_IF( !pMenu ); addChild( pMenu, 1 ); // 后退 { CCMenuItemImage *pBackItem = CCMenuItemImage::create( PATCH_RES_DIR("back.png"), PATCH_RES_DIR("back.png"), this, menu_selector(CGroupInfoScene::menuBackCallback) ); CC_BREAK_IF( !pBackItem ); pBackItem->setAnchorPoint( ccp(0, 0) ); float w = visibleSize.width - pBackItem->getContentSize().width - 5 - pCloseItem->getContentSize().width; float h = 5; pBackItem->setPosition( ccp(w, h) ); CCMenu *pMenu = CCMenu::create( pBackItem, NULL ); pMenu->setPosition( CCPointZero ); CC_BREAK_IF( !pMenu ); addChild( pMenu, 1 ); } bRet = true; // 群名字 CGroupInfo *group = CGameManager::Instance().GetCurGroup(); cocos2d::CCSprite *pGroupSprite = CCSprite::create( PATCH_RES_DIR("group.png") ); pGroupSprite->setScale(BG_SCALE); pGroupSprite->setAnchorPoint( ccp(0, 0) ); pGroupSprite->setPosition( ccp(10, visibleSize.height-pGroupSprite->getContentSize().height-5 ) ); addChild( pGroupSprite, 0 ); CCLabelTTF *labelGroup = CCLabelTTF::create(group->m_Name.c_str(), "Helvetica", 20.0); labelGroup->setAnchorPoint(CCPointZero); labelGroup->setPosition( ccp(pGroupSprite->getContentSize().width+20, visibleSize.height-pGroupSprite->getContentSize().height-5 ) ); addChild( labelGroup, 0 ); // 自己 cocos2d::CCSprite *pSelf = CCSprite::create( PATCH_RES_DIR("group_self.png") ); pSelf->setScale(BG_SCALE); pSelf->setAnchorPoint( ccp(0, 0) ); pSelf->setPosition( ccp(visibleSize.width/2, visibleSize.height-pGroupSprite->getContentSize().height-5 ) ); addChild( pSelf, 0 ); CCLabelTTF *labelSelf = CCLabelTTF::create(CGameManager::Instance().GetUser().c_str(), "Helvetica", 20.0); labelSelf->setAnchorPoint(CCPointZero); labelSelf->setPosition( ccp(visibleSize.width/2+pSelf->getContentSize().width+20, visibleSize.height-pGroupSprite->getContentSize().height-5 ) ); addChild( labelSelf, 0 ); // 加载成员列表 _InitMemberList(); _InitAddGame(); CNetManager::Instance().ResumeProcMsg(); CGameManager::Instance().SetGameWorld( this ); } while (0); return bRet; }
void LoginLayer::_initComponent() { CCSize size = CCDirector::sharedDirector()->getVisibleSize(); /* 大游戏之名字 */ CCSprite* nameOfGame = CCSprite::create("GAME_NAME.png"); nameOfGame->setPosition(ccp(size.width / 2, size.height - 120)); addChild(nameOfGame, 10); /* 可移动背景 */ srand( (unsigned)time( NULL ) ); int x = rand() % 2; int y = rand() % 1500; CCSprite* pLoginBg = CCSprite::create("bg_big.png"); pLoginBg->setAnchorPoint(CCPointZero); pLoginBg->setPosition(ccp(-y/*(LOGIN_BG_WIDTH - size.width) / 2*/, 0)); this->addChild(pLoginBg); if (x == 0) { CCActionInterval * move = CCMoveTo::create(LOGIN_BG_TIME, ccp(size.width - LOGIN_BG_WIDTH, 0)); CCActionInterval * move1 = CCMoveTo::create(LOGIN_BG_TIME, ccp(0, 0)); CCFiniteTimeAction * seq= CCSequence::create(move, move1, NULL); CCActionInterval * repeatForever =CCRepeatForever::create((CCActionInterval* )seq); pLoginBg->runAction(repeatForever); } else if (x == 1) { CCActionInterval * move = CCMoveTo::create(LOGIN_BG_TIME, ccp(0, 0)); CCActionInterval * move1 = CCMoveTo::create(LOGIN_BG_TIME, ccp(size.width - LOGIN_BG_WIDTH, 0)); CCFiniteTimeAction * seq= CCSequence::create(move, move1, NULL); CCActionInterval * repeatForever =CCRepeatForever::create((CCActionInterval* )seq); pLoginBg->runAction(repeatForever); } /* 效果火焰 */ CCParticleSystemQuad* particle = CCParticleSystemQuad::create("dzt3.plist"); particle->setAnchorPoint(CCPointZero); particle->setPosition(ccp(320, 0)); particle->setScaleX(0.9); particle->setAutoRemoveOnFinish(true); addChild(particle, 11); /* 火星 */ CCParticleSystemQuad* particlefire = CCParticleSystemQuad::create("firefly.plist"); particlefire->setAnchorPoint(CCPointZero); particlefire->setPosition(ccp(0, 800)); particlefire->setAutoRemoveOnFinish(true); addChild(particlefire, 11); /* 火星1 */ CCParticleSystemQuad* particlefire1 = CCParticleSystemQuad::create("firefly2.plist"); particlefire1->setAnchorPoint(CCPointZero); particlefire1->setPosition(ccp(0, 800)); particlefire1->setAutoRemoveOnFinish(true); addChild(particlefire1, 11); /* 开始游戏按钮 */ CCMenuItemImage *pLoginBtn = CCMenuItemImage::create( "pre.png", "def.png", this, menu_selector(LoginLayer::gameInit)); pLoginBtn->setAnchorPoint(ccp(0.5, 0)); pLoginBtn->setPosition(ccp(size.width / 2, 70)); CCMenu* pLoginMenu = CCMenu::create(pLoginBtn, NULL); pLoginMenu->setAnchorPoint(CCPointZero); pLoginMenu->setPosition(CCPointZero); this->addChild(pLoginMenu); CCSprite* startFont = CCSprite::create("start.png"); startFont->setAnchorPoint(ccp(0.5, 0)); startFont->setPosition(ccp(size.width / 2, 115)); this->addChild(startFont); CCActionInterval * scale = CCScaleTo::create(1, 1.2); CCActionInterval * scale1 = CCScaleTo::create(1.5, 1); CCFiniteTimeAction * seq= CCSequence::create(scale,scale1,NULL); CCActionInterval * repeatForever =CCRepeatForever::create((CCActionInterval* )seq); startFont->runAction(repeatForever); /* 服务器列表滚动视图 */ /* m_scrollView = CCScrollView::create(VIEW_SIZE); m_scrollView->setViewSize(VIEW_SIZE); m_scrollView->setContentSize(CCSizeMake(62*5, VIEW_SIZE.height)); m_scrollView->setAnchorPoint(ccp(0, 0)); m_scrollView->setPosition(SCROLLVIEW_OFFSETX, OFFSET_1136 + SCROLLVIEW_OFFSETY); m_scrollView->setDirection(kCCScrollViewDirectionHorizontal); m_scrollView->setBounceable(true); m_scrollView->setDelegate(this); this->addChild(m_scrollView); */ for (int i = 0; i < 5; ++i) { CCMenuItemImage *pSvrBtn = CCMenuItemImage::create( "LG_nor.png", "LG_nor.png", this, menu_selector(LoginLayer::option)); pSvrBtn->setTag(SVRBG_NORMAL + i); pSvrBtn->setAnchorPoint(ccp(0, 0)); pSvrBtn->setPosition(ccp(SCROLLVIEW_OFFSETX + 60 * i, m_pGameState->getBottomOffset() + SCROLLVIEW_OFFSETY)); CCMenu* pMenu = CCMenu::create(pSvrBtn, NULL); pMenu->setAnchorPoint(ccp(0, 0)); pMenu->setPosition(ccp(0, 0)); this->addChild(pMenu); /* 服务器名字 */ CCLabelTTF* svrName = CCLabelTTF::create("", "Arial", 30, CCSizeMake(35, 300), kCCTextAlignmentCenter); svrName->setAnchorPoint(ccp(0.5, 0)); svrName->setColor(ccc3(255, 215, 0)); svrName->setPosition(ccp(SCROLLVIEW_OFFSETX + 37.0f + 60.0f * i, m_pGameState->getBottomOffset() + SCROLLVIEW_OFFSETY + 75.0f)); this->addChild(svrName, 2, SVR_NAME + i); /* 选中背景状态 */ CCSprite* pSvrBtnBg = CCSprite::create("LG_sel.png"); pSvrBtnBg->setAnchorPoint(ccp(0, 0)); pSvrBtnBg->setPosition(ccp(SCROLLVIEW_OFFSETX + 60.0f * i, m_pGameState->getBottomOffset() + SCROLLVIEW_OFFSETY - 10.0f)); pSvrBtnBg->setVisible(false); this->addChild(pSvrBtnBg, 1, SVRBG_SELECT + i); } unsigned int server_id = CCUserDefault::sharedUserDefault()->getIntegerForKey(LOGIN_SERVER_KEY, 0); /* 更改服务器名 */ if (0 == server_id) { CCLabelTTF* svrName = (CCLabelTTF*)this->getChildByTag(SVR_NAME); svrName->setScale(1.1); svrName->setString("体验服务器"); CCLabelTTF* svrName1 = (CCLabelTTF*)this->getChildByTag(SVR_NAME+1); svrName1->setString("研发服务器"); m_serverid[0] = 0; m_serverid[1] = 1; CCSprite* sp = (CCSprite*)this->getChildByTag(SVRBG_SELECT); /*默认第一个选中*/ sp->setVisible(true); } else { CCLabelTTF* svrName = (CCLabelTTF*)this->getChildByTag(SVR_NAME); svrName->setString("体验服务器"); CCLabelTTF* svrName1 = (CCLabelTTF*)this->getChildByTag(SVR_NAME + 1); svrName1->setString("研发服务器"); svrName1->setScale(1.1); m_serverid[0] = 0; m_serverid[1] = 1; CCSprite* sp1 = (CCSprite*)this->getChildByTag(SVRBG_SELECT + 1); sp1->setVisible(true); } /* 登陆框,待改善 */ // 取出存储的用户 id std::string strUserId = CCUserDefault::sharedUserDefault()->getStringForKey(USER_NAME_KEY); LoginBox* login = LoginBox::create(); login->setPosition(ccp(100, 200)); login->setContent(strUserId); addChild(login, 100, LOGIN_ID_TAG); }
bool Combat::init(int monsterType) { CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); // Switch BGM AudioPlayer::PlayCombatBGM(); //角色创建 cplayer = CPlayer::create(); cplayer->setPlayer(); this->addChild(cplayer); monster = Monster::create(); monster->setMonster(monsterType); this->addChild(monster); //血条创建 playerblood = Blood::create(); playerblood->setBloodSlider(); playerblood->setSliderPosition(ccp(150,visibleSize.height-50)); playerblood->setTotalBlood(cplayer->healthPoint); playerblood->setCurrentBlood(cplayer->currentHp); monsterblood = Blood::create(); monsterblood->setBloodSlider(); monsterblood->setSliderPosition(ccp(visibleSize.width-150,visibleSize.height-50)); monsterblood->setTotalBlood(monster->healthPoint); monsterblood->setCurrentBlood(monster->currentHp); this->addChild(playerblood); this->addChild(monsterblood); //等级与当前血量label /*等级*/ char level[10],blood[10]; sprintf(level,LEVEL_LBL,cplayer->level); plevelLabel = CCLabelTTF::create(level, LABEL_FONT,20); plevelLabel->setPosition(ccp(60,visibleSize.height-20)); this->addChild(plevelLabel,2); sprintf(level,LEVEL_LBL,monster->level); mlevelLabel = CCLabelTTF::create(level, LEVEL_LBL,20); mlevelLabel->setPosition(ccp(visibleSize.width-60,visibleSize.height-20)); this->addChild(mlevelLabel,2); /*血量*/ sprintf(blood,HEALTH_LBL,cplayer->currentHp); pbloodLabel = CCLabelTTF::create(blood,LABEL_FONT,20); pbloodLabel->setPosition(ccp(240,visibleSize.height-20)); this->addChild(pbloodLabel,2); sprintf(blood,HEALTH_LBL,monster->currentHp); mbloodLabel = CCLabelTTF::create(blood,LABEL_FONT,20); mbloodLabel->setPosition(ccp(visibleSize.width-240,visibleSize.height-20)); this->addChild(mbloodLabel,2); //按钮创建 playerbutton = AbilityButton::create(); monsterbutton = AbilityButton::create(); playerbutton->CreateButton(); playerbutton->setButtonPosition(ccp(50,50)); playerbutton->updateDamage(cplayer->level); monsterbutton->CreateButton(); monsterbutton->setButtonPosition(ccp(480,50)); monsterbutton->LockButtonTouch(); monsterbutton->updateDamage(monster->level); if (monsterType==0) { CCMenuItemImage *pCloseItem = CCMenuItemImage::create(RUN_BTN_IMG_PATH,RUN_DOWN_BTN_IMG_PATH,this,menu_selector(Combat::exitCombat)); pCloseItem->setPosition(ccp(visibleSize.width/2,50)); CCMenu* pMenu = CCMenu::create(pCloseItem,NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu); } this->addChild(playerbutton); this->addChild(monsterbutton); //游戏结束Label winLabel = CCLabelTTF::create(YOU_WIN_LBL, LABEL_FONT, 80); loseLabel = CCLabelTTF::create(YOU_LOSE_LBL, LABEL_FONT, 80); winLabel->setPosition(ccp(visibleSize.width/2-20,visibleSize.height/2+20)); loseLabel->setPosition(ccp(visibleSize.width/2-20,visibleSize.height/2+20)); winLabel->setVisible(false); loseLabel->setVisible(false); this->addChild(winLabel); this->addChild(loseLabel); //schedule监听,每帧刷新一次 this->scheduleUpdate(); //订阅播放动画的消息 CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(Combat::playAnimation),ANIMATION_MSG,NULL); //订阅游戏结束 CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(Combat::gameOver),GAME_OVER_MSG,NULL); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite* pBackground = CCSprite::create("background.png"); pBackground->setPosition(ccp(size.width / 2, size.height / 2)); addChild(pBackground); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); CCPoint posBR = ccp(pEGLView->getVisibleOrigin().x + pEGLView->getVisibleSize().width, pEGLView->getVisibleOrigin().y); CCPoint posTL = ccp(pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y + pEGLView->getVisibleSize().height); // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(posBR.x - 20, posBR.y + 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); CCPoint posStep = ccp(220, -150); CCPoint beginPos = ccpAdd(posTL, ccpMult(posStep, 0.5f)); int line = 0; int row = 0; for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) { CCMenuItemImage* pMenuItem = CCMenuItemImage::create(s_EventMenuItem[i].id.c_str(), s_EventMenuItem[i].id.c_str(), this, menu_selector(HelloWorld::eventMenuCallback)); pMenu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag); CCPoint pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line)); CCSize itemSize = pMenuItem->getContentSize(); if ((pos.x + itemSize.width / 2) > posBR.x) { line += 1; row = 0; pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line)); } row += 1; pMenuItem->setPosition(pos); } CCLabelTTF* label = CCLabelTTF::create("reload all plugins", "Arial", 24); CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::reloadPluginMenuCallback)); pMenuItem->setAnchorPoint(ccp(0.5f, 0)); pMenu->addChild(pMenuItem, 0); pMenuItem->setPosition( ccp(size.width / 2, 0)); return true; }
bool ResultScene::initWithParam(int level, int minScore, int resultScore) { // 初期化色を変更 if (!CCLayerColor::initWithColor(ccc4(0xF8,0xEC,0xDE,0xFF))) //RGBA { return false; } //初期化に関するものを書く m_level = level; CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //RESULTの文字 CCLabelTTF* resultLabel; resultLabel = CCLabelTTF::create("RESULT", "Arial", 100.0); resultLabel->setColor(ccc3(0, 0, 0)); resultLabel->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.9)); this->addChild(resultLabel); //今回のスコア CCString* resultScoreStr = CCString::createWithFormat("TOUCH : %d",resultScore); CCLabelTTF* resultScoreLabel; resultScoreLabel = CCLabelTTF::create(resultScoreStr->getCString(), "Arial", 60.0); resultScoreLabel->setColor(ccc3(0, 0, 0)); resultScoreLabel->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.6)); this->addChild(resultScoreLabel); //今回のLv CCString* levelStr = CCString::createWithFormat("Lv : %d",level); CCLabelTTF* levelLabel; levelLabel = CCLabelTTF::create(levelStr->getCString(), "Arial", 60.0); levelLabel->setColor(ccc3(0, 0, 0)); levelLabel->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.7)); this->addChild(levelLabel); int rank; if(minScore >= resultScore){ rank = 3; //Game Center Cocos2dExt::NativeCodeLauncher::postAchievement(level, 100); }else if( minScore <= resultScore + 8){ rank = 2; }else{ rank = 1; } for (int i=0; i < rank; i++) { CCSprite* pStar = CCSprite::create("star.png"); pStar->setScale(0.2); float posX; if(i == 0){ posX = 0.3; }else if(i == 1){ posX = 0.5; }else{ posX = 0.7; } pStar->setPosition(ccp(winSize.width * posX, winSize.height * 0.8)); this->addChild(pStar); } //リトライボタン CCMenuItemImage* pRetryItem; pRetryItem = CCMenuItemImage::create("reload.png", "reload.png",this,menu_selector(ResultScene::replayGame)); pRetryItem->setScale(0.3); pRetryItem->setPosition(ccp(winSize.width * 0.3, winSize.height * 0.3)); //次のレベルへ CCMenuItemImage* pNextLevelItem; if (m_level == max_level){ pNextLevelItem = CCMenuItemImage::create("next.png", "next.png",this,menu_selector(ResultScene::showTitleMenu)); }else{ pNextLevelItem = CCMenuItemImage::create("next.png", "next.png",this,menu_selector(ResultScene::nextLevelGame)); } pNextLevelItem->setScale(0.3); pNextLevelItem->setPosition(ccp(winSize.width * 0.7, winSize.height * 0.3)); CCMenu* pMenu = CCMenu::create(pRetryItem,pNextLevelItem,NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu); return true; }
bool GameWorld::init() { do { CC_BREAK_IF(! CCLayer::init()); CCMenuItemImage *pTransform = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuTransformCallback)); CCMenuItemImage *pLeft = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuLeftCallback)); CCMenuItemImage *pRight = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuRightCallback)); CCMenuItemImage *pDown = CCMenuItemImage::create( "6.png", "6.png", this, menu_selector(GameWorld::menuDownCallback)); CCMenuItemImage *pGameOver = CCMenuItemImage::create( "GameOver.jpg", "GameOver.jpg", this, menu_selector(GameWorld::menuGameOver)); // Place the menu item bottom-right conner. CCSize size = CCDirector::sharedDirector()->getWinSize(); pTransform->setPosition(ccp(size.width - 190, 210)); pLeft->setPosition(ccp(size.width - 260, 140)); pRight->setPosition(ccp(size.width - 120, 140)); pDown->setPosition(ccp(size.width - 190, 70)); pGameOver->setPosition(ccp(size.width / 2, size.height / 2)); pGameOver->setScale(5); pGameOver->setVisible(false); mMenuGameOver = pGameOver; // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pTransform, pLeft, pRight, pDown, pGameOver, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); addChild(pMenu, 1); CCDrawNode *pDrawNode = CCDrawNode::create(); addChild(pDrawNode); CCPoint points[] = { CCPoint(0,0), CCPoint((CubeManager::MAIN_BOARD_WIDTH)* CubeManager::CUBE_SIZE, 0), CCPoint((CubeManager::MAIN_BOARD_WIDTH)* CubeManager::CUBE_SIZE, (CubeManager::MAIN_BOARD_HEIGHT + 1)* CubeManager::CUBE_SIZE), CCPoint(0, (CubeManager::MAIN_BOARD_HEIGHT + 1)* CubeManager::CUBE_SIZE) }; pDrawNode->drawPolygon(points, sizeof(points)/sizeof(points[0]), ccc4f(0,0,0.3f,0.5f), 4, ccc4f(0,0,1,1)); CCLabelTTF* pLabel = CCLabelTTF::create("Score:", "Arial", 56); pLabel->setPosition(ccp(size.width - 300, 1100)); pLabel->setAnchorPoint(ccp(0, 0.5f)); this->addChild(pLabel); pLabel = CCLabelTTF::create("0", "Arial", 56); pLabel->setPosition(ccp(size.width - 300, 1000)); pLabel->setColor(ccc3(255, 192, 0)); pLabel->setAnchorPoint(ccp(0, 0.5f)); //pLabel->setHorizontalAlignment(kCCTextAlignmentRight); this->addChild(pLabel); mLabelScore = pLabel; if (!mCubeManager.init(this)) return false; schedule(schedule_selector(GameWorld::stepGo), 0.5f); schedule(schedule_selector(GameWorld::checkKeyInput)); schedule(schedule_selector(GameWorld::onKeyInput), 0.05f); } while(0); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); m_fElapsedTime = 0; m_fScale = 1.0; m_bSpeedUp = false; ///////////////////////////// // 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 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Map Maker", "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer this->addChild(pLabel, 1); // 버퍼 생성 및 팔레트 로딩 m_pPaletteBuffer = new unsigned char[NUM_PALETTES * 4]; // RGBA * 256 인덱스 컬러 m_pIndexBuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT]; // 인덱스 컬러로 된 이미지 버퍼 m_pImageBuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT*4]; // RGBA * 512 * 512 /* // LAND.COL로 부터 팔레트 로딩 std::string sFilePath = std::string(""); sFilePath += CCFileUtils::sharedFileUtils()->fullPathForFilename("LAND.COL"); FILE *pal_file = fopen(sFilePath.c_str(), "rb"); if (pal_file) { fread(&head, sizeof(ColHeader), 1, pal_file); //if (head.num_cols > NUM_PALETTES) // head.num_cols = NUM_PALETTES; fread(palette, 256*3, 1, pal_file); fclose(pal_file); // 팔레트 변환 RGB -> RGBA BuildPal(palette, m_pPaletteBuffer); } */ // PNG로부터 팔레트 로딩 GetPaletteFromFile("mapheight.png", m_pPaletteBuffer, 256); // add generating "Map" screen" // 인덱스 컬러로 된 이미지에 랜덤 값을 채운다. for (int i = 0; i < IMAGE_WIDTH*IMAGE_HEIGHT; i++) m_pIndexBuffer[i] = rand()%NUM_PALETTES; // 인덱스 -> 이미지 변환 IndexToImage(m_pIndexBuffer, m_pImageBuffer, m_pPaletteBuffer, IMAGE_WIDTH*IMAGE_HEIGHT); m_pBitmap = new CCImage(); m_pBitmap->initWithImageData(m_pImageBuffer, IMAGE_WIDTH*IMAGE_HEIGHT*4, CCImage::kFmtRawData, IMAGE_WIDTH, IMAGE_HEIGHT, 8); CCTexture2D *pTex = new CCTexture2D(); pTex->initWithImage(m_pBitmap); m_pScreen = CCSprite::createWithTexture(pTex); pTex->release(); m_pBitmap->release(); // position the sprite on the center of the screen m_pScreen->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); m_pScreen->setScale( 2.0 ); // add the sprite as a child to this layer this->addChild(m_pScreen, 0); setTouchEnabled(true); this->scheduleUpdate(); return true; }
// on "init" you need to initialize your instance bool GameState::init() { ////////////////////////////// // 1. super init first ///LayerColor->Layer로 바꿔야됨. 성능차이 많이나는듯 if ( !CCLayer::init() ) { return false; } Camera::sharedCameraInstance()->release(); 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 // create menu, it's an autorelease object this->setTouchEnabled(true); for(int i=0;i<2;i++) { CCMenuItemImage* buttonLeftImg = CCMenuItemImage::create("UI/move_left_normal.png","UI/move_left_normal.png"); CCMenuItemImage* buttonRightImg = CCMenuItemImage::create("UI/move_right_normal.png","UI/move_right_normal.png"); CCSprite *pressImg = CCSprite::create("UI/move_left_normal.png"); pressImg->setOpacity(100); buttonLeftImg->setSelectedImage(pressImg); CCSprite *pressRightImg = CCSprite::create("UI/move_right_normal.png"); pressRightImg->setOpacity(100); buttonRightImg->setSelectedImage(pressRightImg); if(i == 0) { buttonLeftImg->setPosition(ccp(60,60)); buttonRightImg->setPosition(ccp(160,60)); } else { buttonLeftImg->setPosition(ccp(800,60)); buttonRightImg->setPosition(ccp(900,60)); } buttonLeftImg->setTag(LEFT_BUTTON); buttonRightImg->setTag(RIGHT_BUTTON); buttons[i] = Buttons::create(); buttons[i]->setMusicSrc("music/ui/ui_click.ogg"); buttons[i]->addChild(buttonLeftImg); buttons[i]->addChild(buttonRightImg); buttons[i]->setPosition(CCPointZero); this->addChild(buttons[i],ZORDER_BUTTON); } CCMenuItemFont* pMenuItem1 = CCMenuItemFont::create("Http Post",this,menu_selector(GameState::sendPostHttp)); CCMenuItemFont* pMenuItem2 = CCMenuItemFont::create("Http Get",this,menu_selector(GameState::sendGetHttp)); pMenuItem2->setColor(ccc3(255,255,255)); pMenuItem1->setColor(ccc3(255,255,255)); CCMenu* pMenu = CCMenu::create(pMenuItem1,pMenuItem2,NULL); pMenu->setPosition(240,160); pMenu->alignItemsVertically(); //this->addChild(pMenu); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer //this->addChild(pLabel, 1); // add "GameState" splash screen" CCSprite* pSprite = CCSprite::create("background/background_1.png"); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer #ifndef BODY_DEBUG this->addChild(pSprite, -20); #endif //pChracImg = CCSprite::create("chracter/move_ani.png"); //box2d/// b2Vec2 gravity = b2Vec2(0.0f,-30.0f); _world = new b2World(gravity); _world->SetAllowSleeping(true); _world->SetContinuousPhysics(true); contactListener = new MyContactListener(); _world->SetContactListener((b2ContactListener*)contactListener); b2BodyDef groundBodyDef; groundBodyDef.position.Set(0,0); b2Body *groundBody = _world->CreateBody(&groundBodyDef); b2EdgeShape groundEdge; b2FixtureDef boxShapeDef; boxShapeDef.shape = &groundEdge; CCSize s = this->getContentSize(); //////////////////////////////////////// Debug #ifdef BODY_DEBUG b2Draw *debugDraw = new GLESDebugDraw(PTM_RATIO); //PTM_RATIO 를 1~3 정도로 작게 두니까 맞던데? debugDraw->SetFlags(GLESDebugDraw::e_shapeBit); _world->SetDebugDraw(debugDraw); #endif //doSnow(); pressing = false; //doRain(); ////////////////////////pause pauseButton = Buttons::create(); CCMenuItemImage* pauseImage = CCMenuItemImage::create("UI/pause.png","UI/pause.png",NULL,this, menu_selector(GameState::pauseGame)); pauseImage->getSelectedImage()->setScale(0.9f,0.9f); CCSize pauseSize = pauseImage->getContentSize(); pauseButton->addChild(pauseImage); pauseButton->setPosition(ccp(getContentSize().width-pauseSize.width*0.5,getContentSize().height-pauseSize.height*0.5)); this->addChild(pauseButton,ZORDER_INTERFACE); //////////////////////////////Stage stageManager = new StageManager(_world,this); //////////////Sound SoundManager::getInstance()->playBackgroundMusic("music/background/game_bgm_loop.ogg",true); SoundManager::getInstance()->setBackgroundMusicVolume(0.3f); PauseState::getInstance()->setResumeTarget(this); EffectManager::getInstance()->setTarget(this); //fade fade = CCSprite::create("over/fade.png"); fade->setOpacity(0); fade->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); fade->retain(); #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) this->setKeypadEnabled(true); //키패드 활성. #endif userInterface = new UserInterface(this); doRain(); isPressReload = false; tutorialManager = new TutorialManager(this); tutorialManager->setCurrentPage(100); this->schedule(schedule_selector(GameState::update)); this->schedule(schedule_selector(Timer::update)); this->schedule(schedule_selector(GameState::box2DTest)); stageManager->stageUp(); ScoreManager::sharedInstance()->moveDistance = 0; ScoreManager::sharedInstance()->point = 0; return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::createWithItem(pCloseItem); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". CCLabelTTF* pLabel = CCLabelTTF::create("THE HULK", "Thonburi", 34); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 20)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("streetbackground.jpg"); // position the sprite on the center of the screen pSprite->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer this->addChild(pSprite, 0); /////////////////////////////////////////////////////////////////////////////// //Generate Menu bar for hulk actions generateHulkMenu(); /////////////////////////////////////////////////////////////////////////////// CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("hulkmoves.plist"); CCSpriteBatchNode * sceneSpriteBatchNode = CCSpriteBatchNode::create("hulkmoves.png"); this->addChild(sceneSpriteBatchNode, 0); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //hulk= CCSprite::spriteWithFile("slice_0_1.png.png"); hulk=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("slice_0_1.png")); hulk->setScale(0.8); hulk->setPosition(ccp(winSize.width/2,winSize.height*0.4)); addChild(hulk, 1); bRet = true; } while (0); return bRet; }
// on "init" you need to initialize your instance bool HelloWorld::init() { // Static reference mytry = this; // Init if ( !CCLayer::init() ) { return false; } // Menu CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // Menu CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // Hello World InterfaceJNI::helloWorld(); #endif // Welcome CCLabelTTF* pLabel = CCLabelTTF::create("CCSocialNetwork", "Thonburi", 34); CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); this->addChild(pLabel, 1); // Static Label Score CCLabelTTF* scLabel = CCLabelTTF::create("Your Score:", "Thonburi", 34); scLabel->setPosition( ccp(10 + scLabel->getContentSize().width/2, size.height - 20) ); this->addChild(scLabel, 1); // Simulate var Score sLabel = CCLabelTTF::create("123", "Thonburi", 34); sLabel->setPosition( ccp(10 + scLabel->getContentSize().width/2, size.height - 60) ); this->addChild(sLabel, 1); // Internet Label CCLabelTTF* internetLabel = CCLabelTTF::create("Internet Connection:", "Thonburi", 20); CCLabelTTF* internetValueLabel = CCLabelTTF::create("", "Thonburi", 20); internetLabel->setPosition( ccp(10 + internetLabel->getContentSize().width/2, size.height - 90) ); internetValueLabel->setPosition( ccp(10 + internetValueLabel->getContentSize().width/2, size.height - 120) ); this->addChild(internetLabel, 1); this->addChild(internetValueLabel, 1); // Check Internet CCLog("Go to check Internet connection..."); bool internet = this->tryIsInternetConnection(); if (internet) { internetValueLabel->setString("Connected YES"); } else { internetValueLabel->setString("Connected NO"); } CCLog("Internet connection value: %d", internet); mLabel = CCLabelTTF::create("", "Thonburi", 20); mLabel->setPosition(ccp(size.width/2, 20)); this->addChild(mLabel); //Main Menu With Font CCLabelTTF *sendMailButtonLabel = CCLabelTTF::create("Send an email", "Thonburi", 24); CCMenuItemLabel *sendMailButton = CCMenuItemLabel::create(sendMailButtonLabel, this, menu_selector(HelloWorld::trySendAnEmail)); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) CCLabelTTF *sendMailButtonLabelInApp = CCLabelTTF::create("Send an email (in app mailer)", "Thonburi", 24); CCMenuItemLabel *sendMailButtonInApp = CCMenuItemLabel::create(sendMailButtonLabelInApp, this, menu_selector(HelloWorld::trySendAnEmailInApp)); #endif CCLabelTTF *sendTweetButtonLabel = CCLabelTTF::create("Send a tweet", "Thonburi", 24); CCMenuItemLabel *sendTweetButton = CCMenuItemLabel::create(sendTweetButtonLabel, this, menu_selector(HelloWorld::trySendATweet)); CCLabelTTF *postOnFBButtonLabel = CCLabelTTF::create("Post on FB", "Thonburi", 24); CCMenuItemLabel *postOnFBButton = CCMenuItemLabel::create(postOnFBButtonLabel, this, menu_selector(HelloWorld::tryPostOnFB)); CCMenu *mainMenu = CCMenu::create(sendMailButton, #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) sendMailButtonInApp, #endif sendTweetButton, postOnFBButton, NULL); mainMenu->alignItemsVerticallyWithPadding(size.height * 0.06f); mainMenu->setPosition(ccp(size.width/2, size.height/2)); this->addChild(mainMenu, 1); return true; }
bool MPJonsMapLayer::init() { CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); // Exit button CCMenuItemImage *backItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(MPJonsMapLayer::titleCallback)); backItem->setPosition(ccp(origin.x + visibleSize.width - backItem->getContentSize().width/2 , origin.y + backItem->getContentSize().height/2)); CCMenu* menu = CCMenu::create(backItem, NULL); menu->setPosition(CCPointZero); this->addChild(menu, 1); // Screen Title CCLabelTTF* label = CCLabelTTF::create("A Map Scene", "Arial", TITLE_FONT_SIZE); label->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label, 1); // Tile Map CCTMXTiledMap *map = CCTMXTiledMap::create("JonMap/Random.tmx"); this->addChild(map, 0); MPJonsMapLayer::antiAliasMap(map); map->setAnchorPoint(ccp(.5, .5)); map->setPosition(ccp(visibleSize.width * .5, visibleSize.height * .5)); map->setScale(.5); CCTMXLayer* layer = map->layerNamed("Background"); assert(layer != NULL); CCSize mapSize = map->getContentSize(); float mapHeight = mapSize.height; float mapWidth = mapSize.width; // player->setPosition(ccp(positionX,positionY)); /* CCSprite *tile = layer->tileAt(ccp(5,6)); assert(tile != NULL); layer->removeTileAt(ccp(5, 6)); CCActionInterval* actionBy = CCMoveBy::create(2, ccp(visibleSize.width * .5, visibleSize.height * .5)); map->runAction(actionBy); CCActionInterval* action = CCScaleBy::create(2, 2); map->runAction(action); */ /* CCSize s = layer->getLayerSize(); for (int x = 2; x < s.width; x++) { for (int y = 0; y < s.height; y++) { layer->removeTileAt(ccp(x, y)); } } */ return true; }
void GameScene::showOverView() { if (m_pOverLayer == NULL) { m_pOverLayer = LGLayerColor::create(ccc4(0, 0, 0, 0)); m_pOverLayer->retain(); m_pOverLayer->setTouchMode(kCCTouchesOneByOne); m_pOverLayer->setTouchEnabled(true); m_pOverLayer->setZOrder(3); CCSprite *pTitle = CCSprite::create("title_over.png"); pTitle->setPosition(ccp(m_VisibleOrigin.x + m_VisibleSize.width / 2, m_VisibleOrigin.y + m_VisibleSize.height * 0.76f)); m_pOverLayer->addChild(pTitle); CCSprite *pBgScore = CCSprite::create("bg_score.png"); //pBgScore->setPosition(ccp(m_VisibleOrigin.x + m_VisibleSize.width / 2, m_VisibleOrigin.y + m_VisibleSize.height * 0.535f)); pBgScore->setPosition(ccp(m_VisibleOrigin.x + m_VisibleSize.width / 2, m_VisibleOrigin.y - pBgScore->getContentSize().height / 2)); pBgScore->setTag(kTagOverScoreBoard); m_pOverLayer->addChild(pBgScore); float bgW = pBgScore->getContentSize().width, bgH = pBgScore->getContentSize().height; int record = CCUserDefault::sharedUserDefault()->getIntegerForKey("record", 0); if (record < m_CurrentScore) { record = m_CurrentScore; CCUserDefault::sharedUserDefault()->setIntegerForKey("record", record); NativeBridge::submitScore(record); CCSprite *indicatorNew = CCSprite::create("new.png"); indicatorNew->setPosition(ccp(bgW * (0.5 - 0.2448980f), bgH * (0.5 - 0.08035714f))); pBgScore->addChild(indicatorNew); } CCLabelAtlas *current = CCLabelAtlas::create(CCString::createWithFormat("%d", m_CurrentScore)->getCString(), "fonts/font2.png", 36, 48, '0'); current->setAnchorPoint(ccp(0.5, 0.5)); current->setPosition(bgW / 2, bgH * 0.6394f); pBgScore->addChild(current); CCLabelAtlas *best = CCLabelAtlas::create(CCString::createWithFormat("%d", record)->getCString(), "fonts/font2.png", 36, 48, '0'); best->setAnchorPoint(ccp(0.5, 0.5)); best->setPosition(bgW / 2, bgH * (0.5 - 0.27679f)); pBgScore->addChild(best); CCMenuItemImage *pRetry = LGMenuItemImage::create("retry.png", NULL, this, menu_selector(GameScene::retryCallback)); CCMenuItemImage *pMenu = LGMenuItemImage::create("menu.png", NULL, this, menu_selector(GameScene::menuCallback)); pRetry->setPosition(ccp(m_VisibleOrigin.x + m_VisibleSize.width * 0.73333333f, m_VisibleOrigin.y + m_VisibleSize.height * 0.285f)); pMenu->setPosition(ccp(m_VisibleOrigin.x + m_VisibleSize.width * 0.26666667f, pRetry->getPositionY())); CCMenu *pCCMenu = CCMenu::create(pRetry, pMenu, NULL); pCCMenu->setTag(kTagOverMenu); pCCMenu->setAnchorPoint(CCPointZero); pCCMenu->setPosition(ccp(0, m_VisibleOrigin.y - (pRetry->getPositionY() - m_VisibleOrigin.y) * 2)); m_pOverLayer->addChild(pCCMenu); } m_pOverLayer->getChildByTag(kTagOverScoreBoard)->runAction(CCMoveTo::create(0.5f, ccp(m_VisibleOrigin.x + m_VisibleSize.width / 2, m_VisibleOrigin.y + m_VisibleSize.height * 0.535f))); m_pOverLayer->getChildByTag(kTagOverMenu)->runAction(CCMoveTo::create(1.0f, CCPointZero)); if (m_pOverLayer && m_pOverLayer->getParent() == NULL) { getParent()->addChild(m_pOverLayer); CC_SAFE_RELEASE_NULL(m_CurrentLayer); m_CurrentLayer = m_pOverLayer; m_CurrentLayer->retain(); onShowOverlay(true); } m_ScoreLabel->setVisible(false); }
void MainGameScene::makeResumeDaialog() { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //CCSpriteで作ると、タッチ判定無効とかめんどくさいからBGもMenuItemで作る CCMenuItemImage* resumeBG = CCMenuItemImage::create("logo_Info2.png","logo_Info2.png"); resumeBG->setScale(0.7); resumeBG->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.5)); resumeBG->setEnabled(false); CCSize pResumeBGSize = resumeBG->getContentSize(); CCLabelTTF* resumeLabel; resumeLabel = CCLabelTTF::create(NKLocalizedString::LocalizedString("resume", "RESUME"), "Copperplate", 60.0); resumeLabel->setColor(ccc3(0, 0, 0)); resumeLabel->setPosition(ccp(pResumeBGSize.width * 0.5 ,pResumeBGSize.height * 0.7)); resumeBG->addChild(resumeLabel); CCMenuItemImage* titleItem = CCMenuItemImage::create("button1.png","button1.png" ,this, menu_selector(MainGameScene::tapTitleButton)); titleItem->setPosition(ccp(winSize.width * 0.3, winSize.height * 0.47)); CCLabelTTF* titleLabel; titleLabel = CCLabelTTF::create("TITLE", "Arial", 30.0); CCSize pTitleItemSize = titleItem->getContentSize(); titleLabel->setPosition(ccp(pTitleItemSize.width / 2 ,pTitleItemSize.height / 2)); titleItem->addChild(titleLabel); titleItem->setScale(0.8); CCMenuItemImage* returnGameItem = CCMenuItemImage::create("button2.png","button2.png" ,this, menu_selector(MainGameScene::returnMainGame)); returnGameItem->setPosition(ccp(winSize.width * 0.7, winSize.height * 0.47)); CCLabelTTF* returnGameLabel; returnGameLabel = CCLabelTTF::create("CANCEL", "Arial", 30.0); CCSize pReturnGameSize = returnGameItem->getContentSize(); returnGameLabel->setPosition(ccp(pReturnGameSize.width / 2 ,pReturnGameSize.height / 2)); returnGameItem->addChild(returnGameLabel); returnGameItem->setScale(0.8); CCMenuItemImage* retryGameItem = CCMenuItemImage::create("button3.png","button3.png" ,this, menu_selector(MainGameScene::doRetry)); retryGameItem->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.39)); CCLabelTTF* retryGameLabel; retryGameLabel = CCLabelTTF::create("RETRY", "Arial", 30.0); CCSize pRetryGameSize = retryGameItem->getContentSize(); retryGameLabel->setPosition(ccp(pRetryGameSize.width / 2 ,pRetryGameSize.height / 2)); retryGameItem->addChild(retryGameLabel); retryGameItem->setScale(0.8); CCMenu* resumeDialog = CCMenu::create(resumeBG, titleItem, returnGameItem,retryGameItem,NULL); resumeDialog->setPosition(CCPointZero); resumeDialog->setTag(tagResumeDialog); resumeDialog->setOpacity(0); resumeDialog->setZOrder(Z_ORDER_RESUME_DIALOG); resumeDialog->setEnabled(false); this->addChild(resumeDialog); }
// on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))); // 1. Add a menu item with "X" image, which is clicked to quit the program. //Background CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite *backgroud = CCSprite::create("MainMenu.png"); CC_BREAK_IF(! backgroud); backgroud->setPosition(ccp(size.width / 2, size.height / 2)); this->addChild(backgroud); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". // const char *title = "俄罗斯方块"; // const char *startGame = "开始游戏"; // const char *gameSet = "游戏设置"; // const char *score = "游戏得分"; // const char *gameOver = "游戏结束"; /* const char *title = "TetrisGame";*/ // const char *startGame = "startGame"; // const char *gameSet = "gameSet"; // const char *score = "score"; // const char *gameOver = "gameEnd"; // CCLabelTTF* pLabel = CCLabelTTF::create(title, "Arial", 36); // CC_BREAK_IF(! pLabel); // // // Get window size and place the label upper. // pLabel->setPosition(ccp(size.width / 2, size.height * 4 / 5)); // // pLabel->setColor(ccc3(255, 255, 255)); // // Add the label to HelloWorld layer as a child layer. // this->addChild(pLabel, 1); // 加入4个行走方向的控制菜单 // CCMenuItem *first = CCMenuItemFont::create(startGame, this, menu_selector(HelloWorld::menuCallbackStart)); // CCMenuItem *second = CCMenuItemFont::create(gameSet, this, menu_selector(HelloWorld::menuCallbackSet)); // CCMenuItem *third = CCMenuItemFont::create(score, this, menu_selector(HelloWorld::menuCallbackScore)); // CCMenuItem *forth = CCMenuItemFont::create(gameOver, this, menu_selector(HelloWorld::menuCallbackEnd)); // CCMenu *menu = CCMenu::create(first, second, third, forth, NULL); // menu->setColor(ccc3(255, 255, 255)); // 为了查找方便,给菜单项设置tag //down->setTag(kDown); //left->setTag(kLeft); //right->setTag(kRight); //up->setTag(kUp); // 菜单项按间距垂直排列 // menu->alignItemsVerticallyWithPadding(50); // this->addChild(menu CCMenuItemImage *play = CCMenuItemImage::create( "play.png", "play.png", this, menu_selector(HelloWorld::menuCallbackStart)); play->setAnchorPoint(ccp(0, 0)); play->setPosition(ccp(size.width / 4, (size.height * 2) / 5)); CCMenuItemImage *end = CCMenuItemImage::create( "end.png", "end.png", this, menu_selector(HelloWorld::menuCallbackEnd)); end->setAnchorPoint(ccp(0, 0)); end->setPosition(ccp(size.width / 4, (size.height * 2) / 7)); CCMenu *menu = CCMenu::create(play, end, NULL); menu->setAnchorPoint(ccp(0, 0)); menu->setPosition(ccp(0, 0)); this->addChild(menu); bRet = true; } while (0); return bRet; }
bool MainScene::Init() { CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(MainScene::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); this->addChild(pLabel, 1,1); CCSprite* pSprite = CCSprite::create("HelloWorld.png"); pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(pSprite, 0); setTouchEnabled(true); // /* Create an autorelease CCNodeLoaderLibrary. */ // CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); //// ccNodeLoaderLibrary->registerCCNodeLoader("MainScene", MainSceneLayerLoader::loader()); // /* Create an autorelease CCBReader. */ // cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary); // /* Read a ccbi file. */ // CCNode * node = ccbReader->readNodeGraphFromFile("MainScene.ccbi", this); // m_AnimationManager = ccbReader->getAnimationManager(); // m_AnimationManager->runAnimationsForSequenceNamed("default"); // m_bMenuShow = true; // ccbReader->release(); // if(node != NULL) { // this->addChild(node); // } // CCFilteredSpriteWithOne* pSpOne = CCFilteredSpriteWithOne::create("Icon-144.png"); // addChild(pSpOne); // pSpOne->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); // CCGrayFilter* pF = CCGrayFilter::create(ccc4f(0.2, 0.3, 0.5, 0.1)); // pSpOne->setFilter(pF); // // CCFilteredSpriteWithMulti* pSpMulti = CCFilteredSpriteWithMulti::create("Icon-144.png"); // addChild(pSpMulti); // pSpMulti->setPosition(ccp(visibleSize.width/2,visibleSize.height/4)); // CCHueFilter* pF1 = CCHueFilter::create(240); // CCSaturationFilter* pF2 = CCSaturationFilter::create(1.5); // CCBrightnessFilter* pF3 = CCBrightnessFilter::create(-0.4); // CCArray* pArray = CCArray::create(); // pArray->addObject(pF1); // pArray->addObject(pF2); // pArray->addObject(pF3); // pSpMulti->setFilters(pArray); // //test setBlendFunc // ccBlendFunc cbl = { GL_SRC_ALPHA , GL_ONE }; // CCRotateBy * ccRotateBy = CCRotateBy::create(20.0f, 360); // CCRepeatForever * ccRepeatForever = CCRepeatForever::create(ccRotateBy); // this->mBurstSprite->runAction(ccRepeatForever); // pSp->setBlendFunc(cbl); //test Shader Utils // ShaderUtils::alphaMask(pSp, "Icon-114.png"); // ShaderUtils::colorHSL(pSp, 120, 60); m_count = 0; // CCSprite *sprite = CCSprite::create("Icon-144.png"); // CCSize winSize = CCDirector::sharedDirector()->getWinSize(); // CCTableView* tableView = CCTableView::create(this,ccp(sprite->getContentSize().width*4,sprite->getContentSize().height*4)); // tableView->setDirection(kCCScrollViewDirectionVertical); // tableView->setPosition(ccp(winSize.width/3,winSize.height/3)); // tableView->setDelegate(this); // tableView->setVerticalFillOrder(kCCTableViewFillTopDown); // this->addChild(tableView,0,111); // tableView->reloadData(); // sprite->release(); //背景图片 CCSprite * background = CCSprite::create("HelloWorld.png"); background->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); this->addChild(background,0); //创建一个裁剪节点用来实现遮罩的效果 CCClippingNode * clippingNode = CCClippingNode::create(); this->addChild(clippingNode,1); //向裁剪节点中加入内容,这里加入的是一个透明的层 CCLayerColor * layer = CCLayerColor::create(ccc4(0,0,0,200)); clippingNode->addChild(layer); //继续向裁剪节点中加入内容,这里加入的是一个精灵 CCSprite * sprite = CCSprite::create("Icon-144.png"); sprite->setPosition(ccp(visibleSize.width/4,visibleSize.height/2)); clippingNode->addChild(sprite); //创建模板,裁剪节点将按照这个模板来裁剪区域 CCSprite * stencil = CCSprite::create("Icon-114.png"); stencil->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); clippingNode->setStencil(stencil); //向裁剪节点中加入精灵,精灵的位置和裁剪的位置相同,所以最后让裁剪掉了 CCSprite * sprite2 = CCSprite::create("Icon-144.png"); sprite2->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); clippingNode->addChild(sprite2); //这个是用来设置显示裁剪区域还是非裁剪区域的 clippingNode->setInverted(true); //我们之前放了一张裁剪的模板,按照这个模板裁剪的时候同时按照这个alpha的值裁剪,这个值的范围是0-1 //设为0就把透明的区域裁剪掉了 // clippingNode->setAlphaThreshold(0); return true; }
bool GameMenu::init() { if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //初始化背景 CCSprite* bg = CCSprite::create("bg.png"); bg->setScale(0.5); bg->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(bg, 0,0); //初始化背景星球 CCSprite*bgstar = CCSprite::create("moon.png"); bgstar->setAnchorPoint(ccp(0.5,0)); bgstar->setScale(0.5); bgstar->setPosition(ccp(size.width/3 * 2, 0)); this->addChild(bgstar,1,1); //初始化标题 CCNode *title = CCNode::create(); title->setContentSize(CCSizeMake(size.width - 40,50)); CCSprite *ptmLabel = CCSprite::create("meowstar.png"); ptmLabel->setScale(0.5); ptmLabel->setPosition( ccp(0,30) ); title->addChild(ptmLabel); CCSprite *ptbLabel = CCSprite::create("battle.png"); ptbLabel->setScale(0.5); ptbLabel->setPosition( ccp(0,-30) ); title->addChild(ptbLabel); title->setPosition(ccp(size.width / 2, size.height - 80)); this->addChild(title,2,2); //初始化按钮 CCMenuItemImage *newGameItem = CCMenuItemImage::create("newGameA.png", "newGameB.png",this,menu_selector(GameMenu::menuNewGameCallback)); newGameItem->setScale(0.5); newGameItem->setPosition(ccp(size.width / 2,size.height / 2 - 20)); newGameItem->setEnabled(false); CCMenuItemImage *continueItem = CCMenuItemImage::create("continueA.png", "continueB.png",this,menu_selector(GameMenu::menuContinueCallback)); continueItem->setScale(0.5); continueItem->setPosition(ccp(size.width / 2,size.height / 2 - 80)); continueItem->setEnabled(false); CCMenuItemImage *aboutItem = CCMenuItemImage::create("aboutA.png", "aboutB.png",this,menu_selector(GameMenu::menuAboutCallback)); aboutItem->setScale(0.5); aboutItem->setPosition(ccp(size.width / 2,size.height / 2 - 140)); aboutItem->setEnabled(false); soundItem = CCMenuItemImage::create("sound-on-A.png", "sound-on-B.png",this,menu_selector(GameMenu::menuSoundCallback)); soundItem->setScale(0.5); soundItem->setEnabled(false); soundItem->setPosition(ccp(40,40)); //使用按钮创建菜单 CCMenu* mainmenu = CCMenu::create(newGameItem,continueItem,aboutItem,soundItem,NULL); mainmenu->setPosition(ccp(0,0)); this->addChild(mainmenu,1,3); //初始化声音部分 issound = true;//是否开启声音参数 SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.mp3") ); SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5); SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.mp3")).c_str(), true); return true; }
// on "init" you need to initialize your instance bool HelloWorld::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 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); CCLOG("%.2f,%.2f\n",pMenu->getContentSize().width,pMenu->getContentSize().height); // add the label as a child to this layer this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayerColor::initWithColor( ccc4(255,255,255,255) ) ); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2, origin.y + pCloseItem->getContentSize().height/2)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); ///////////////////////////// // 2. add your codes below... CCSprite *player = CCSprite::create("Player.png", CCRectMake(0, 0, 27, 40) ); player->setPosition( ccp(origin.x + player->getContentSize().width/2, origin.y + visibleSize.height/2) ); this->addChild(player); this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 ); this->setTouchEnabled(true); _targets = new CCArray; _projectiles = new CCArray; // use updateGame instead of update, otherwise it will conflit with SelectorProtocol::update // see http://www.cocos2d-x.org/boards/6/topics/1478 this->schedule( schedule_selector(HelloWorld::updateGame) ); CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true); bRet = true; } while (0); return bRet; }
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; }
bool GameMenuScene::init() { if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); CCUserDefault*handler=CCUserDefault::sharedUserDefault(); issound=handler->getBoolForKey("issound",true); //playaudio=new PlayAudio(); // playaudio->bgSound(); CCSprite* bg = CCSprite::create("ui/menubg.jpg"); bg->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(bg, 0,0); //logo CCSprite*logo=CCSprite::create("ui/logo.png"); logo->setAnchorPoint(ccp(0.5,0.5)); this->addChild(logo,3); logo->setPosition(ccp(360,920)); //³õʼ»¯°´Å¥ CCMenuItemImage *newGameItem = CCMenuItemImage::create("ui/start1.png", "ui/start2.png.png",this,menu_selector(GameMenuScene::menuNewGameCallback)); //newGameItem->setScale(0.5); newGameItem->setPosition(ccp(size.width / 2,size.height / 2 - 20)); //newGameItem->setEnabled(false); CCMenuItemImage *continueItem = CCMenuItemImage::create("ui/score1.png", "ui/score2.png",this,menu_selector(GameMenuScene::menuScoreCallback)); //continueItem->setScale(0.5); continueItem->setPosition(ccp(size.width / 2,size.height / 2 - 195)); //continueItem->setEnabled(false); CCMenuItemImage *aboutItem = CCMenuItemImage::create("ui/about1.png", "ui/about2.png",this,menu_selector(GameMenuScene::menuScoreCallback)); //aboutItem->setScale(0.5); aboutItem->setPosition(ccp(size.width / 2,size.height / 2 - 280)); // aboutItem->setEnabled(false); soundItem = CCMenuItemImage::create("ui/ound1.png", "ui/sound2.png",this,menu_selector(GameMenuScene::menuSoundCallback)); //soundItem->setScale(0.5); soundItem->setEnabled(true); soundItem->setPosition(ccp(50,50)); if(issound){ soundItem->setNormalImage(CCSprite::create("ui/sound1.png")); //soundItem->setDisabledImage(CCSprite::create("sound1.png")); }else{ soundItem->setNormalImage(CCSprite::create("ui/sound2.png")); //soundItem->setDisabledImage(CCSprite::create("sound2.png")); } quitItem = CCMenuItemImage::create("ui/back1.png", "ui/back2.png",this,menu_selector(GameMenuScene::menuQuitCallback)); //soundItem->setScale(0.5); quitItem->setEnabled(false); quitItem->setPosition(ccp(670,50)); CCMenu* mainmenu = CCMenu::create(newGameItem,continueItem,soundItem,quitItem,NULL); mainmenu->setPosition(ccp(0,0)); this->addChild(mainmenu,1,3); issound = true; /*SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("background.mp3"); SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5); SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background.mp3", true); */ // this->addChild(pNode); CCMenuItemFont*menuitem1=CCMenuItemFont::create("menuitem1", this,menu_selector(GameMenuScene::menu1CallBack)); menuitem1->setPosition(ccp(300,300)); CCMenu*tempmenu=CCMenu::create(menuitem1,NULL); this->addChild(tempmenu); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } this->setTouchEnabled(true); ///////////////////////////// // 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 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition(MENU_POSITION_CLOSE ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); // create gen menu CCMenuItemImage *ptestItem1 = CCMenuItemImage::create("gen.png", "gen.png",this,menu_selector(HelloWorld::menuOfGen)); ptestItem1->setPosition(MENU_POSITION_GEN); CCMenu *ptestMenu1 = CCMenu::create(ptestItem1,NULL); ptestMenu1->setPosition(CCPointZero); this->addChild(ptestMenu1,5); ///////////////////////////// // 3. add your codes below... // create label of pokers _pRightPoker = CCLabelAtlas::create("0123456789", "bignumber.png", 36, 80, '0') ; _pLeftPoker = CCLabelAtlas::create("0123456789", "bignumber.png", 36, 80, '0') ; _pRightPoker->setPosition(LABEL_POSITION_RIGHTPOKER); _pLeftPoker->setPosition( LABEL_POSITION_LEFTPOKER); _pLeftPoker->setString(""); _pRightPoker->setString(""); this->addChild(_pRightPoker,10); this->addChild(_pLeftPoker,11); //create label of Wins _pWins = CCLabelTTF::create("0","Marker Felt",50); _pWins->setPosition(LABEL_POSITION_WINS); this->addChild(_pWins); //create label of mynumber _pMyNumberLabel = CCLabelAtlas::create("0123456789", "scorenumber.png", 34, 60, '0') ; _pMyNumberLabel->setPosition( LABEL_POSITION_MYNUMBER); _pMyNumberLabel->setString("98"); this->addChild(_pMyNumberLabel, 1); // add my number sprite CCSprite *pScore = CCSprite::create("scoreframe.png"); pScore->setPosition(SPRITE_POSITION_SCORE); pScore->setTag(SCORETAG); this->addChild(pScore); // add two poker sprites CardSprite *pPokerLeftSide = CardSprite::create("poker.png","poker.png",1,callfuncN_selector(HelloWorld::flipLeftCard),this); CardSprite *pPokerRightSide = CardSprite::create("poker.png","poker.png",1,callfuncN_selector(HelloWorld::flipRightCard),this); pPokerLeftSide->setPosition(SPRITE_POSITION_LEFTPOKER); pPokerRightSide->setPosition(SPRITE_POSITION_RIGHTPOKER); pPokerLeftSide->setTag(LEFTPOKERTAG); pPokerRightSide->setTag(RIGHTPOKERTAG); this->addChild(pPokerRightSide, 1); this->addChild(pPokerLeftSide, 2); // add get and ok item for action CCSprite *getItem = CCSprite::create("get.png"); getItem->setPosition(SPRITE_POSITION_GET); getItem->setTag(GETTAG); CCSprite *okItem = CCSprite::create("ok.png"); okItem->setPosition(SPRITE_POSITION_OK); okItem->setTag(OKTAG); this->addChild(getItem); this->addChild(okItem); return true; }
// on "init" you need to initialize your instance bool RectWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pPauseItem = CCMenuItemImage::itemFromNormalImage("pause.png","pause.png",this,menu_selector(RectWorld::menuPause)); CC_BREAK_IF(! pPauseItem); // Place the menu item bottom-right conner. pPauseItem->setPosition(ccp(22, 22)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::menuWithItems(pPauseItem, NULL); pMenu->setPosition(CCPointZero); pMenu->setTag(6); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 10); // Create a "close" menu item with close icon, it's an auto release object. // Create a menu with the "close" menu item, it's an auto release object. // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); // 3. Add add a splash screen, show the cocos2d splash image. // Place the sprite on the center of the screen MovingBackground *bk = MovingBackground::node(); this->addChild(bk); //CCParticleSystem *particle = ARCH_OPTIMAL_PARTICLE_SYSTEM::particleWithFile("tile.plist"); //particle->setPosition(ccp(_player->getContentSize().width / 2 + size.width / 5 * 3, size.height / 2)); //this->addChild(particle, 1); //this->schedule(schedule_selector(RectWorld::collision)); this->schedule(schedule_selector(RectWorld::gameLogic), 1.0); this->schedule(schedule_selector(RectWorld::update)); //CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true); if (_type == 3) { TimeLayer *timeLayer = TimeLayer :: node(); timeLayer->setInitalTime(60); timeLayer->setTag(4); this->addChild(timeLayer, 3); this->schedule(schedule_selector(RectWorld::clockPassBy), 1.0f); } bRet = true; } while (0); return bRet; }
// 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; }
TanSuoLayer::TanSuoLayer() { m_nFontSize = 24; m_nMaxCountDestroy = 3; m_bIsJieFeng = false; m_nSelectProId = -1; mBg = BaseSprite::create("xunbao_bg.png"); addChild(mBg); mBg->setPosition(ccp(mWinSize.width/2, mBg->getContentSize().height/2)); mMainMenu = CCMenu::create(); mBg->addChild(mMainMenu, 10); mMainMenu->setPosition(ccp(0,0)); CCMenuItemImage *yijian = CCMenuItemImage::create( "xunbao_anniu_yijianxunbao.png", "xunbao_anniu_yijianxunbao_select.png", this, menu_selector(TanSuoLayer::menuItemClicked_TanSuo)); mMainMenu->addChild(yijian, 0, Tag_MenuItem_YiJianTanSuo); yijian->setPosition(ccp(548,mBg->getContentSize().height-674)); Button *tansuo = Button::create(); tansuo->loadTextures("xunbao_anniu_xunbao.png","xunbao_anniu_xunbao_select.png","xunbao_anniu_xunbao_select.png",UI_TEX_TYPE_LOCAL); /*CCMenuItemImage *tansuo = CCMenuItemImage::create( "xunbao_anniu_xunbao.png", "xunbao_anniu_xunbao_select.png", this, menu_selector(TanSuoLayer::menuItemClicked_TanSuo));*/ tansuo->addTouchEventListener(this,SEL_TouchEvent(&TanSuoLayer::clickXunBao)); UILayer* layer = UILayer::create(); layer->ignoreAnchorPointForPosition(false); layer->setContentSize(tansuo->getContentSize()); layer->setAnchorPoint(ccp(0.5,0.5)); layer->addWidget(tansuo); layer->setPosition(320,mBg->getContentSize().height-674);//以前cpp(x,y) 现在左边 都是Point 比如颜色 ccWHITE 也改成了Color3B::WHITE mBg->addChild(layer); tansuo->setPosition(ccp(tansuo->getContentSize().width / 2 , tansuo->getContentSize().height / 2)); m_sXunBaoSpr = CCSprite::create("xunbao_anniu_xunbao_xiaoguo.png"); m_sXunBaoSpr->setPosition(ccp(320,mBg->getContentSize().height-674)); mBg->addChild(m_sXunBaoSpr); m_sXunBaoSpr->runAction(CCRepeatForever::create(CCSequence::create(CCFadeOut::create(1.0f),CCFadeIn::create(1.0f),NULL))); //mMainMenu->addChild(tansuo, 0, Tag_MenuItem_TanSuo); //tansuo->setPosition(ccp(320,mBg->getContentSize().height-674)); { CCMenuItemImage *jiefen = CCMenuItemImage::create( "xunbao_anniu_jiechufengyin.png", "xunbao_anniu_jiechufengyin_select.png", this, menu_selector(TanSuoLayer::menuItemClicked_TanSuo)); mMainMenu->addChild(jiefen, 0, Tag_MenuItem_JieFen); jiefen->setPosition(ccp(320,mBg->getContentSize().height-437)); jiefen->setVisible(false); CCSprite *img = CCSprite::create("xunbao_anniu_jiechufengyin_xiaoguo.png"); jiefen->addChild(img); img->setPosition(ccp(jiefen->getContentSize().width/2, jiefen->getContentSize().height/2)); img->runAction(CCRepeatForever::create(CCSequence::create( CCFadeTo::create(1, 50), CCFadeTo::create(1, 255), NULL))); } mMainMenu->setVisible(false); //title CCLabelTTF *tilte = CCLabelTTF::create(LFStrings::getValue("XunBaoShuoMing").c_str(), fontStr_kaiti, m_nFontSize); mBg->addChild(tilte); tilte->setColor(fonColor_PuTong); tilte->setPosition(ccp(mBg->getContentSize().width/2, mBg->getContentSize().height-110)); //2 CCLabelTTF *la1 = CCLabelTTF::create(LFStrings::getValue("XunBaoJieFeng").c_str(), fontStr_kaiti, m_nFontSize); mBg->addChild(la1); la1->setAnchorPoint(ccp(0, 0.5)); la1->setColor(fonColor_CiYao); la1->setPosition(ccp(134, mBg->getContentSize().height-140)); CCLabelTTF *la2 = CCLabelTTF::create((LFStrings::getValue("shenbing")+LFStrings::getValue("zhuangbei")).c_str() , fontStr_kaiti, m_nFontSize); la1->addChild(la2); la2->setColor(ImageManager::getManager()->getShowColor(ZhuangbeiColour_Chuanqi, false, false)); la2->setPosition(ccp(la1->getContentSize().width+la2->getContentSize().width/2, la1->getContentSize().height/2)); la1->setPositionX((mBg->getContentSize().width-la1->getContentSize().width-la2->getContentSize().width)/2); }
// on "init" you need to initialize your instance bool LevelSelectionLayer::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(LevelSelectionLayer::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World". CCLabelTTF* pLabel = CCLabelTTF::create("Level Selection", "Arial", 24); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 50)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel, 1); // 3. Add add a splash screen, show the cocos2d splash image. CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen pSprite->setPosition(ccp(size.width/2, size.height/2)); // Add the sprite to HelloWorld layer as a child layer. //this->addChild(pSprite, 0); //add all of the level stuff CCDictionary* plistDictionary = CCDictionary::createWithContentsOfFile("LevelData.plist"); CCArray* stagesArray = (CCArray*)plistDictionary->objectForKey("STAGES"); CCDictionary* stageData = (CCDictionary*)stagesArray->objectAtIndex(0); CCArray* levelArray = (CCArray*) stageData->objectForKey("LEVELS"); int numLevels = levelArray->count(); //how far to space the buttons CCSize stageButtonSize = CCSizeMake(30, 30); CCMenu* menu = CCMenu::create(); for(int levelIndex = 0; levelIndex < numLevels;levelIndex++) { CCString* test = CCString::createWithFormat("%d",levelIndex); CCLabelTTF* stageLabel1 = CCLabelTTF::create(test->getCString(), FONT_NAME, STAGE_FONTSIZE); CCLabelTTF* stageLabel2 = CCLabelTTF::create("OH HAI DER", FONT_NAME, STAGE_FONTSIZE); CCMenuItemSprite* stageLabel = CCMenuItemSprite::create(stageLabel1, stageLabel2, this, menu_selector(LevelSelectionLayer::levelClickCallback)); float posX = (stageButtonSize.width*1.05*(-0.5 + 1*levelIndex%2)); float posY = (stageButtonSize.height*1.05*( 0.5-levelIndex/2)); stageLabel->setPosition(ccp(posX, posY)); //set the tag to be the stage index menu->addChild(stageLabel, 0, levelIndex); } this->addChild(menu); bRet = true; } while (0); return bRet; }
bool CMainSelect::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } this->setTouchEnabled(true); // 创建背景 CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite* backgroud = CCSprite::create("mainsel/bg.png"); backgroud->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(backgroud); // 按钮 CCMenuItemImage *pLack = CCMenuItemImage::create("mainsel/lackNor.png", "mainsel/lackSel.png", this, menu_selector(CMainSelect::lakeCallback) ); CCMenuItemImage *pForest = CCMenuItemImage::create("mainsel/forestNor.png", "mainsel/forestSel.png", this, menu_selector(CMainSelect::forestCallback) ); CCMenuItemImage *pDesert = CCMenuItemImage::create("mainsel/desertNor.png", "mainsel/desertSel.png", this, menu_selector(CMainSelect::desertCallback) ); CCMenuItemImage *pPrairie = CCMenuItemImage::create("mainsel/prairieNor.png", "mainsel/prairieSel.png", this, menu_selector(CMainSelect::prairieCallback) ); pLack->setPosition( ccp(171, 437) ); pForest->setPosition( ccp(539, 401) ); pDesert->setPosition( ccp(864, 411) ); pPrairie->setPosition( ccp(392, 208) ); CCMenu* pMenu = CCMenu::create(pLack, pForest, pDesert, pPrairie, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); // 创建sprite sheet CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("mainsel/jingyu.plist"); CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("mainsel/jingyu.png"); this->addChild(spriteSheet); // 创建对象 CCSprite *sprite = CCSprite::createWithSpriteFrameName("jingyu01.png"); sprite->setPosition(ccp(920,220)); spriteSheet->addChild(sprite, 0); CCArray *arrShang = CCArray::create(); // 动画帧数组 for(int i=1; i<=8; ++i) { CCString *name = CCString::createWithFormat("jingyu%02d.png", i); arrShang->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name->getCString())); } CCArray *arrPeng = CCArray::create(); // 动画帧数组 for(int i=9; i<=11; ++i) { CCString *name = CCString::createWithFormat("jingyu%02d.png", i); arrPeng->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name->getCString())); } CCArray *arrPengRev = CCArray::createWithArray(arrPeng); arrPengRev->reverseObjects(); arrPeng->addObjectsFromArray(arrPengRev); CCFiniteTimeAction *delay = CCDelayTime::create(1); CCAnimate* anShang = CCAnimate::create(CCAnimation::createWithSpriteFrames(arrShang, 0.25)); CCAnimate* anPeng = CCAnimate::create(CCAnimation::createWithSpriteFrames(arrPeng, 0.15)); sprite->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create(anShang, delay, anPeng,anPeng, anPeng, anPeng, anPeng, anShang->reverse(),delay,delay,delay,NULL))); return true; }