1.设置屏幕尺寸
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { Configuration *pConf = Configuration::getInstance(); pConf->loadConfigFile("strings.plist"); glview = GLView::create(pConf->getValue("string1").asString()); director->setOpenGLView(glview); } director->setProjection(Director::Projection::_2D);//设置图片抗锯齿 glview->setDesignResolutionSize(360,640,ResolutionPolicy::EXACT_FIT); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) glview->setFrameSize(360,640); #endif // turn on display FPS director->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = HomePage::createScene(); // run director->runWithScene(scene); return true; }
2.一个scene上放两个layer,其中一个半透明
cocos2d::Scene * FengJing::createScene() { auto scene = Scene::create(); //auto layer = FengJing::create(); auto layer = GalleryLayer::create(); auto layer2 = Poetry::create(); layer2->setOpacity(5); scene->addChild(layer); scene->addChild(layer2); return scene; }
3.设置背景颜色
if (!LayerColor::initWithColor(Color4B(255,255,255))) { <span style="white-space:pre"> </span>return false; }
4.定时器
//scheduleUpdate(); scheduleOnce(schedule_selector(HomePage::update),1.0f);
void HomePage::update(float delta) { auto director = Director::getInstance(); auto scene = FengJing::createScene(); director->replaceScene(scene); }
5.获取系统当前时间
<span style="white-space:pre"> </span>struct tm *tm; time_t timep; time(&timep); tm = localtime(&timep); char time[64] = { 0 }; sprintf(time,"%02d月%02d日",tm->tm_mon + 1,tm->tm_mday); std::string stime = time;
6.用系统字体显示文字
<span style="white-space:pre"> </span>auto label = Label::createWithSystemFont(CMyUtils::getInstance()->Gb2312ToUtf8(stime.c_str()),"Arial",20); <span style="white-space:pre"> </span>label->setColor(Color3B::WHITE); <span style="white-space:pre"> </span>label->setAnchorPoint(Vec2(0,1)); <span style="white-space:pre"> </span>label->setPosition(origin.x + 10,origin.y + visibleSize.height - 10 ); <span style="white-space:pre"> </span>addChild(label);
7.用.ttf文件显示自定义字体文字
<span style="white-space:pre"> </span>label = Label::createWithTTF(CMyUtils::getInstance()->Gb2312ToUtf8("少小离家老大回"),"fonts/test2.ttf",26); <span style="white-space:pre"> </span>label->setColor(Color3B::WHITE); label->setAnchorPoint(Vec2(0,origin.y + visibleSize.height - 40 ); <span style="white-space:pre"> </span>addChild(label);
<span style="white-space:pre"> </span>auto menuitem1 = MenuItemImage::create("bt_main_shicitiaozhan.png","bt_main_shicitiaozhan.png",CC_CALLBACK_1(Poetry::ShiciTiaozhanCallback,this)); menuitem1->setScale(0.3f); menuitem1->setAnchorPoint(Vec2(0,0)); menuitem1->setPosition(Vec2(origin.x + 10,origin.y + 20)); menuitem1->setOpacity(200); auto menuitem2 = MenuItemImage::create("bt_main_jingcaijiemu.png","bt_main_jingcaijiemu.png"); menuitem2->setScale(0.3f); menuitem2->setAnchorPoint(Vec2(0,0)); menuitem2->setPosition(Vec2(origin.x + 120,origin.y + 20)); menuitem2->setOpacity(200); auto menuitem3 = MenuItemImage::create("bt_main_baomingcansai.png","bt_main_baomingcansai.png"); menuitem3->setScale(0.3f); menuitem3->setAnchorPoint(Vec2(0,0)); menuitem3->setPosition(Vec2(origin.x + 235,origin.y + 20)); menuitem3->setOpacity(200); auto menu2 = Menu::create(menuitem1,menuitem2,menuitem3,NULL); menu2->setPosition(Vec2::ZERO); <span style="white-space:pre"> </span>this->addChild(menu2,1);
void Poetry::ShiciTiaozhanCallback(Ref* pSender) { auto director = Director::getInstance(); auto scene = ShiciTiaozhan::createScene(); director->replaceScene(scene); }
9.另一种利用监听回调的按钮
<span style="white-space:pre"> </span>auto sprite4 = Sprite::create("title_back_red.png"); sprite4->setScaleX(0.5f); sprite4->setScaleY(0.5f); sprite4->setAnchorPoint(Vec2(0,1)); sprite4->setPosition(Vec2(origin.x + 15,visibleSize.height + origin.y - sprite3->boundingBox().size.height - 15)); this->addChild(sprite4,0); auto touchListener = EventListenerTouchOneByOne::create(); <span style="white-space:pre"> </span>touchListener->onTouchBegan = [sprite4](Touch* t,Event * e) { <span style="white-space:pre"> </span>if (sprite4->getBoundingBox().containsPoint(t->getLocation())) { <span style="white-space:pre"> </span>auto director = Director::getInstance(); auto scene = FengJing::createScene(); director->replaceScene(scene); <span style="white-space:pre"> </span>} return false; };
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(touchListener,this);