bool ChoosePackageScene::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } setKeypadEnabled(true); CCDirector *director = CCDirector::sharedDirector(); LayoutManager *layoutManager = LayoutManager::getInstance(); CCRect visibleRect = layoutManager->getVisibleRect(); m_Background = CCSprite::create("bg_common.jpg"); m_Background->retain(); m_Background->setPosition(director->getVisibleOrigin() + director->getVisibleSize() / 2); addChild(m_Background); CCSprite *sprite = CCSprite::create("stage_select.png"); sprite->setPosition(ccp(visibleRect.origin.x + visibleRect.size.width / 2, 630)); addChild(sprite); // Create the page view int pageViewHeight = 556; int pageWidth = 336; LGPageView *pageView = LGPageView::create(); pageView->setTouchEnabled(true); pageView->setSize(CCSizeMake(visibleRect.size.width, pageViewHeight)); pageView->setPosition(CCSizeMake(visibleRect.origin.x, + visibleRect.origin.y + (visibleRect.size.height - pageViewHeight) / 2)); pageView->setPageWidth(pageWidth); pageView->setIndicatorEnable(true); StageManager *stageManager = StageManager::getInstance(); for (int i = 0; i < sizeof(PACKAGE_IMAGE) / sizeof(PACKAGE_IMAGE[0]); ++i) { Layout *layout = Layout::create(); layout->setSize(CCSizeMake(pageWidth, pageView->getSize().height)); UIImageView *imageView = UIImageView::create(); imageView->loadTexture(PACKAGE_IMAGE[i]); imageView->setPosition(ccp(layout->getRect().size.width / 2, layout->getRect().size.height / 2)); imageView->setTag(i); imageView->addReleaseEvent(this, coco_releaseselector(ChoosePackageScene::selectPackageCallback)); imageView->setTouchEnabled(true); layout->addChild(imageView); ccFontDefinition strokeTextDef; strokeTextDef.m_fontSize = 100; #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) strokeTextDef.m_fontName = "Lithograph"; #else strokeTextDef.m_fontName = "fonts/Lithograph.ttf"; #endif strokeTextDef.m_stroke.m_strokeEnabled = true; strokeTextDef.m_stroke.m_strokeColor = LayoutManager::getInstance()->DefaultStroke; strokeTextDef.m_stroke.m_strokeSize = 30; strokeTextDef.m_fontFillColor = ccWHITE; CCLabelTTF *labelStage = CCStrokeLabel::createWithFontDefinition(CCString::createWithFormat(" %d ", (i + 1))->getCString(), strokeTextDef); labelStage->setPosition(ccp(layout->getRect().size.width / 2, layout->getRect().size.height / 2)); layout->addCCNode(labelStage); strokeTextDef.m_fontSize = 40; strokeTextDef.m_stroke.m_strokeSize = 16; CCLabelTTF *labelCompletion = CCStrokeLabel::createWithFontDefinition(CCString::createWithFormat(" %d/%d ", stageManager->getCompletedStageCount(i), stageManager->getStageCount(i))->getCString(), strokeTextDef); labelCompletion->setPosition(ccp(layout->getRect().size.width / 2, layout->getRect().size.height / 2 - labelStage->getContentSize().height / 2 - labelCompletion->getContentSize().height / 2)); layout->addCCNode(labelCompletion); m_LabelCompletions->addObject(labelCompletion); pageView->addPage(layout); } UILayer *pUiLayer = UILayer::create(); pUiLayer->addWidget(pageView); addChild(pUiLayer); pUiLayer->scheduleUpdate(); CCMenuItemImage *pBack = CCMenuItemImage::create("back.png", NULL, this, menu_selector(ChoosePackageScene::backCallback)); pBack->setAnchorPoint(CCPointZero); pBack->setPosition(ccp(visibleRect.origin.x + 18, visibleRect.origin.y + 12)); CCMenu *pMenu = CCMenu::create(pBack, NULL); pMenu->setAnchorPoint(CCPointZero); pMenu->setPosition(CCPointZero); addChild(pMenu); return true; }
// 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::itemWithNormalImage( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::menuWithItems(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::labelWithString("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); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::spriteWithFile("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); AnalyticX::flurrySetAppVersion("v_1_97"); cocos2d::CCLog("--->>>get flurry version = %s", AnalyticX::flurryGetFlurryAgentVersion()); AnalyticX::flurrySetDebugLogEnabled(false); AnalyticX::flurrySetSessionContinueSeconds(143); AnalyticX::flurrySetSecureTransportEnabled(false); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) AnalyticX::flurryStartSession("QFNXVFK2XX4P56GS76EA"); #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) AnalyticX::flurryStartSession("W7IBK43RJCHPT4IRP4HI"); #endif AnalyticX::flurrySetUserID("fake_user_id"); AnalyticX::flurrySetAge(34); AnalyticX::flurrySetGender("f"); AnalyticX::flurrySetReportLocation(false); AnalyticX::flurryLogEvent("event_3"); AnalyticX::flurryLogEventTimed(" log event timed test...", false); CCDictionary *testDict = new CCDictionary(); CCString *testCCString; testCCString = CCString::stringWithCString("obj 0"); testDict->setObject(testCCString, "key 0"); testCCString = CCString::stringWithCString("obj 1"); testDict->setObject(testCCString, "key 1"); AnalyticX::flurryLogEventWithParameters("event_5_with_params_no_timed", testDict); AnalyticX::flurryLogEventWithParametersTimed("test flurryLogEventWithParameters + timed", testDict, true); AnalyticX::flurryEndTimedEventWithParameters("test end event...", NULL); AnalyticX::flurryLogPageView(); //AnalyticX::flurryEndSession(); return true; }
bool CGameMessage::init() { bool bRet = false; do{ CC_BREAK_IF(! CCLayer::init()); CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Image/Message.plist"); /** 信息框 */ CCSprite *pBox = CCSprite::createWithSpriteFrameName("Box.png"); CC_BREAK_IF(! pBox); pBox->setPosition(ccp(size.width/2, size.height - pBox->getContentSize().height/2)); this->addChild(pBox, 0); /** 分数 */ m_pScore = CCLabelAtlas::create("0", "Image/Num.plist"); CC_BREAK_IF(! m_pScore); this->addChild(m_pScore, 0); m_pScore->setPosition(ccp(100, size.height - 24) ); /** 生命 */ m_pLife = CCLabelAtlas::create("3", "Image/Num.plist"); CC_BREAK_IF(! m_pLife); this->addChild(m_pLife, 0); m_pLife->setPosition(ccp(236, size.height - 20) ); /** HP */ m_pHp = CCSprite::createWithSpriteFrameName("Hp.png"); CC_BREAK_IF(! m_pHp); this->addChild(m_pHp, 0); m_pHp->setAnchorPoint(ccp(0, 0)); m_iWidth = m_pHp->getContentSize().width; m_iHeight = m_pHp->getContentSize().height; m_pHp->setPosition(ccp(97, size.height - 49)); m_fMinX = m_pHp->getTextureRect().getMinX(); m_fMinY = m_pHp->getTextureRect().getMinY(); // 导弹图标 m_pMissileItem = CCMenuItemImage::create(); CC_BREAK_IF(! m_pMissileItem); m_pMissileItem->setCallback( CC_CALLBACK_1( CGameMessage::shootMissile, this ) ); m_pMissileItem->setNormalSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("MissileIco.png")); m_pMissileItem->setSelectedSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("MissileIco.png")); m_pMissileItem->setPosition(ccp(15, 80)); CCMenu *pMissile = CCMenu::create(m_pMissileItem, NULL); CC_BREAK_IF(! pMissile); pMissile->setPosition(ccp(0, 0)); this->addChild(pMissile, 0); m_pMissileNum = CCLabelAtlas::create("2", "Image/Num.plist"); CC_BREAK_IF(! m_pMissileNum); m_pMissileItem->addChild(m_pMissileNum, 0); this->scheduleUpdate(); bRet = true; } while(0); return bRet; }
ControlLayer::ControlLayer( LayerSwitcher* pLayerSwitcher,MapControl* pControl ) { m_pLayerSwitcher = pLayerSwitcher; m_pControl = pControl; 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 *pZoomIn = CCMenuItemImage::create( "zoomin.png", "zoomin.png", this, menu_selector(ControlLayer::zoomIn)); CC_BREAK_IF(! pZoomIn); // Place the menu item bottom-right conner. pZoomIn->setPosition(ccp(25, 25)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::create(pZoomIn, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu, 1,1); // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pZoomOut = CCMenuItemImage::create( "zoomout.png", "zoomout.png", this, menu_selector(ControlLayer::zoomOut)); CC_BREAK_IF(! pZoomOut); // Place the menu item bottom-right conner. pZoomOut->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 25, 25)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu2 = CCMenu::create(pZoomOut, NULL); pMenu2->setPosition(CCPointZero); CC_BREAK_IF(! pMenu2); this->addChild(pMenu2,2,2); // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pLayerSwitch = CCMenuItemImage::create( "r_overlays.png", "r_overlays.png", this, menu_selector(ControlLayer::layerswitch)); CC_BREAK_IF(! pLayerSwitch); // Place the menu item bottom-right conner. pLayerSwitch->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2, CCDirector::sharedDirector()->getWinSize().height-25)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu3 = CCMenu::create(pLayerSwitch, NULL); pMenu3->setPosition(CCPointZero); CC_BREAK_IF(! pMenu3); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu3, 3,3); m_ScaleList.reserve(19); for (int i=0;i<19;++i) { CCString str; str.initWithFormat("scale%d.png",i+1); CCMenuItemImage *pScaleItem = CCMenuItemImage::create(str.getCString(),str.getCString()); CC_BREAK_IF(! pScaleItem); // Place the menu item bottom-right conner. pScaleItem->setPosition(ccp(100, 35)); // Create a menu with the "close" menu item, it's an auto release object. CCMenu* pScaleMenu = CCMenu::create(pScaleItem, NULL); pScaleMenu->setPosition(CCPointZero); pScaleMenu->retain(); m_ScaleList.push_back(pScaleMenu); } int zoom = pControl->getZoomLevel(); addChild(m_ScaleList[zoom-1],4,4); } while (0); }
// 初始化顶部bar void MessageView::initTopBar(CCSize winSize) { //////////////////////////////////////////////////////////////////////////////////////////////////// // topBarNode 背景 CCSprite* topBarBg = CCSprite::create("bg_nav_top.png") ; topBarBg->setAnchorPoint(ccp(0.5, 0)) ; topBarBg->setPosition(ccp(winSize.width/2, 0)) ; const float topBar_height = topBarBg->getContentSize().height ; // 顶部条信息 ////////////////////////////////////////////////////////////////////////////////////// this->topBarNode = CCNode::create() ; this->topBarNode->setContentSize(CCSizeMake(winSize.width, topBar_height)) ; this->topBarNode->setAnchorPoint(ccp(0, 0)) ; this->topBarNode->setPosition(ccp(0, winSize.height - topBar_height)) ; this->addChild(this->topBarNode, 0, 1) ; // 将topBarNode背景加入到topBarNode this->topBarNode->addChild(topBarBg, 0 ) ; ///////////////////////////// 左侧布局 ////////////////////////// CCNode* leftNode = CCNode::create() ; leftNode->setContentSize(CCSizeMake(300, topBar_height)) ; leftNode->setAnchorPoint(ccp(0, 0.5)) ; leftNode->setPosition(ccp(30, topBar_height/2)) ; this->topBarNode->addChild(leftNode, 1) ; // 头像 CCSprite* headPortraitSprite = CCSprite::create("user/head_default_f2.png") ; headPortraitSprite->setAnchorPoint(ccp(0, 0)) ; headPortraitSprite->setPosition(ccp(0, 10)) ; headPortraitSprite->setScale(0.8) ; leftNode->addChild(headPortraitSprite) ; // 昵称 CCLabelTTF* nikeNameLabel = CCLabelTTF::create("昵称:我是主角", "font01.ttf", 22.0f) ; // nikeNameLabel->setColor(ccc3(191, 173, 111)) ; nikeNameLabel->setAnchorPoint(ccp(0, 0)) ; nikeNameLabel->setPosition(ccp(70, topBar_height/2+2)) ; nikeNameLabel->setHorizontalAlignment(kCCTextAlignmentLeft) ; // 水平对齐方式 nikeNameLabel->setVerticalAlignment(kCCVerticalTextAlignmentCenter) ; // 垂直对齐方式 leftNode->addChild(nikeNameLabel) ; // 金币 CCLabelTTF* goldCoinLabel = CCLabelTTF::create("金币:10000", "font01.ttf", 22.0f) ; goldCoinLabel->setAnchorPoint(ccp(0, 1)) ; goldCoinLabel->setPosition(ccp(70, topBar_height/2)) ; goldCoinLabel->setHorizontalAlignment(kCCTextAlignmentLeft) ; // 水平对齐方式 goldCoinLabel->setVerticalAlignment(kCCVerticalTextAlignmentCenter) ; // 垂直对齐方式 leftNode->addChild(goldCoinLabel) ; // 信息 CCSprite* titleMsg = CCSprite::create("icon_title_message.png") ; titleMsg->setAnchorPoint(ccp(0.5, 0.5)) ; titleMsg->setPosition(ccp(winSize.width/2, topBar_height/2)) ; this->topBarNode->addChild(titleMsg, 1) ; // 分割线 CCSprite* spliteLineLeft = CCSprite::create("icon_top_nav_splite.png") ; spliteLineLeft->setAnchorPoint(ccp(0, 1)) ; spliteLineLeft->setPosition(ccp(winSize.width/3+10, topBar_height)) ; this->topBarNode->addChild(spliteLineLeft, 1) ; // 分割线 CCSprite* spliteLineRight = CCSprite::create("icon_top_nav_splite.png") ; spliteLineRight->setAnchorPoint(ccp(0, 1)) ; spliteLineRight->setPosition(ccp(winSize.width*2/3-20, topBar_height)) ; this->topBarNode->addChild(spliteLineRight, 1) ; ///////////////////////////////////////////////////////////////////////////////////////////////// /****************************** 菜单选项 ********************************************************/ // 右侧menu const float right_width = 200 ; const float item_width = 55 ; // 礼物 CCMenuItemImage* menuGifts = CCMenuItemImage::create("menu_gifts_n.png", "menu_gifts_s.png", this, menu_selector(MessageView::menuGiftsClickCallback)) ; // menuBack->setContentSize(CCSizeMake(67, 68)) ; menuGifts->setAnchorPoint(ccp(0, 0.5)) ; menuGifts->setPosition(ccp(0, topBar_height/2)) ; //menuGifts->setPosition(ccp(right_width-2*item_width, topBar_height/2)) ; //rightMenu->addChild(menuGifts) ; // 返回大厅 CCMenuItemImage* menuBack = CCMenuItemImage::create("menu_back_n.png", "menu_back_s.png", this, menu_selector(MessageView::menuBackClickCallback)) ; menuBack->setAnchorPoint(ccp(0, 0.5)) ; menuBack->setPosition(ccp(2*(item_width+10), topBar_height/2)) ; //menuBack->setPosition(ccp(right_width, topBar_height/2)) ; //rightMenu->addChild(menuBack) ; // 设置 CCMenuItemImage* menuSetting = CCMenuItemImage::create("menu_setting_n.png", "menu_setting_s.png", this, menu_selector(MessageView::menuSettingClickCallback)) ; // menuBack->setContentSize(CCSizeMake(67, 68)) ; menuSetting->setAnchorPoint(ccp(0, 0.5)) ; menuSetting->setPosition(ccp(item_width+10, topBar_height/2)) ; //menuSetting->setPosition(ccp(right_width-item_width, topBar_height/2)) ; //rightMenu->addChild(menuSetting) ; CCMenu* rightMenu = CCMenu::create(menuGifts, menuBack, menuSetting, NULL) ; rightMenu->setContentSize(CCSizeMake(winSize.width/4, topBar_height)) ; rightMenu->setAnchorPoint(ccp(0, 0)) ; rightMenu->setPosition(ccp(winSize.width*3/4-10, 0)) ; this->topBarNode->addChild(rightMenu, 1) ; }
void PlayMenu::setupGUI() { // this->schedule(schedule_selector(PlayMenu::timerForLifeCheck),1/60); { CCLabelTTF* timerLabel= CCLabelTTF::create(NEXT_LIFE_IN, GAME_LABEL_FONT_NATHAN, 29); // coinsLabel->setColor(ccc3(87, 0, 5)); timerLabel->setPosition(ccp(winSize.width/2-170,winSize.height/2+430)); timerLabel->setColor(ccBLUE); timerLabel->setTag(KTAG_PLYMENU_COINSLBL); // this->addChild(timerLabel,11-KTAG_LAYERING_PARAM); } timerForLife= CCLabelTTF::create("", GAME_LABEL_FONT_NATHAN, 29); // coinsLabel->setColor(ccc3(87, 0, 5)); timerForLife->setPosition(ccp(winSize.width/2-20,winSize.height/2+430)); timerForLife->setColor(ccBLACK); timerForLife->setTag(KTAG_PLYMENU_COINSLBL); // this->addChild(timerForLife,11-KTAG_LAYERING_PARAM); /// Total Life Left // { // CCLabelTTF* TotalLifeLabel= CCLabelTTF::create("", GAME_LABEL_FONT_NATHAN, 29); // // coinsLabel->setColor(ccc3(87, 0, 5)); // TotalLifeLabel->setPosition(ccp(winSize.width-120,winSize.height/2+430)); // TotalLifeLabel->setColor(ccBLUE); // TotalLifeLabel->setTag(KTAG_Life_Count); // this->addChild(TotalLifeLabel,11-KTAG_LAYERING_PARAM); // string TotalLifeCount = static_cast<ostringstream*>( &(ostringstream() << GAME_SHARED_HANDLER->getLives()) )->str(); // TotalLifeCount="Life Count " + TotalLifeCount; // TotalLifeLabel->setString(TotalLifeCount.c_str()); // CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(PlayMenu::updateLifeCountLabel),N_LIFE_COUNT_UPDATED,NULL); // } //add bg. CCSprite* bckg = CCSprite::create(BACKGROUND); bckg->setPosition( ccp(winSize.width/2,winSize.height/2) ); CCLOG("win size %f ",winSize.width); this->addChild(bckg, 0-KTAG_LAYERING_PARAM); // CCSprite *logobg = CCSprite::create(LOGO); // logobg->setPosition(ccp(winSize.width/2, winSize.height/2+(155))); // this->addChild(logobg); CCMenuItemImage *playMenuItem = CCMenuItemImage::create(PLAY_IMAGE, PLAY_IMAGE_PRESSED, this,menu_selector(PlayMenu::play)); // facebookMenuItem = CCMenuItemImage::create(FACEBOOK_LOGIN_IMAGE, FACEBOOK_LOGIN_IMAGE_PRESSED, this,menu_selector(PlayMenu::facebookConnect)); CCMenu *menu = CCMenu::create(playMenuItem,NULL); menu->setPosition(ccp(0, 0)); playMenuItem->setPosition(ccp(winSize.width/2, winSize.height/2-(275))); // facebookMenuItem->setPosition(ccp(winSize.width/2, winSize.height/2-(325))); this->setFBStatus(); CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(PlayMenu::setFBStatus),N_USER_LOG_STATUS_CHANGED,NULL); this->checkAndAddRequestsMenu(); CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(PlayMenu::checkAndAddRequestsMenu),N_REQUESTS_DATA_UPDATED,NULL); this->addChild(menu); }
// 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 char fontName[13] = {"airborne.ttf"}; char myText[21] = {"Rendering"}; CCLabelTTF* pLabel = CCLabelTTF::create(myText, fontName, 24); // position the label on the center of the screen pLabel->setPosition(ccp(350,50)); pLabel->setColor(cocos2d::ccRED); // add the label as a child to this layer this->addChild(pLabel, 1); this->setTouchEnabled(true); /*this->schedule( schedule_selector(handleUserTouch) );*/ CCSprite* pSprite = CCSprite::create("bg.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); //The code starts here.. all the assets are done by me and plist file and sprite file was generated with sprite packer. CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("SpriteColl1.plist"); CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("SpriteColl1.png"); this->addChild(spriteSheet); CCSprite *sprite = CCSprite::createWithSpriteFrameName("Sprite1.png"); this->addChild(sprite); sprite->setPosition(ccp(400,200)); CCArray *moveFrames = CCArray::createWithCapacity(15); char str[100] = {0}; for (int i = 1; i <= 4; i++) { sprintf(str, "Sprite%d.png", i); CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str); moveFrames->addObject(pFrame); } CCAnimation *animation = CCAnimation::createWithSpriteFrames(moveFrames, 0.3f); // 14 frames * 1sec = 14 seconds sprite->runAction(CCRepeatForever::create(CCAnimate::create(animation))); this->loadController(); return true; }
KDbool MenuLayer::init ( KDvoid ) { if ( !CCLayer::init ( ) ) { return KD_FALSE; } m_nIndexText = 0; CCSprite* pBackground = CCSprite::create ( "Image 816 at frame 10.jpg" ); pBackground->setScale ( IOSSCALE ); pBackground->setPosition ( ccpMid ( m_tContentSize ) ); this->addChild ( pBackground ); CCMenuItemFont::setFontName ( "Arial Bold.ttf" ); CCMenuItemFont::setFontSize ( 18 ); CCMenuItem* pItemAchievement = CCMenuItemFont::create ( "Achievement" ); CCMenuItem* pItemLeaderboard = CCMenuItemFont::create ( "Leaderboard" ); CCMenuItem* pItemGetProps = CCMenuItemFont::create ( "GetProps" ); CCMenuItem* pItemPlay = CCMenuItemImage::create ( "Image 822 at frame 10.jpg", "Image 823 alpha channel at frame 10.png", this, menu_selector ( MenuLayer::playGame ) ); CCMenu* pMenu = CCMenu::create ( pItemAchievement, pItemLeaderboard, pItemGetProps, pItemPlay, KD_NULL ); pMenu->alignItemsHorizontallyWithPadding ( 18 ); pMenu->setPosition ( m_tContentSize.cx / 2, m_tContentSize.cy / 2 - 50 ); this->addChild ( pMenu ); m_pTelScope = TeleScope::create ( ); m_pTelScope->setPosition ( ccp ( 80, 60 ) ); m_pTelScope->setOpacity ( 0 ); pBackground->addChild ( m_pTelScope, 1 ); m_aTexts [ 0 ] = CCSprite::create ( "Image 817 at frame 10.jpg" ); m_aTexts [ 1 ] = CCSprite::create ( "Image 818 at frame 10.jpg" ); m_aTexts [ 2 ] = CCSprite::create ( "Image 819 at frame 10.jpg" ); m_aTexts [ 3 ] = CCSprite::create ( "Image 820 at frame 10.jpg" ); m_aTexts [ 0 ]->setPosition ( ccp ( 156 / 2 , 264 / 2 ) ); m_aTexts [ 1 ]->setPosition ( ccp ( 156 / 2 + 40, 264 / 2 ) ); m_aTexts [ 2 ]->setPosition ( ccp ( 156 / 2 + 80, 264 / 2 ) ); m_aTexts [ 3 ]->setPosition ( ccp ( 156 / 2 + 120, 264 / 2 ) ); m_aTexts [ 0 ]->setOpacity ( 0 ); m_aTexts [ 1 ]->setOpacity ( 0 ); m_aTexts [ 2 ]->setOpacity ( 0 ); m_aTexts [ 3 ]->setOpacity ( 0 ); pBackground->addChild ( m_aTexts [ 0 ] ); pBackground->addChild ( m_aTexts [ 1 ] ); pBackground->addChild ( m_aTexts [ 2 ] ); pBackground->addChild ( m_aTexts [ 3 ] ); m_aHoles [ 0 ] = CCSprite::create ( "Image 824 at frame 10.jpg" ); m_aHoles [ 1 ] = CCSprite::create ( "Image 824 at frame 10.jpg" ); m_aHoles [ 2 ] = CCSprite::create ( "Image 824 at frame 10.jpg" ); m_aHoles [ 3 ] = CCSprite::create ( "Image 824 at frame 10.jpg" ); m_aHoles [ 0 ]->setOpacity ( 0 ); m_aHoles [ 1 ]->setOpacity ( 0 ); m_aHoles [ 2 ]->setOpacity ( 0 ); m_aHoles [ 3 ]->setOpacity ( 0 ); m_aHoles [ 0 ]->setPosition ( ccp ( 58, 142 ) ); m_aHoles [ 1 ]->setPosition ( ccp ( 88, 112 ) ); m_aHoles [ 2 ]->setPosition ( ccp ( 226, 105 ) ); m_aHoles [ 3 ]->setPosition ( ccp ( 178, 132 ) ); pBackground->addChild ( m_aHoles [ 0 ] ); pBackground->addChild ( m_aHoles [ 1 ] ); pBackground->addChild ( m_aHoles [ 2 ] ); pBackground->addChild ( m_aHoles [ 3 ] ); SimpleAudioEngine::sharedEngine ( )->preloadEffect ( "Sound 1 at frame 10.mp3" ); SimpleAudioEngine::sharedEngine ( )->preloadEffect ( "Sound 5 at frame 10.mp3" ); SimpleAudioEngine::sharedEngine ( )->preloadEffect ( "Sound 3 at frame 10.mp3" ); return KD_TRUE; }
bool OverMenu::init(CCNode* layer_) { if(!CCLayer::init()){ return false; } layer=layer_; layer->addChild(this,50); this->schedule(schedule_selector(OverMenu::tick)); int score=((GameLayer*)layer)->gamebody->score; if(((GameLayer*)layer)->gamebody->game_mode==MODE_SHUANGZI){ score=((GameLayer*)layer)->gamebody->score+((GameLayer*)layer)->gamebody2->score; } bool ishigh=false; int modeid=CCUserDefault::sharedUserDefault()->getIntegerForKey("gamemode"); char modehighscore[200]; sprintf(modehighscore, "highscore%d",modeid); int highscore=CCUserDefault::sharedUserDefault()->getIntegerForKey(modehighscore); if(score>highscore){ highscore=score; CCUserDefault::sharedUserDefault()->setIntegerForKey(modehighscore, highscore); CCUserDefault::sharedUserDefault()->flush(); ishigh=true; CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sound/newscore.mp3", false); } { CCMenuItemImage *button = CCMenuItemImage::create( "game/black.jpg", "game/black.jpg", this, NULL ); CCMenu* Menu = CCMenu::create(button, NULL); Menu->setPosition(CCPointZero); this->addChild(Menu,-1); button->setAnchorPoint(ccp(0.5f,0)); button->setPosition(ccp(screenSize.width*0.5f,0)); button->setOpacity(150); } { pane=CCSprite::create("over/back.png"); this->addChild(pane,1); pane->setScale(0.3f*screenSize.height/pane->getContentSize().height); pane->setPosition(ccpPercent(50, 155)); { CCMoveTo* a1=CCMoveTo::create(0.4f, ccpPercent(50, 60)); pane->runAction(a1); } { CCLabelTTF* word = CCLabelTTF::create("游戏结束", "ttf/word.ttf", pane->getContentSize().height*0.2f); word->setAnchorPoint(ccp(0.5f,0.5f)); pane->addChild(word, 10); word->setPosition(ccpContent(pane->getContentSize(), 50, 120)); word->setColor(ccGRAY); } { char* name; GAME_MODE gamemode=((GameLayer*)layer)->game_mode; char* gamecenterid; if(gamemode==MODE_WANMEI){ name=(char*)"完美模式"; gamecenterid=(char*)"com.adultdream.mengmengda.wanmei"; } if(gamemode==MODE_SHANDIAN){ name=(char*)"闪电模式"; gamecenterid=(char*)"com.adultdream.mengmengda.shandian"; } if(gamemode==MODE_NIUZHUAN){ name=(char*)"扭转模式"; gamecenterid=(char*)"com.adultdream.mengmengda.niuzhuan"; } if(gamemode==MODE_SHUANGZI){ name=(char*)"双子模式"; gamecenterid=(char*)"com.adultdream.mengmengda.shuangzi"; } if(gamemode==MODE_XIANSHI){ name=(char*)"限时模式"; gamecenterid=(char*)"com.adultdream.mengmengda.xianshi"; } if(gamemode==MODE_JINGDIAN){ name=(char*)"经典模式"; gamecenterid=(char*)"com.adultdream.mengmengda.jingdian"; } CCLabelTTF* word = CCLabelTTF::create(name, "ttf/word.ttf", pane->getContentSize().height*0.15f); word->setAnchorPoint(ccp(0.5f,0.5f)); pane->addChild(word, 10); word->setPosition(ccpContent(pane->getContentSize(), 50, 80)); word->setColor(ccc3(255, 0, 255)); if(ishigh==true){ } } { char number[200]; sprintf(number, "%d", score); CCLabelTTF* word = CCLabelTTF::create(number, "ttf/word.ttf", pane->getContentSize().height*0.2f); word->setAnchorPoint(ccp(0.5f,0.5f)); pane->addChild(word, 10); word->setPosition(ccpContent(pane->getContentSize(), 50, 50)); word->setColor(ccBLACK); if(ishigh==true){ CCLabelTTF* hword = CCLabelTTF::create("(新记录!)", "ttf/word.ttf", pane->getContentSize().height*0.1f); hword->setAnchorPoint(ccp(0.5f,0.5f)); pane->addChild(hword, 10); hword->setPosition(ccpContent(pane->getContentSize(), 50, 40)); hword->setColor(ccRED); word->setPosition(ccpContent(pane->getContentSize(), 50, 60)); } } { char text[200]; sprintf(text, "最佳:%d", highscore); CCLabelTTF* word = CCLabelTTF::create(text, "ttf/word.ttf", pane->getContentSize().height*0.1f); word->setAnchorPoint(ccp(0.5f,0.5f)); pane->addChild(word, 10); word->setPosition(ccpContent(pane->getContentSize(), 50, 20)); word->setColor(ccBLACK); } { CCMenuItemImage *button = CCMenuItemImage::create( "over/replay.png", "over/replay.png", layer, menu_selector(GameLayer::replaceScene) ); CCMenu* Menu = CCMenu::create(button, NULL); Menu->setPosition(CCPointZero); pane->addChild(Menu,1); button->setScale(0.3f*pane->getContentSize().height/button->getContentSize().height); button->setPosition(ccpContent(pane->getContentSize(), 90, -80)); { button->setOpacity(0); CCDelayTime* d=CCDelayTime::create(0.5f); CCFadeTo* a1=CCFadeTo::create(0.5f, 255); CCSequence* l=(CCSequence*)CCSequence::create(d,a1,NULL); button->runAction(l); } } { CCMenuItemImage *button = CCMenuItemImage::create( "over/home.png", "over/home.png", layer, menu_selector(OverMenu::back_menu) ); CCMenu* Menu = CCMenu::create(button, NULL); Menu->setPosition(CCPointZero); pane->addChild(Menu,1); button->setScale(0.3f*pane->getContentSize().height/button->getContentSize().height); button->setPosition(ccpContent(pane->getContentSize(), 10, -80)); { button->setOpacity(0); CCDelayTime* d=CCDelayTime::create(0.5f); CCFadeTo* a1=CCFadeTo::create(0.5f, 255); CCSequence* l=(CCSequence*)CCSequence::create(d,a1,NULL); button->runAction(l); } } // { // CCMenuItemImage *button = CCMenuItemImage::create( // "over/gamecenter.png", // "over/gamecenter.png", // layer, // menu_selector(OverMenu::game_center) ); // CCMenu* Menu = CCMenu::create(button, NULL); // Menu->setPosition(CCPointZero); // pane->addChild(Menu,1); // button->setScale(0.3f*pane->getContentSize().height/button->getContentSize().height); // button->setPosition(ccpContent(pane->getContentSize(), 50, -80)); // // { // button->setOpacity(0); // CCDelayTime* d=CCDelayTime::create(0.5f); // CCFadeTo* a1=CCFadeTo::create(0.5f, 255); // CCSequence* l=(CCSequence*)CCSequence::create(d,a1,NULL); // button->runAction(l); // } // } // { // CCMenuItemImage *button = CCMenuItemImage::create( // "over/weixin.png", // "over/weixin.png", // layer, // menu_selector(OverMenu::share_weixin) ); // CCMenu* Menu = CCMenu::create(button, NULL); // Menu->setPosition(CCPointZero); // pane->addChild(Menu,1); // button->setScale(0.25f*pane->getContentSize().height/button->getContentSize().height); // button->setPosition(ccpContent(pane->getContentSize(), 85, -30)); // // { // button->setOpacity(0); // CCDelayTime* d=CCDelayTime::create(0.5f); // CCFadeTo* a1=CCFadeTo::create(0.5f, 255); // CCSequence* l=(CCSequence*)CCSequence::create(d,a1,NULL); // button->runAction(l); // } // // int check=BBGetOnlineParam((char*)"weixin", (char*)"0"); // if(check==0){ // button->setVisible(false); // } // } // { // CCMenuItemImage *button = CCMenuItemImage::create( // "over/friend.png", // "over/friend.png", // layer, // menu_selector(OverMenu::share_friend) ); // CCMenu* Menu = CCMenu::create(button, NULL); // Menu->setPosition(CCPointZero); // pane->addChild(Menu,1); // button->setScale(0.25f*pane->getContentSize().height/button->getContentSize().height); // button->setPosition(ccpContent(pane->getContentSize(),15, -30)); // // { // button->setOpacity(0); // CCDelayTime* d=CCDelayTime::create(0.5f); // CCFadeTo* a1=CCFadeTo::create(0.5f, 255); // CCSequence* l=(CCSequence*)CCSequence::create(d,a1,NULL); // button->runAction(l); // } // // int check=BBGetOnlineParam((char*)"weixin", (char*)"0"); // if(check==0){ // button->setVisible(false); // } // } } // { // float umeng_delay=BBGetOnlineParam((char*)"FullScreenDelay", (char*)"1"); // CCDelayTime* d=CCDelayTime::create(umeng_delay); // CCCallFunc * f=CCCallFunc::create(this, callfunc_selector(OverMenu::show_full_ad)); // CCSequence* l=(CCSequence*)CCSequence::create(d,f,NULL); // runAction(l); // } HLAdManagerWrapper::showUnsafeInterstitial(); return true; }
bool ChuanqiduanzaoItem::initItem(BaseActor* info) { bool bret = false; //初始化背景 if (CCSprite::initWithFile("shangdian_chuanqi_bg.png")) { bret = true; } if (bret) { m_Profession_id = info->getCurrentProperty().profession; //顶部文字显示 CCLabelTTF* pTopTip = CCLabelTTF::create(LFStrings::getValue("ShenBingDuanZao_ShuoMingTiShi").c_str(), fontStr_kaiti, 18); pTopTip->setPosition(ccp(this->getContentSize().width/2, this->getContentSize().height-70)); pTopTip->setColor(fonColor_FaGuang); this->addChild(pTopTip); //头像 CCSprite* pIcon = CCSprite::create(this->getIconFileNameByType(info->getActorType()).c_str()); this->addChild(pIcon); pIcon->setPosition(ccp(this->getContentSize().width/2, 400)); //美术字(英雄类型) CCSprite* pHeroType = CCSprite::create(this->getHeroTypeFileNameByType(info->getActorType()).c_str()); this->addChild(pHeroType); pHeroType->setPosition(ccp(this->getContentSize().width/2, pIcon->getPositionY()-pIcon->getContentSize().height/2-pHeroType->getContentSize().height+2)); //套装名字 string zhuangBeiStr = ChuanqiPool::getPool()->getChuanQiSuitName(info->getActorType()); CCLabelTTF* pName = CCLabelTTF::create(zhuangBeiStr.c_str(), fontStr_kaiti, 20); pName->setPosition(ccp(this->getContentSize().width/2, 280)); this->addChild(pName); CCMenu* pMenu = CCMenu::create(); this->addChild(pMenu); pMenu->setPosition(CCPointZero); //锻造一次 CCMenuItemImage *yici = CCMenuItemImage::create( "shangdian_shenbin_anniu_duanzaoyici.png", "shangdian_shenbin_anniu_duanzaoyici_select.png", this, menu_selector(ChuanqiduanzaoItem::menuItemClicked_DuanZao)); pMenu->addChild(yici, 0, Tag_MenuItem_Yici); yici->setPosition(ccp(this->getContentSize().width/2-yici->getContentSize().width/2, 185)); yici->setUserData((void*)info->getActorType()); //锻造十次 CCMenuItemImage *shici = CCMenuItemImage::create( "shangdian_shenbin_anniu_duanzaoshici.png", "shangdian_shenbin_anniu_duanzaoshici_select.png", this, menu_selector(ChuanqiduanzaoItem::menuItemClicked_DuanZao)); pMenu->addChild(shici, 0, Tag_MenuItem_Shici); shici->setPosition(ccp(this->getContentSize().width/2+yici->getContentSize().width/2, yici->getPositionY())); shici->setUserData((void*)info->getActorType()); char buf[20]; //锻造一次需要金币 BaseSprite *goldIcon1 = BaseSprite::create("jinbi_tubiao.png"); this->addChild(goldIcon1); goldIcon1->setScale(0.5f); goldIcon1->setPosition(ccp(yici->getPositionX()-goldIcon1->getContentSize().width/2, yici->getPositionY()+yici->getContentSize().height/2+goldIcon1->getContentSize().height/2)); sprintf(buf, "%d", 400); CCLabelTTF *yiciLabel = CCLabelTTF::create(buf, fontStr_BookAntiqua, 20); this->addChild(yiciLabel); yiciLabel->setPosition(ccp(goldIcon1->getPositionX()+goldIcon1->getContentSize().width/2*goldIcon1->getScaleX()+yiciLabel->getContentSize().width/2 , goldIcon1->getPositionY())); //锻造十次需要金币 BaseSprite *goldIcon10 = BaseSprite::create("jinbi_tubiao.png"); this->addChild(goldIcon10); goldIcon10->setScale(0.5f); goldIcon10->setPosition(ccp(shici->getPositionX()-goldIcon10->getContentSize().width/2, shici->getPositionY()+shici->getContentSize().height/2+goldIcon10->getContentSize().height/2)); sprintf(buf, "%d", 400*10); CCLabelTTF *yiciLabel0 = CCLabelTTF::create(buf, fontStr_BookAntiqua, 20); this->addChild(yiciLabel0); yiciLabel0->setPosition(ccp(goldIcon10->getPositionX()+goldIcon10->getContentSize().width/2*goldIcon10->getScaleX()+yiciLabel0->getContentSize().width/2 , goldIcon10->getPositionY())); CCMenuItemImage* pXiaoguo = CCMenuItemImage::create( "shandian_anniu_taozhuangxiaoguo.png", "shandian_anniu_taozhuangxiaoguo_select.png", this, menu_selector(ChuanqiduanzaoItem::menuItemClicked_Xiaoguo) ); pMenu->addChild(pXiaoguo); pXiaoguo->setPosition(ccp(this->getContentSize().width/2, pXiaoguo->getContentSize().height/2)); } return bret; }
bool PlayScene::init() { bool bRet = false; do { winSize = CCDirector::sharedDirector()->getWinSize(); isPause = false; //this->registerWithTouchDispatcher(); this->setKeypadEnabled(true); int lvl = CCUserDefault::sharedUserDefault()->getIntegerForKey("lvl"); if(lvl == 0) { lvl = 1; CCUserDefault::sharedUserDefault()->setIntegerForKey("lvl",1); } char mapPath[20]; char lvlinform[30]; //map_tmx_lvl1 sprintf(mapPath,"map/lvl%d.tmx",lvl); sprintf(lvlinform,"Lvl %d",lvl); isOver = -1; CCLabelTTF * lvlInform = CCLabelTTF::create(lvlinform, "Thonburi", 64); lvlInform->setPosition(ccp(-100,winSize.height/2)); this->addChild(lvlInform,3); overinfor = CCLabelTTF::create("Game Over", "Thonburi", 64); overinfor->setPosition(ccp(winSize.width/2,winSize.height/2)); overinfor->setVisible(false); this->addChild(overinfor,3); //这里是暂停菜单信息s lvlInform->runAction(CCJumpTo::create(3,ccp(winSize.width+100,winSize.height/2),60,10)); ccColor4B t; t.a = 200; t.r = 0; t.g = 0; t.b = 0; pauseLayer = CCLayerColor::create(t); this->addChild(pauseLayer,10); pauseLayer->setPosition(CCPointZero); CCSprite *pauseBg = CCSprite::create(pause_bg); pauseBg->setPosition(ccp(winSize.width/2 , winSize.height/2)); pauseLayer->addChild(pauseBg); CCMenuItemImage *resume = CCMenuItemImage::create( pause_resume, pause_resume, this, menu_selector(PlayScene::menuResume_CallBack) ); CCMenuItemImage *save = CCMenuItemImage::create( pause_save, pause_save, this, menu_selector(PlayScene::menuSaveCallBack) ); CCMenuItemImage *returnMenu = CCMenuItemImage::create( pause_return, pause_return, this, menu_selector(PlayScene::menuReturnCallBack) ); CCMenu * pauseMenu = CCMenu::create(resume,save,returnMenu,NULL); pauseMenu->alignItemsVerticallyWithPadding(30); pauseLayer->addChild(pauseMenu); pauseLayer->setVisible(false); ////////////////////////////////////////////////////////////////////////// Mediator::getInstance()->release(); //释放 Mediator::getInstance()->init(); //初始化 //加载地图 map = TmxMap::initMap(mapPath); // 地图 this->addChild(map); //添加摇杆,如果宏定义是手机 #ifdef IAMPHONE__ CCSprite * bg = CCSprite::create(control_bg); CCSprite * cen = CCSprite::create(control_ce); control = Joystick::JoystickWithCenter(ccp(120,120),100.f,cen,bg); control->Active(); this->addChild(control,1); control->setOpacity((float)0.3); //按钮 fire = CCMenuItemImage::create( fire_normal, fire_press ); fire->setOpacity(0.3*255); fire->setPosition(ccp(winSize.width - fire->getContentSize().width/2 - 10 , fire->getContentSize().height/2 + 10)); CCMenu * menu = CCMenu::create(fire,NULL); menu->setPosition(CCPointZero); CCLayer * menu_layer = CCLayer::create(); menu_layer->addChild(menu); this->addChild(menu_layer); this->setTouchEnabled(true); #endif // IAMPHONE__ Tank * tanks = Tank::getATank(tank_player,UP,HERO); //添加坦克 this->addChild(tanks); tanks->getTankSprite()->setPosition(ccp(14.5*map->getTileSize().width ,16)); #ifdef IAMPHONE__ Mediator::getInstance()->addJoyStick(control); //添加摇杆 Mediator::getInstance()->addFireButton(fire); //添加按钮 #endif Mediator::getInstance()->addGameScene(this); //添加场景 Mediator::getInstance()->addHeroTank(tanks); //添加坦克 Mediator::getInstance()->addTileMap(map); //添加地图 //schedule schedule(schedule_selector(PlayScene::gameFire),0.3f); //开火键监听器 schedule(schedule_selector(PlayScene::gameLoop),0.01f); //游戏主循环监听器 schedule(schedule_selector(PlayScene::gameCrash),0.02f); //游戏碰撞监听器 schedule(schedule_selector(PlayScene::gameAddEnemy),2.0f); //每隔一段时间添加敌人 schedule(schedule_selector(PlayScene::gameEnemyAction),0.05f); //敌方坦克的运动 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... ////////////////////////////////////////////////////////////////////////// CCSize size = CCDirector::sharedDirector()->getWinSize(); // 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 World", "Arial", 24); //CC_BREAK_IF(! pLabel); //// Get window size and place the label upper. //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. // pSprite = CCSprite::create("HelloWorld.png"); // CC_BREAK_IF(! pSprite); // pSprite->setPosition(ccp(size.width/3, size.height/3)); //pSprite->setAnchorPoint(ccp(0, 0)); // this->addChild(pSprite, 0); CCTMXTiledMap* tileMap = CCTMXTiledMap::tiledMapWithTMXFile("tmx/orthogonal.tmx"); this->addChild(tileMap, -1, TileMapNode); CCTMXLayer* eventLayer = tileMap->layerNamed("GameEventLayer"); eventLayer->setVisible(false); //tileMap->layerNamed("WinterLayer")->setVisible(false); //tileMap->layerNamed("Background")->setVisible(false); this->setTouchEnabled(true); bRet = true; } while (0); return bRet; }
// 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)); // // 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); //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); /*CCMenuItemLabel* labTtf = CCMenuItemLabel::create(CCLabelTTF::create("lable Menu Test","Arial",20),this,menu_selector(HelloWorld::clickImage)); labTtf->setPosition(ccp(visibleSize.width/2,visibleSize.height/10)); CCMenu *pMenuLabl = CCMenu::create(labTtf,NULL); pMenuLabl->setPosition(CCPointZero); this->addChild(pMenuLabl,1);*/ CCMenuItem *pImage = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::clickImage)); pImage->setPosition(visibleSize.width - pImage->getContentSize().width/2,visibleSize.height/2); CCMenu *pMenu = CCMenu::create(pImage,NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu,1); return true; }
// 初始化底部Bar void MessageView::initBottomBar(CCSize winSize) { /////////////////////////////////////////// 底部条 //////////////////////////////////////// this->footerBarNode = CCNode::create() ; this->footerBarNode->setAnchorPoint(ccp(0, 0)) ; this->footerBarNode->setPosition(ccp(0, -5)) ; this->addChild(this->footerBarNode) ; /////////////////////////////////////// 定义背景 ///////////////////////////////////////// // 背景 CCSprite* bottomBarBg = CCSprite::create("bg_bottom_bar.png") ; bottomBarBg->setAnchorPoint(ccp(0.5, 0)) ; bottomBarBg->setPosition(ccp(winSize.width/2, 0)) ; this->footerBarNode->addChild(bottomBarBg, 1) ; // 定义底部条高度 const float footerBarNode_height = bottomBarBg->getContentSize().height ; // 设置底部条的高和宽 this->footerBarNode->setContentSize(CCSizeMake(winSize.width, footerBarNode_height)) ; // 背景遮罩层 CCSprite* bottomBg = CCSprite::create("bg_bottom.png") ; bottomBg->setAnchorPoint(ccp(0, 1)) ; bottomBg->setPosition(ccp(0, footerBarNode_height)) ; this->footerBarNode->addChild(bottomBg, 0) ; CCSpriteBatchNode* bottomIcon = CCSpriteBatchNode::create("bg_bottom_icon.png") ; // 左边的icon CCSprite* leftIcon = CCSprite::createWithTexture(bottomIcon->getTexture()) ; leftIcon->setAnchorPoint(ccp(0, 0)) ; leftIcon->setPosition(ccp(0, 0)) ; this->footerBarNode->addChild(leftIcon, 1) ; // 右边的icon CCSprite* rightIcon = CCSprite::createWithTexture(bottomIcon->getTexture()) ; rightIcon->setAnchorPoint(ccp(1, 0)) ; rightIcon->setPosition(ccp(winSize.width, 0)) ; rightIcon->setFlipX(true) ; // 水平翻转 this->footerBarNode->addChild(rightIcon, 1) ; const float menuItem_margin = 50.0f ; // 距离中间位置的距离 const float menuItem_padding = 100.0f ; // menuItem之间的间隔 // 底部菜单 CCMenu* footerMenu = CCMenu::create() ; footerMenu->setContentSize(CCSizeMake(winSize.width, footerBarNode_height) ) ; footerMenu->setAnchorPoint(ccp(0, 0)) ; footerMenu->setPosition(ccp(0, 0)) ; this->footerBarNode->addChild(footerMenu, 2) ; // 活动菜单 CCMenuItemImage* activityMenuItem = CCMenuItemImage::create("menu_activity_n.png", "menu_activity_s.png", this, menu_selector(MessageView::menuActivityClickCallback)) ; activityMenuItem->setAnchorPoint(ccp(1, 1)) ; activityMenuItem->setPosition(ccp(winSize.width/2-menuItem_margin, footerBarNode_height-3)) ; footerMenu->addChild(activityMenuItem) ; // 获取menuItem宽度 const float menuItem_width = activityMenuItem->getContentSize().width ; // 信息菜单 CCMenuItemImage* infoMenuItem = CCMenuItemImage::create("menu_message_n.png", "menu_message_s.png", this, menu_selector(MessageView::menuInfoClickCallback)) ; infoMenuItem->setAnchorPoint(ccp(1, 1)) ; infoMenuItem->setPosition(ccp(winSize.width/2-menuItem_margin-menuItem_width-menuItem_padding, footerBarNode_height-3)) ; footerMenu->addChild(infoMenuItem) ; // 商城菜单 CCMenuItemImage* shopMenuItem = CCMenuItemImage::create("menu_shop_n.png", "menu_shop_s.png", this, menu_selector(MessageView::menuShopClickCallback)) ; shopMenuItem->setAnchorPoint(ccp(0, 1)) ; shopMenuItem->setPosition(ccp(winSize.width/2+menuItem_margin, footerBarNode_height-3)) ; footerMenu->addChild(shopMenuItem) ; // 菜单菜单 CCMenuItemImage* moreMenuItem = CCMenuItemImage::create("menu_more_n.png", "menu_more_s.png" , this, menu_selector(MessageView::menuMoreClickCallback)) ; moreMenuItem->setAnchorPoint(ccp(0, 1)) ; moreMenuItem->setPosition(ccp(winSize.width/2+menuItem_margin+menuItem_width+menuItem_padding, footerBarNode_height-3)) ; footerMenu->addChild(moreMenuItem) ; // 更多菜单背景 CCSprite* moreMenuBg = CCSprite::create("bg_more_menu.png") ; moreMenuBg->setAnchorPoint(ccp(0,0)) ; moreMenuBg->setPosition(ccp(0, 0)) ; const float moreMenu_width = moreMenuBg->getContentSize().width ; const float moreMenu_height = moreMenuBg->getContentSize().height ; // 更多菜单节点 moreMenuNode = CCNode::create() ; moreMenuNode->setContentSize(moreMenuBg->getContentSize()) ; // 设置moreMenuNode的尺寸 moreMenuNode->setAnchorPoint(ccp(1, 0)) ; moreMenuNode->setPosition(ccp(winSize.width-80, 80)) ; moreMenuNode->retain() ; // this->addChild(moreMenuNode, 5, MORE_MENU_NODE_TAG) ; moreMenuNode->addChild(moreMenuBg, 0) ; // 将背景加入到更多菜单中 // 更多菜单 CCMenu* moreMenu = CCMenu::create() ; moreMenu->setAnchorPoint(ccp(0, 0)) ; moreMenu->setPosition(ccp(0, 0)) ; moreMenuNode->addChild(moreMenu, 1, MORE_MENU_TAG) ; // 排行榜菜单项 CCMenuItemImage* rankingMenuItem = CCMenuItemImage::create("menu_ranking_n.png", "menu_ranking_s.png" , this, menu_selector(GameMenuView::menuRankingClickCallback)) ; rankingMenuItem->setAnchorPoint(ccp(0, 1)) ; rankingMenuItem->setPosition(ccp(10, moreMenu_height-10)) ; moreMenu->addChild(rankingMenuItem) ; // 任务菜单 CCMenuItemImage* taskMenuItem = CCMenuItemImage::create("menu_task_n.png", "menu_task_s.png" , this, menu_selector(GameMenuView::menuTaskClickCallback)) ; taskMenuItem->setAnchorPoint(ccp(1, 1)) ; taskMenuItem->setPosition(ccp(moreMenu_width-10, moreMenu_height-10)) ; moreMenu->addChild(taskMenuItem) ; // 好友菜单 CCMenuItemImage* friendMenuItem = CCMenuItemImage::create("menu_friend_n.png", "menu_friend_s.png", this, menu_selector(GameMenuView::menuFriendClickCallback)) ; friendMenuItem->setAnchorPoint(ccp(0, 0)) ; friendMenuItem->setPosition(ccp(10, 5)) ; moreMenu->addChild(friendMenuItem) ; // 客服菜单 CCMenuItemImage* customserviceMenuItem = CCMenuItemImage::create("menu_cs_n.png", "menu_cs_s.png", this, menu_selector(GameMenuView::menuSettingClickCallback)) ; customserviceMenuItem->setAnchorPoint(ccp(1, 0)) ; customserviceMenuItem->setPosition(ccp(moreMenu_width-10, 5)) ; moreMenu->addChild(customserviceMenuItem) ; }
bool Level::init() { if (!CCLayer::init()) { return false; } /*预加载音效*/ SimpleAudioEngine::sharedEngine()->preloadEffect("confirm.mp3"); /*添加背景*/ CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite* spBackGround = CCSprite::create("levelBackground.png"); spBackGround->setPosition(ccp(size.width*0.5, size.height*0.5)); addChild(spBackGround); /*创建图片菜单项*/ CCMenuItemImage *item1 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel1)); CCMenuItemImage *item2 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel2)); CCMenuItemImage *item3 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel3)); CCMenuItemImage *item4 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel4)); CCMenuItemImage *item5 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel5)); /*创建返回菜单项*/ CCMenuItemImage *itemBack = CCMenuItemImage::create("display_back.png", "display_back.png", this, menu_selector(Level::backToMenu));; /*设置菜单项大小*/ item1->setScale(0.45); item2->setScale(0.45); item3->setScale(0.45); item4->setScale(0.45); item5->setScale(0.45); /*创建菜单*/ CCMenu* menu1 = CCMenu::create(item1, item2, item3, item4, item5, NULL); menu1->setPosition(ccp(size.width * 0.5, size.height * 0.55)); menu1->alignItemsHorizontallyWithPadding(70); CCMenu* menuBack = CCMenu::create(itemBack, NULL); addChild(menuBack); menuBack->setPosition(ccp(size.width * 0.36, size.height * 0.05)); menuBack->setScale(0.7); menuBack->setColor(ccc3(0, 32, 240)); /*创建图片菜单项*/ CCMenuItemImage *item6 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel6)); CCMenuItemImage *item7 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel7)); CCMenuItemImage *item8 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel8)); CCMenuItemImage *item9 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel9)); CCMenuItemImage *item10 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel10)); /*设置菜单项大小*/ item6->setScale(0.45); item7->setScale(0.45); item8->setScale(0.45); item9->setScale(0.45); item10->setScale(0.45); /*创建菜单*/ CCMenu* menu2 = CCMenu::create(item6, item7, item8, item9, item10, NULL); menu2->setPosition(ccp(size.width * 1.5, size.height * 0.55)); menu2->alignItemsHorizontallyWithPadding(70); /*-----------------------*/ /*创建图片菜单项*/ CCMenuItemImage *item11 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel11)); CCMenuItemImage *item12 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel12)); CCMenuItemImage *item13 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel13)); CCMenuItemImage *item14 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel14)); CCMenuItemImage *item15 = CCMenuItemImage::create("level_normal.png", "level_selected.png", this, menu_selector(Level::chooseLevel15)); /*设置菜单项大小*/ item11->setScale(0.45); item12->setScale(0.45); item13->setScale(0.45); item14->setScale(0.45); item15->setScale(0.45); /*创建菜单*/ CCMenu* menu3 = CCMenu::create(item11, item12, item13, item14, item15, NULL); menu3->setPosition(ccp(size.width * 2.5, size.height * 0.55)); menu3->alignItemsHorizontallyWithPadding(70); /*添加关卡锁*/ lock2 = CCSprite::create("lock.png"); lock3 = CCSprite::create("lock.png"); lock4 = CCSprite::create("lock.png"); lock5 = CCSprite::create("lock.png"); lock6 = CCSprite::create("lock.png"); lock7 = CCSprite::create("lock.png"); lock8 = CCSprite::create("lock.png"); lock9 = CCSprite::create("lock.png"); lock10 = CCSprite::create("lock.png"); lock11 = CCSprite::create("lock.png"); lock12 = CCSprite::create("lock.png"); lock13 = CCSprite::create("lock.png"); lock14 = CCSprite::create("lock.png"); lock15 = CCSprite::create("lock.png"); item2->addChild(lock2); item3->addChild(lock3); item4->addChild(lock4); item5->addChild(lock5); item6->addChild(lock6); item7->addChild(lock7); item8->addChild(lock8); item9->addChild(lock9); item10->addChild(lock10); item11->addChild(lock11); item12->addChild(lock12); item13->addChild(lock13); item14->addChild(lock14); item15->addChild(lock15); addLock(); //创建层容器 CCLayer * containLayer = CCLayer::create(); containLayer->addChild(menu1); containLayer->addChild(menu2); containLayer->addChild(menu3); containLayer->setContentSize(CCSizeMake(size.width * 4, size.height)); /*添加关数*/ CCLabelTTF* level1 = CCLabelTTF::create("1st", "Georgia-Bold", 45); CCLabelTTF* level2 = CCLabelTTF::create("2nd", "Georgia-Bold", 45); CCLabelTTF* level3 = CCLabelTTF::create("3rd", "Georgia-Bold", 45); CCLabelTTF* level4 = CCLabelTTF::create("4th", "Georgia-Bold", 45); CCLabelTTF* level5 = CCLabelTTF::create("5th", "Georgia-Bold", 45); CCLabelTTF* level6 = CCLabelTTF::create("6th", "Georgia-Bold", 45); CCLabelTTF* level7 = CCLabelTTF::create("7th", "Georgia-Bold", 45); CCLabelTTF* level8 = CCLabelTTF::create("8th", "Georgia-Bold", 45); CCLabelTTF* level9 = CCLabelTTF::create("9th", "Georgia-Bold", 45); CCLabelTTF* level10 = CCLabelTTF::create("10th", "Georgia-Bold", 45); CCLabelTTF* level11 = CCLabelTTF::create("11th", "Georgia-Bold", 45); CCLabelTTF* level12 = CCLabelTTF::create("12th", "Georgia-Bold", 45); CCLabelTTF* level13 = CCLabelTTF::create("13th", "Georgia-Bold", 45); CCLabelTTF* level14 = CCLabelTTF::create("14th", "Georgia-Bold", 45); CCLabelTTF* level15 = CCLabelTTF::create("15th", "Georgia-Bold", 45); containLayer->addChild(level1); containLayer->addChild(level2); containLayer->addChild(level3); containLayer->addChild(level4); containLayer->addChild(level5); containLayer->addChild(level6); containLayer->addChild(level7); containLayer->addChild(level8); containLayer->addChild(level9); containLayer->addChild(level10); containLayer->addChild(level11); containLayer->addChild(level12); containLayer->addChild(level13); containLayer->addChild(level14); containLayer->addChild(level15); level1->setPosition(ccp(size.width * 0.18, size.height * 0.4)); level2->setPosition(ccp(size.width * 0.34, size.height * 0.4)); level3->setPosition(ccp(size.width * 0.5, size.height * 0.4)); level4->setPosition(ccp(size.width * 0.657, size.height * 0.4)); level5->setPosition(ccp(size.width * 0.82, size.height * 0.4)); level6->setPosition(ccp(size.width * 1.18, size.height * 0.4)); level7->setPosition(ccp(size.width * 1.34, size.height * 0.4)); level8->setPosition(ccp(size.width * 1.5, size.height * 0.4)); level9->setPosition(ccp(size.width * 1.657, size.height * 0.4)); level10->setPosition(ccp(size.width * 1.82, size.height * 0.4)); level11->setPosition(ccp(size.width * 2.18, size.height * 0.4)); level12->setPosition(ccp(size.width * 2.34, size.height * 0.4)); level13->setPosition(ccp(size.width * 2.5, size.height * 0.4)); level14->setPosition(ccp(size.width * 2.657, size.height * 0.4)); level15->setPosition(ccp(size.width * 2.82, size.height * 0.4)); /*创建滚动视图*/ scrollView = CCScrollView::create(CCSizeMake(size.width*3, size.height)); addChild(scrollView); scrollView->setBounceable(false);//设置scrollview有弹跳效果(默认为true) scrollView->setViewSize(CCSizeMake(size.width*2, size.height));//设置scrollView尺寸 scrollView->setContainer(containLayer);//设置容器 scrollView->getViewSize();//获取视图尺寸 scrollView->setTouchEnabled(true);//开启监听多触点 scrollView->setDelegate(this);//注册监听 return true; }
bool StoryWorld::init() { if ( !CCLayer::init() ) { return false; } char theName[10][11]={"","穆婧:", "子轩:", "少杰:", "建国", "路人A:", "路人B:", "路人C:", "老爷爷:", "江姐:"}; char play[20] = SCRIPT_PATH; int current=sGlobal->mapState->storyCnt+'0';//stay play[SCRIPT_PATH_LEN] = current; reader.ReadFileWithFullPath(CCFileUtils::sharedFileUtils()->fullPathForFilename(play)); setTouchEnabled(true); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( CLOSEN_IMG_PATH, CLOSES_IMG_PATH, this, menu_selector(StoryWorld::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); addChild(pMenu, 2); char bg_name[30] = BGNAME_IMG_PATH; bg_name[BGNAME_PATH_LEN] = current; CCSprite* pBackground = CCSprite::create(bg_name); pBackground->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); pBackground->setScale(1); pBackground->setTag(108); addChild(pBackground, 0); CCSprite* dialogBox = CCSprite::create(DUIHUAKUANG_IMG_PATH); dialogBox->setPosition(ccp(visibleSize.width/2, dialogBox->getContentSize().height/2)); dialogBox->setOpacity(220); addChild(dialogBox,1); CCLabelTTF* pName = CCLabelTTF::create(theName[0], "Heiti SC", 40); pName->setTag(101); pName->setPosition(ccp(pName->getContentSize().width/2, dialogBox->getContentSize().height - 2 * pName->getContentSize().height)); pName->setAnchorPoint(CCPointZero); addChild(pName, 1); CCLabelTTF* pLabel = CCLabelTTF::create("Click to Start", "Heiti SC", 40); pLabel->setTag(100); pLabel->setPosition(ccp(40, origin.y + dialogBox->getContentSize().height - 3.4 * pLabel->getContentSize().height)); pLabel->setAnchorPoint(CCPointZero); pLabel->setDimensions(CCSizeMake(1100, 0)); pLabel->setHorizontalAlignment(kCCTextAlignmentLeft); addChild(pLabel, 1); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(PLIST_IMG_PATH); CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::create(VDRAWING_IMG_PATH); spriteBatch->setTag(102); addChild(spriteBatch, 0); spriteBatch->setPosition(CCPointZero); CCSprite *leftSprite=CCSprite::createWithSpriteFrameName("me_1.png"); leftSprite->setScale(0.8); leftSprite->setPosition(ccp(leftSprite->getContentSize().width*0.8, leftSprite->getContentSize().height/2 *0.8)); leftSprite->setTag(1); leftSprite->setOpacity(0); spriteBatch->addChild(leftSprite, 0); CCSprite *rightSprite=CCSprite::createWithSpriteFrameName("blank.png"); rightSprite->setPosition(ccp(800, 130)); rightSprite->setTag(2); rightSprite->setOpacity(0); spriteBatch->addChild(rightSprite, 0); avgGame(); 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::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 World", "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; }
void StoryWorld::makeAChoice(CCObject *sender) { int choice = ((CCNode *)sender)->getTag(); if (current == '3') { switch (choice) { case fChoice: { // 显示宣传单一 cout << "xuanchuan " << endl; removeChildByTag(2); removeChildByTag(3); CCSprite *blackBG = CCSprite::create(BLACK_IMG_PATH); blackBG->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/2)); blackBG->setScale(1); blackBG->setTag(21); addChild(blackBG, 2); // TODO CCLabelTTF *xuanchuan = CCLabelTTF::create(); xuanchuan->setPosition(ccp(0, 40)); xuanchuan->setHorizontalAlignment(kCCTextAlignmentLeft); xuanchuan->setAnchorPoint(CCPointZero); xuanchuan->setDimensions(CCSizeMake(1100, 0)); xuanchuan->setTag(22); addChild(xuanchuan, 3); // 关闭按钮 CCMenuItemImage *start = CCMenuItemImage::create(LEAFLET_IMG_PATH, LEAFLET_IMG_PATH, this, menu_selector(StoryWorld::menuLeafletsCloseCallback)); start->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width-40, CCDirector::sharedDirector()->getVisibleSize().height-40)); //菜单 CCMenu* pMenu = CCMenu::create(start, NULL); pMenu->setPosition(CCPointZero); pMenu->setTag(23); addChild(pMenu, 4); } return; case sChoice: default:{ current+=1; CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionExactFit); CCDirector::sharedDirector()->popScene(); } return; } } char fileName[8] = "Fin "; switch (choice) { case fChoice: fileName[3]='1'; break; case sChoice: fileName[3]='2'; break; case tChoice: fileName[3]='3'; break; default: break; } reader.ChangeFile(cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName)); removeChildByTag(2); removeChildByTag(3); setTouchEnabled(true); avgGame(); }
bool MyWorld::init() { if (!oPlatformWorld::init()) { return false; } this->setTouchEnabled(true); // Create Back Menu CCPoint visibleOrigin = CCEGLView::sharedOpenGLView()->getVisibleOrigin(); CCSize visibleSize = CCEGLView::sharedOpenGLView()->getVisibleSize(); CCMenuItemFont *itemBack = CCMenuItemFont::create("Back", this, menu_selector(MyWorld::toExtensionsMainLayer)); itemBack->setPosition(ccp(visibleOrigin.x+visibleSize.width - 50, visibleOrigin.y+25)); CCMenu *menuBack = CCMenu::create(itemBack, NULL); menuBack->setPosition(CCPointZero); this->getUILayer()->addChild(menuBack); // Set Contact Info /* By default all groups won`t collide each other. You could set the contact info before game start or change it later. Group number can only be 0 to 15. */ GROUP_TERRAIN = 0; GROUP_ONE = 1; GROUP_TWO = 2; GROUP_TEST_SENSOR = 15; this->setShouldContact(GROUP_TERRAIN, GROUP_TERRAIN, true); this->setShouldContact(GROUP_TWO, GROUP_TERRAIN, true); this->setShouldContact(GROUP_TEST_SENSOR, GROUP_ONE, true);// Body from GROUP_ONE will be detected by test sensor. // Create Borders oBodyDef* bodyDef = oBodyDef::create(); bodyDef->type = b2_staticBody; vector<b2Vec2> vertices(5); vertices[0].Set(0, 0); vertices[1].Set(visibleSize.width, 0); vertices[2].Set(visibleSize.width, visibleSize.height); vertices[3].Set(0, visibleSize.height); vertices[4].Set(0, 0); bodyDef->attachChain(vertices, 0.4f, 0.4f); oBody* body = bodyDef->toBody(this, GROUP_TERRAIN, 0, 0, 0); CCSprite* sprite = CCSprite::create("Images/Icon.png"); sprite->setPosition(ccp(100,40)); body->addChild(sprite); this->addChild(body); // Create Body sprite = CCSprite::create("Images/Icon.png"); CCSize size = sprite->getContentSize(); bodyDef = oBodyDef::create(); bodyDef->type = b2_dynamicBody; bodyDef->attachPolygon(size.width, size.height, 1.0f, 0.4f, 0.4f);//Body define use bodyDef->attach body = bodyDef->toBody(this, GROUP_ONE, visibleSize.width*0.5f-20.0f, visibleSize.height*0.5f+20.0f, 50.0f); //body->attach(CCBodyDef::polygon(size.width, size.height, 1.0f, 0.4f, 0.4f)); //Body instance use body->attach body->addChild(sprite); this->addChild(body); body = bodyDef->toBody(this, GROUP_TERRAIN, visibleSize.width*0.5f+50.0f, visibleSize.height*0.5f+20.0f); sprite = CCSprite::create("Images/Icon.png"); sprite->runAction( CCRepeatForever::create( CCSequence::create( CCRotateTo::create(3.0f, 45), CCRotateTo::create(3.0f, 0), nullptr))); body->addChild(sprite); this->addChild(body); // Create Test Sensor int SENSOR_TAG = 1; bodyDef = oBodyDef::create(); bodyDef->type = b2_staticBody; //bodyDef->attachPolygonSensor(SENSOR_TAG, visibleSize.width, 40.0f); body = bodyDef->toBody(this, GROUP_TEST_SENSOR, visibleSize.width*0.5f, 20.0f); //CCSensor* sensor = body->getSensorByTag(SENSOR_TAG); oSensor* sensor = body->attachSensor(SENSOR_TAG, oBodyDef::polygon(visibleSize.width, 40.0f)); oSharedEffectCache.load("main.effect"); sensor->bodyEnter += [&](oSensor* sensor, oBody* body) { // When body enters sensor area, remove the body //body->getParent()->removeChild(body); this->query(CCRect(0.0f,0.0f,100.0f,100.0f), [&](oBody* body) { if (body->getGroup() != GROUP_TERRAIN) { body->getParent()->removeChild(body); return true; } return false; }); // Create a jumping grossini using its def _character = _characterDef->toBody(this, GROUP_TWO, 100.0f, 200.0f); CCSprite* sprite = CCSprite::create("Images/grossini.png"); _character->addChild(sprite); this->getCamera()->setFollowRatio(ccp(0.1f,0.1f)); this->getCamera()->follow(_character); this->addChild(_character); oEffect::create(1)->attachTo(_character)->autoRemove()->start(); }; //sensor->bodyEnter += std::make_pair(this, &MyWorld::onBodyEnter); this->addChild(body); /* Create a Platform Character`s Define (The method to make a platform character will be included in Box2D Nodes extension in the future) */ TAG_GROUND_SENSOR = 2; float BOTTOM_OFFSET = 4.0f; float GROUND_SENSOR_HEIGHT = 4.0f; size.setSize(65,116);//The physical size of our character _characterDef = oBodyDef::create(); _characterDef->type = b2_dynamicBody; //_characterDef->fixedRotation = true; float hw = size.width * 0.5f; float hh = size.height * 0.5f; b2Vec2 verts[] = { b2Vec2(-hw, hh), b2Vec2(-hw + BOTTOM_OFFSET, -hh), b2Vec2(hw - BOTTOM_OFFSET, -hh), b2Vec2(hw, hh) }; _characterDef->attachPolygon(verts, 4, 1.0f, 0.4f, 0.4f); _characterDef->attachPolygonSensor( TAG_GROUND_SENSOR, size.width - BOTTOM_OFFSET * 2, GROUND_SENSOR_HEIGHT, b2Vec2(0, -hh - GROUND_SENSOR_HEIGHT * 0.5f)); sprite = CCSprite::create("Images/Icon.png"); //sprite->setOpacity(0); sprite->runAction( oSequence::create( CCSpawn::create( oKeyPos::create(2.0f, 300.0f, 300.0f, oEase::OutBack), oKeyScale::create(2.0f, 3.0f, 3.0f, oEase::InOutElastic), nullptr), CCSpawn::create( oKeyPos::create(2.0f, 100.0f, 100.0f, oEase::OutBack), oKeyScale::create(2.0f, 1.0f, 1.0f, oEase::InOutElastic), nullptr), nullptr)); this->addChild(sprite); this->getCamera()->setBoudary(CCRectMake(-1000, -1000, 2000, 2000)); sprite = CCSprite::create("Images/Icon.png"); sprite->setScale(100.0f); this->addChild(sprite, -100); this->setLayerRatio(-100, ccp(-1.5f, -1.5f)); /* CCSpriteBatchNode* batchNode = CCSpriteBatchNode::createWithTexture(CCTextureCache::sharedTextureCache()->addImage("Images/grossini.png")); sprite = CCSprite::create("Images/grossini.png"); CCSprite* childSp = CCSprite::create("Images/grossini.png"); childSp->setRotation(45.0f); childSp->setPosition(ccp(20,30)); sprite->addChild(childSp); batchNode->addChild(sprite); this->addChild(batchNode);*/ oModelDef* modelDef = oSharedModelCache.load("nvjing.model"); _model = modelDef->toModel(); _model->setPosition(ccp(200,200)); _model->setRecovery(0.2f); _model->setFaceRight(false); _model->setLook("happy"); _model->setLoop(true); _model->play("walk"); this->addChild(_model); return true; }
void StoryWorld::avgGame(void) { CCLabelTTF* myDialog = (CCLabelTTF *)getChildByTag(100); CCLabelTTF* myName = (CCLabelTTF *)getChildByTag(101); CCSpriteBatchNode *spriteBatch = (CCSpriteBatchNode *)getChildByTag(102); CCSprite *myLeftSprite = (CCSprite *)spriteBatch->getChildByTag(1); CCSprite *myRightSprite = (CCSprite *)spriteBatch->getChildByTag(2); strcpy(dialog, reader.GetNextDialog().c_str()); char theName[10][11]={"","穆婧1:", "子轩:", "少杰:", "建国", "路人A:", "路人B:", "路人C:", "老爷爷:", "江姐:"}; myName->setString(theName[dialog[0]-48]); //任务立绘切换 switch (dialog[0]) { case '0': { //无人 char b[10]="me_ .png"; b[3] = dialog[1]+1; myLeftSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '1': { //ME char b[10]="me_ .png"; b[3] = dialog[1]+1; myLeftSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '2': { //子轩 char b[10]="zx_ .png"; b[3] = dialog[1]+1; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '3': { //少杰 char b[10]="sj_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '4': { //建国 char b[10]="jg_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '5': { char b[10]="la_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '6': { char b[10]="lb_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '7': { char b[10]="lc_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '8': { char b[10]="yy_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; case '9': { char b[10]="jj_ .png"; b[3] = dialog[1]; myRightSprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(b)); } break; default: break; } switch (dialog[2]) { // 选择走向 case '1': { //开始选择走向 //停止触摸 setTouchEnabled(false); CCSprite *back = CCSprite::create(LANDSCAPE_IMG_PATH); back->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/2)); back->setOpacity(150); back->setTag(3); addChild(back, 3); switch (current) { case '3': { CCLabelTTF *Label1 = CCLabelTTF::create("Yes", "Heiti SC", 40); CCLabelTTF *Label2 = CCLabelTTF::create("No", "Heiti SC", 40); CCMenuItemLabel *firstChoice = CCMenuItemLabel::create(Label1, this, menu_selector(StoryWorld::makeAChoice)); CCMenuItemLabel *secondChoice = CCMenuItemLabel::create(Label2, this, menu_selector(StoryWorld::makeAChoice)); firstChoice->setTag(fChoice); secondChoice->setTag(sChoice); firstChoice->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/5 * 4.1)); secondChoice->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/5 * 2.9)); CCMenu *menu = CCMenu::create(firstChoice, secondChoice, NULL); menu->setPosition(CCPointZero); menu->setTag(2); addChild(menu, 3); return; } break; case '9': { //创建CCMenu CCLabelTTF *Label1 = CCLabelTTF::create("子轩", "Heiti SC", 40); CCLabelTTF *Label2 = CCLabelTTF::create("少杰", "Heiti SC", 40); CCLabelTTF *Label3 = CCLabelTTF::create("建国", "Heiti SC", 40); CCMenuItemLabel *firstChoice = CCMenuItemLabel::create(Label1, this, menu_selector(StoryWorld::makeAChoice)); CCMenuItemLabel *secondChoice = CCMenuItemLabel::create(Label2, this, menu_selector(StoryWorld::makeAChoice)); CCMenuItemLabel *thirdChoice = CCMenuItemLabel::create(Label3, this, menu_selector(StoryWorld::makeAChoice)); firstChoice->setTag(fChoice); secondChoice->setTag(sChoice); thirdChoice->setTag(tChoice); firstChoice->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/5 * 4.1)); secondChoice->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/5 * 2.9)); thirdChoice->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/5 * 1.8)); CCMenu *menu = CCMenu::create(firstChoice, secondChoice, thirdChoice, NULL); menu->setPosition(CCPointZero); menu->setTag(2); addChild(menu, 3); return; } break; default: break; } } break; case '2': { CCScene *combatScene = Combat::scene(); CCDirector::sharedDirector()->pushScene(combatScene); } break; case '3': { if (myLeftSprite->getOpacity() == 0) myLeftSprite->setOpacity(255); else myLeftSprite->runAction(CCFadeOut::create(1)); } break; case '4': { if (myRightSprite->getOpacity() == 0) myRightSprite->setOpacity(255); else myRightSprite->runAction(CCFadeOut::create(1)); } break; case '5': { if (myRightSprite->getOpacity()==0 && myLeftSprite->getOpacity()==0) { myLeftSprite->setOpacity(255); myRightSprite->setOpacity(255); } else if (myRightSprite->getOpacity()!=0 && myLeftSprite->getOpacity()!=0) { myLeftSprite->runAction(CCFadeOut::create(1)); myRightSprite->runAction(CCFadeOut::create(1)); } } break; default: break; } // 剧本切换及完结及特效 switch (dialog[3]) { case '1':{ // 保存进度 current+=1; CCEGLView::sharedOpenGLView()->setDesignResolutionSize(672, 448, kResolutionExactFit); CCDirector::sharedDirector()->popScene(); // TODO: POP OUT } break; case '2':{ // wanjie CCSprite* staff_bg = CCSprite::create(STAFFBG_IMG_PATH); staff_bg->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2 + CCDirector::sharedDirector()->getVisibleOrigin().x, CCDirector::sharedDirector()->getVisibleSize().height/2 + CCDirector::sharedDirector()->getVisibleOrigin().y)); addChild(staff_bg, 4); CCSprite* staff = CCSprite::create(STAFF_IMG_PATH ); staff->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, -CCDirector::sharedDirector()->getVisibleSize().height/2)); addChild(staff, 5); CCActionInterval * moveTo = CCMoveTo::create(20,ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height*3/2)); CCFiniteTimeAction* sequneceAction = CCSequence::create( moveTo, CCCallFunc::create(this, callfunc_selector(StoryWorld::over)), NULL); staff->runAction(sequneceAction); } break; case '3':{ CCSprite *black = CCSprite::create(BLACK_IMG_PATH); black->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width/2, CCDirector::sharedDirector()->getVisibleSize().height/2)); black->setOpacity(0); addChild(black, 4); black->runAction(CCSequence::create(CCFadeIn::create(0.5), CCFadeOut::create(0.5), NULL)); } break; default: break; } //音效 switch (dialog[4]) { case '1': case '2': case '3': case '4': case '5': case '6':{ char musicName[7] = " .mp3"; musicName[0] = dialog[4]; if (CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) { CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(true); } CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename(musicName).c_str()); CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename(musicName).c_str(), true); } break; case '7': { CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(true); } break; case '8': case '9': case 'A': case 'B': case 'C': case 'D': case 'G':{ char effectName[7] = " .wav"; effectName[0] = dialog[4]; CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(CCFileUtils::sharedFileUtils()->fullPathForFilename(effectName).c_str()); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CCFileUtils::sharedFileUtils()->fullPathForFilename(effectName).c_str()); } break; case 'E': case 'F': case 'H':{ char effectName[7] = " .mp3"; effectName[0] = dialog[4]; CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(CCFileUtils::sharedFileUtils()->fullPathForFilename(effectName).c_str()); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(CCFileUtils::sharedFileUtils()->fullPathForFilename(effectName).c_str()); } break; default: break; } // 背景 char all_bg[8][4] = {"010", "030", "204", "701", "717", "725", "732"}; char bg_num[4]="000"; int curLine = reader.getCurLine(); sprintf(bg_num, "%c%02d", current, curLine); if (curLine == 0) { CCSprite *Background = (CCSprite *)getChildByTag(108); char bg_name[30]=BGNAME_IMG_PATH; bg_name[BGNAME_PATH_LEN] = current; Background->setTexture(CCTextureCache::sharedTextureCache()->addImage(bg_name)); } else { for (int i =0; i<8; i++) { if (strcmp(all_bg[i], bg_num)==0) { CCSprite *Background = (CCSprite *)getChildByTag(108); char bg_name[30]="img/story/bg_000.jpg"; sprintf(bg_name, "img/story/bg_%s.jpg", bg_num); Background->setTexture(CCTextureCache::sharedTextureCache()->addImage(bg_name)); } } } myDialog->setString(dialog+5); }
// Helper bool CCRichLabelTTF::updateTexture() { CCTexture2D_richlabel *tex; tex = new CCTexture2D_richlabel(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccRichFontDefinition texDef = _prepareTextDefinition(true); tex->initWithRichString( m_string.c_str(), &texDef ); #else tex->initWithRichString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = _texture->getContentSize(); this->setTextureRect(rect); // create link menu CCMenu* menu = (CCMenu*)getChildByTag(TAG_MENU); const LinkMetaList& linkMetas = tex->getLinkMetas(); if(!linkMetas.empty() && m_textChanging) { // delete old menu if has if(menu) { menu->removeFromParent(); menu = NULL; } // now create items for every link //CCArray* items = CCArray::create(); Vector<MenuItem*> items; for(LinkMetaList::const_iterator iter = linkMetas.begin(); iter != linkMetas.end(); iter++) { const LinkMeta& meta = *iter; CCMenuItemColor* item = CCMenuItemColor::create(ccc4FromInt(meta.normalBgColor), ccc4FromInt(meta.selectedBgColor), this, menu_selector(CCRichLabelTTF::onLinkMenuItemClicked)); item->setTag(START_TAG_LINK_ITEM + meta.tag); item->setPosition(ccp(meta.x + meta.width / 2, meta.y + meta.height / 2)); item->setContentSize(CCSizeMake(meta.width, meta.height)); item->setStateListener(m_stateListener); items.pushBack(item); } // add menu in -1 z order so that it won't override label UI CCMenu* newMenu = CCMenu::createWithArray(items); newMenu->setPosition(CCPointZero); addChild(newMenu, -1, TAG_MENU); // clear flag m_textChanging = false; } // sync shadow stroke padding to link menu // the padding must divide scale factor. Because at this time, texture already scaled for label menu = (CCMenu*)getChildByTag(TAG_MENU); if(menu) { const CCPoint& p = tex->getShadowStrokePadding(); menu->setPosition(ccp(p.x / CC_CONTENT_SCALE_FACTOR(), p.y / CC_CONTENT_SCALE_FACTOR())); } //ok return true; }
// 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 - 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); // 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); // add "HelloWorld" splash screen" m_pSourceSprite = CCSprite::create("source.png"); m_pSourceSprite->setPosition( ccp(size.width/2, size.height/2) ); m_pSourceTexture = CCRenderTexture::create(m_pSourceSprite->getContentSize().width, m_pSourceSprite->getContentSize().height); m_pSourceTexture->setPosition( ccp(size.width/2, size.height/2) ); m_pSourceTexture->begin(); m_pSourceSprite->visit(); m_pSourceTexture->end(); this->addChild(m_pSourceTexture); MaskSprite *maskSprite = MaskSprite::create("CloseNormal1.png"); maskSprite->setPosition(ccp(0,size.height - maskSprite->getContentSize().height/2)); this->addChild(maskSprite); return true; }
//------------------------------------------------------------------ // // MenuLayer1 // //------------------------------------------------------------------ MenuLayer1::MenuLayer1() { CCMenuItemFont::setFontSize( 30 ); CCMenuItemFont::setFontName("Courier New"); setIsTouchEnabled(true); // Font Item CCSprite* spriteNormal = CCSprite::spriteWithFile(s_MenuItem, CCRectMake(0,23*2,115,23)); CCSprite* spriteSelected = CCSprite::spriteWithFile(s_MenuItem, CCRectMake(0,23*1,115,23)); CCSprite* spriteDisabled = CCSprite::spriteWithFile(s_MenuItem, CCRectMake(0,23*0,115,23)); //dynamic_cast<CCNode*>(mgr)->addChild(spriteNormal); //dynamic_cast<CCNode*>(mgr)->addChild(spriteSelected); //dynamic_cast<CCNode*>(mgr)->addChild(spriteDisabled); CCMenuItemSprite* item1 = CCMenuItemSprite::itemFromNormalSprite(spriteNormal, spriteSelected, spriteDisabled, this, menu_selector(MenuLayer1::menuCallback) ); // Image Item CCMenuItem* item2 = CCMenuItemImage::itemFromNormalImage(s_SendScore, s_PressSendScore, this, menu_selector(MenuLayer1::menuCallback2) ); // Label Item (LabelAtlas) CCLabelAtlas* labelAtlas = CCLabelAtlas::labelWithString("0123456789", "fonts/fps_images.png", 16, 24, '.'); CCMenuItemLabel* item3 = CCMenuItemLabel::itemWithLabel(labelAtlas, this, menu_selector(MenuLayer1::menuCallbackDisabled) ); item3->setDisabledColor( ccc3(32,32,64) ); item3->setColor( ccc3(200,200,255) ); // Font Item CCMenuItemFont *item4 = CCMenuItemFont::itemFromString("I toggle enable items", this, menu_selector(MenuLayer1::menuCallbackEnable) ); item4->setFontSizeObj(20); item4->setFontName("Marker Felt"); // Label Item (CCLabelBMFont) CCLabelBMFont* label = CCLabelBMFont::labelWithString("configuration", "fonts/bitmapFontTest3.fnt"); CCMenuItemLabel* item5 = CCMenuItemLabel::itemWithLabel(label, this, menu_selector(MenuLayer1::menuCallbackConfig)); // Testing issue #500 item5->setScale( 0.8f ); // Font Item CCMenuItemFont* item6 = CCMenuItemFont::itemFromString("Quit", this, menu_selector(MenuLayer1::onQuit)); CCActionInterval* color_action = CCTintBy::actionWithDuration(0.5f, 0, -255, -255); CCActionInterval* color_back = color_action->reverse(); CCFiniteTimeAction* seq = CCSequence::actions(color_action, color_back, NULL); item6->runAction(CCRepeatForever::actionWithAction((CCActionInterval*)seq)); CCMenu* menu = CCMenu::menuWithItems( item1, item2, item3, item4, item5, item6, NULL); menu->alignItemsVertically(); // elastic effect CCSize s = CCDirector::sharedDirector()->getWinSize(); int i=0; CCNode* child; CCArray * pArray = menu->getChildren(); CCObject* pObject = NULL; CCARRAY_FOREACH(pArray, pObject) { if(pObject == NULL) break; child = (CCNode*)pObject; CCPoint dstPoint = child->getPosition(); int offset = (int) (s.width/2 + 50); if( i % 2 == 0) offset = -offset; child->setPosition( CCPointMake( dstPoint.x + offset, dstPoint.y) ); child->runAction( CCEaseElasticOut::actionWithAction( CCMoveBy::actionWithDuration(2, CCPointMake(dstPoint.x - offset,0)), 0.35f ) ); i++; } m_disabledItem = item3; item3->retain(); m_disabledItem->setIsEnabled( false ); addChild(menu); }
// 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... // CCLayerColor *layer=CCLayerColor::create(ccc4(2255,255,255,255)); //addChild(layer); _mouseJoint=NULL; showXYZ="X:0, Y:0, Z:10"; ////////////////////////////////////////////////////////////////////////// // 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". _pLabel = CCLabelTTF::create("Hello World", "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); //Box2D/////////////////////////////////////////// this->createWorld(); this->registerContactListener(); // this->createDebugDraw(); this->createWrapWall(); // this->createRigidCircle(20,20); // this->createRigidCircle(22,30); // this->createRigidCircle(24,40); this->createBridge(); //this->createRopeJoint(3);//创建实心圆到小方块的绳索关节 this->setTouchEnabled(true); this->setAccelerometerEnabled(true); resinBallBody->init(_world); //resinBallBody->shrinkResinBallBody(1); this->initInsects(); CCSprite* sprite=CCSprite::create("CloseNormal.png"); sprite->setPosition(ccp(150,150)); addChild(sprite); b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(150/PTM_RATIO, 150/PTM_RATIO); bodyDef.userData=sprite; b2Body* body = _world->CreateBody(&bodyDef); b2CircleShape bodyShap; bodyShap.m_radius=sprite->getContentSize().width/2.0f/PTM_RATIO; b2FixtureDef fixtureDef; b2Filter filter; filter.groupIndex=2; fixtureDef.filter=filter; fixtureDef.shape=&bodyShap; body->CreateFixture(&fixtureDef); CCSprite* sprite2=CCSprite::create("CloseNormal.png"); sprite2->setPosition(ccp(250,150)); addChild(sprite2); b2BodyDef bodyDef2; bodyDef2.type = b2_dynamicBody; bodyDef2.position.Set(250/PTM_RATIO, 150/PTM_RATIO); bodyDef2.userData=sprite2; b2Body* body2 = _world->CreateBody(&bodyDef2); b2CircleShape bodyShap2; bodyShap2.m_radius=sprite2->getContentSize().width/2.0f/PTM_RATIO; b2FixtureDef fixtureDef2; b2Filter filter2; filter2.groupIndex=2; fixtureDef2.filter=filter2; fixtureDef2.shape=&bodyShap2; body2->CreateFixture(&fixtureDef2); b2Vec2 force = b2Vec2(10, 10); //body->ApplyLinearImpulse(force, bodyDef.position); //body->ApplyForceToCenter(b2Vec2(109,109)); this->schedule(schedule_selector(HelloWorld::tick)); this->schedule(schedule_selector(HelloWorld::changeXYZShow),1); bRet = true; } while (0); return bRet; }