bool UIButtonTextOnly::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Text Only Button", "fonts/Marker Felt.ttf",32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create(); button->setNormalizedPosition(Vec2(0.5f, 0.5f)); button->setTitleText("PLAY GAME"); CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width, button->getContentSize().height); button->setZoomScale(0.3f); button->setPressedActionEnabled(true); button->addClickEventListener([this](Ref* sender) { CCLOG("clicked!"); }); _uiLayer->addChild(button); return true; } return false; }
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } FileUtils::getInstance()->addSearchPath("particles/materials"); FileUtils::getInstance()->addSearchPath("particles/scripts"); FileUtils::getInstance()->addSearchPath("models"); // Particles are created with Particle Universe editor auto particle = PUParticleSystem3D::create("blackHole.pu", "pu_mediapack_01.material"); // auto particle = PUParticleSystem3D::create("explosionSystem.pu"); // auto particle = PUParticleSystem3D::create("hypno.pu", "pu_mediapack_01.material"); this->addChild(particle); particle->setNormalizedPosition(Vec2(0.5,0.5)); particle->setScale(20); particle->startParticleSystem(); return true; }
bool UIButtonIgnoreContentSizeTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Button IgnoreContent Size Test", "fonts/Marker Felt.ttf",32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->ignoreContentAdaptWithSize(false); button->setContentSize(Size(200,100)); button->setNormalizedPosition(Vec2(0.3f, 0.5f)); button->setTitleText("PLAY GAME"); button->setZoomScale(0.3f); button->setPressedActionEnabled(true); button->addClickEventListener([=](Ref* sender) { CCLOG("clicked!"); button->setScale(1.2f); }); _uiLayer->addChild(button); // Create the button auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button2->ignoreContentAdaptWithSize(false); button2->setContentSize(Size(200,100)); button2->setNormalizedPosition(Vec2(0.8f, 0.5f)); button2->setTitleText("PLAY GAME"); button2->setZoomScale(0.3f); button2->setPressedActionEnabled(true); button2->addClickEventListener([=](Ref* sender) { button2->runAction(ScaleTo::create(1.0f, 1.2f)); CCLOG("clicked!"); }); _uiLayer->addChild(button2); return true; } return false; }
bool StartScene::init(){ if (!Scene::init()){ return false; } //添加坦克世界的图片 AudioInit(); auto title = Sprite::create("BattleCity.png"); title->setNormalizedPosition(Vec2(0.5, 0.6)); this->addChild(title); //添加开始游戏的按钮 auto item = MenuItemFont::create("StartGame", [](Ref*){ Director::getInstance()->replaceScene(GameScene::create()); }); auto menu = Menu::create(item, nullptr); menu->setPosition(0, 0); item->setNormalizedPosition(Vec2(0.5,0.2)); this->addChild(menu); return true; }
bool UIButtonFlipTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Button X Flipped", "fonts/Marker Felt.ttf",20); _displayValueLabel->setNormalizedPosition(Vec2(0.3, 0.7)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setNormalizedPosition(Vec2(0.3f, 0.5f)); button->setTitleText("PLAY GAME"); button->setTitleFontName("fonts/Marker Felt.ttf"); button->setZoomScale(0.3f); button->setScale(2.0f); button->setFlippedX(true); button->setPressedActionEnabled(true); _uiLayer->addChild(button); // Create the button auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button2->setNormalizedPosition(Vec2(0.8f, 0.5f)); button2->setTitleText("PLAY GAME"); button2->setFlippedY(true); _uiLayer->addChild(button2); auto titleLabel = Text::create("Button Y flipped", "Arial", 20); titleLabel->setNormalizedPosition(Vec2(0.8, 0.7)); this->addChild(titleLabel); return true; } return false; }
bool UIButtonTitleEffectTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Button Title Effect", "fonts/Marker Felt.ttf",32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setNormalizedPosition(Vec2(0.3f, 0.5f)); button->setTitleText("PLAY GAME"); button->setTitleFontName("fonts/Marker Felt.ttf"); button->setZoomScale(0.3f); button->setScale(2.0f); button->setPressedActionEnabled(true); Label *title = button->getTitleRenderer(); button->setTitleColor(Color3B::RED); title->enableShadow(Color4B::BLACK,Size(2,-2)); _uiLayer->addChild(button); // Create the button auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button2->setNormalizedPosition(Vec2(0.8f, 0.5f)); button2->setTitleText("PLAY GAME"); auto title2 = button2->getTitleRenderer(); title2->enableOutline(Color4B::GREEN, 3); _uiLayer->addChild(button2); return true; } return false; }
//------------------------------------------------------------------ // // Billboard Rotation Test // //------------------------------------------------------------------ BillBoardRotationTest::BillBoardRotationTest() { auto root = Sprite3D::create(); root->setNormalizedPosition(Vec2(.5,.25)); addChild(root); auto model = Sprite3D::create("Sprite3DTest/orc.c3b"); model->setScale(5); model->setRotation3D(Vec3(0,180,0)); root->addChild(model); auto bill = BillBoard::create(); bill->setPosition(0, 120); root->addChild(bill); auto sp = Sprite::create("Images/SpookyPeas.png"); sp->setScale(2); bill->addChild(sp); auto lbl = Label::create(); lbl->setPosition(0, 30); lbl->setString("+100"); bill->addChild(lbl); auto r = RotateBy::create(10, Vec3(0,360,0)); auto rp = RepeatForever::create(r); root->runAction(rp); auto jump = JumpBy::create(1, Vec2(0, 0), 30, 1); auto scale = ScaleBy::create(2, 2, 2, 0.1); auto seq = Sequence::create(jump,scale, NULL); auto rot = RotateBy::create(2, Vec3(-90, 0, 0)); auto act = Spawn::create(seq, rot,NULL); auto scale2 = scale->reverse(); auto rot2 = rot->reverse(); auto act2 = Spawn::create(scale2, rot2, NULL); auto seq2 = Sequence::create(act, act2, NULL); auto repeat = RepeatForever::create(seq2); model->runAction(repeat); }
bool ControlLayer::init() { if (!Layer::init()) { return false; } //设置摇杆的图片精灵 auto joyStick = Sprite::create("control_bg.png"); this->addChild(joyStick); joyStick->setPosition(100, 100); joyStick->setOpacity(100); //设置摇杆上的控制器的精灵 auto stick = Sprite::create("cen.png"); stick->setOpacity(150); stick->setVisible(false); stick->setNormalizedPosition(Vec2(0.5, 0.5)); joyStick->addChild(stick); auto lisenter = EventListenerTouchOneByOne::create(); lisenter->onTouchBegan = [=](Touch *touch, Event* event){ //获取触摸点 auto point = touch->getLocation(); auto j_point = joyStick->getPosition(); if (sqrt((point.x - j_point.x)*(point.x - j_point.x) + (point.y - j_point.y)*(point.y - j_point.y))<joyStick->getContentSize().width/2){ stick->setVisible(true); stick->setPosition(point); } return true; }; lisenter->onTouchMoved = [=](Touch *touch, Event* event){ //获取触摸点 auto point = touch->getLocation(); auto j_point = joyStick->getPosition(); if (sqrt((point.x - j_point.x)*(point.x - j_point.x) + (point.y - j_point.y)*(point.y - j_point.y)) < joyStick->getContentSize().width / 2+10){ /*stick->setVisible(true);*/ stick->setPosition(point); //判断移动方向 int sub_x = point.x - j_point.x; int sub_y = point.y - j_point.y; if (tank == nullptr) { return ; } if (sub_y > 0 && sub_y > abs(sub_x)) //上 { tank->setState(Up); tank->setisStop(false); } if (sub_x<0 && abs(sub_x)>abs(sub_y)) //左 { tank->setState(Left); tank->setisStop(false); } if (sub_x > 0 && abs(sub_x) > abs(sub_y)) //右 { tank->setState(Right); tank->setisStop(false); } if (sub_y<0 && abs(sub_y)>abs(sub_x)) //下 { tank->setState(Down); tank->setisStop(false); } } else { stick->setPosition(joyStick->getPosition()); } }; lisenter->onTouchEnded = [=](Touch *touch, Event* event){ stick->setVisible(false); if (tank == nullptr) { return; } tank->setisStop(true); }; //设置发射子弹的按钮 auto fireItem = MenuItemImage::create("fire_button_default.png", "fire_button_press.png", [=](Ref* pSender){ if (tank == nullptr) { return; } tank->fire(); }); fireItem->setPosition(700,100); fireItem->setOpacity(120); auto menu = Menu::create(fireItem, nullptr); menu->setPosition(0, 0); this->addChild(menu,1); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(lisenter,this); return true; }