Exemplo n.º 1
0
bool CCControlButtonTest_HelloVariableSize::init()
{
    if (CCControlScene::init())
    {
        CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
        
        // Defines an array of title to create buttons dynamically
        CCArray *stringArray = CCArray::create(
            ccs("Hello"),
            ccs("Variable"),
            ccs("Size"),
            ccs("!"),
            NULL);
        
        CCNode *layer = CCNode::create();
        addChild(layer, 1);
        
        double total_width = 0, height = 0;
        
        // For each title in the array
        CCObject* pObj = NULL;
        CCARRAY_FOREACH(stringArray, pObj)
        {
            CCString* title = (CCString*)pObj;
            // Creates a button with this string as title
            CCControlButton *button = standardButtonWithTitle(title->getCString());
            button->setPosition(ccp (total_width + button->getContentSize().width / 2, button->getContentSize().height / 2));
            layer->addChild(button);
            
            // Compute the size of the layer
            height = button->getContentSize().height;
            total_width += button->getContentSize().width;
        }

        layer->setAnchorPoint(ccp (0.5, 0.5));
        layer->setContentSize(CCSizeMake(total_width, height));
        layer->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
        
        // Add the black background
        CCScale9Sprite *background = CCScale9Sprite::create("extensions/buttonBackground.png");
        background->setContentSize(CCSizeMake(total_width + 14, height + 14));
        background->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
        addChild(background);
        return true;
    }
Exemplo n.º 2
0
void AboutLayer::addMenu()
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Point origin = Director::getInstance()->getVisibleOrigin();
	Size wndSize = Director::getInstance()->getWinSize();
	auto backSprite = Sprite::create("background.png");
	Size bg = backSprite->getContentSize();
	backSprite->setScale(wndSize.width/bg.width,wndSize.height/bg.height);
	//backSprite->setContentSize(Size(wndSize.width,wndSize.height));
	backSprite->setPosition(Point(wndSize.width/2,wndSize.height/2));
	this->addChild(backSprite,-1);

	//int width = 0 ,height =0;
	pScore_ = standardButtonWithTitle("历史分数");
	pFps_ = standardButtonWithTitle("帧率显示");
	pAbout_ = standardButtonWithTitle("关于游戏");
	pBack_ = standardButtonWithTitle("返回继续");
	pQuit_ = standardButtonWithTitle("结束游戏");
	pReStart_ =standardButtonWithTitle("新的一局");


// 	pQuit_->setContentSize(Size(width,height));
// 	pBack_->setContentSize(Size(width,height));
// 	pAbout_->setContentSize(Size(width,height));
// 	pScore_->setContentSize(Size(width,height));

	int cy = visibleSize.height/2/5;
	int ypos= visibleSize.height/4 ;
	int xpos = origin.x + visibleSize.width/2;
	ypos += cy;
	pQuit_->setPosition(xpos,ypos);
	ypos += cy;
	pAbout_->setPosition(xpos,ypos);
	ypos += cy;
	pReStart_->setPosition(xpos,ypos);
	ypos += cy;
	pBack_->setPosition(xpos,ypos);
	ypos += cy;
	pFps_->setPosition(xpos,ypos);
	ypos += cy;
	pScore_->setPosition(xpos,ypos);

	std::string top = "Cocos2d-x3.0Alpha1\nBY\[email protected]";
	pInfoLabel_ = LabelTTF::create(top, "Marker Felt", 30);
	//pInfoLabel_->setAnchorPoint(Point(0,0.5));
	pInfoLabel_->setPosition(Point(origin.x + visibleSize.width/2,
		origin.y + /*visibleSize.height -*/ pInfoLabel_->getContentSize().height ));
	pInfoLabel_->setTag(ID_LABEL_IFNO);
	pInfoLabel_->setVisible(false);
	pInfoLabel_->retain();
	this->addChild(pInfoLabel_);
}
Exemplo n.º 3
0
bool ControlButtonTest_Styling::init()
{
    if (ControlScene::init())
    {
        auto screenSize = Director::getInstance()->getWinSize();

        auto layer = Node::create();
        addChild(layer, 1);
        
        int space = 10; // px
        
        double max_w = 0, max_h = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                // Add the buttons
                ControlButton *button = standardButtonWithTitle(StringUtils::format("%d",rand() % 30).c_str());
                button->setAdjustBackgroundImage(false);  // Tells the button that the background image must not be adjust
                                                    // It'll use the prefered size of the background image
                button->setPosition(button->getContentSize().width / 2 + (button->getContentSize().width + space) * i,
                                       button->getContentSize().height / 2 + (button->getContentSize().height + space) * j);
                layer->addChild(button);
                
                max_w = MAX(button->getContentSize().width * (i + 1) + space  * i, max_w);
                max_h = MAX(button->getContentSize().height * (j + 1) + space * j, max_h);
            }
        }
        
        layer->setAnchorPoint(Vec2(0.5, 0.5));
        layer->setContentSize(Size(max_w, max_h));
        layer->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f);
        
        // Add the black background
        auto backgroundButton = ui::Scale9Sprite::create("extensions/buttonBackground.png");
        backgroundButton->setContentSize(Size(max_w + 14, max_h + 14));
        backgroundButton->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f);
        addChild(backgroundButton);
        return true;
    }
    return false;
}
Exemplo n.º 4
0
bool ControlButtonTest_HelloVariableSize::init()
{
    if (ControlScene::init())
    {
        auto screenSize = Director::getInstance()->getWinSize();
        
        // Defines an array of title to create buttons dynamically
        std::vector<std::string> vec;
        vec.push_back("Hello");
        vec.push_back("Variable");
        vec.push_back("Size");
        vec.push_back("!");
        
        auto layer = Node::create();
        addChild(layer, 1);
        
        double total_width = 0, height = 0;
        
        int i = 0;
        
        for (auto& title : vec)
        {
            // Creates a button with this string as title
            ControlButton *button = standardButtonWithTitle(title.c_str());
            if (i == 0)
            {
                button->setOpacity(50);
                button->setColor(Color3B(0, 255, 0));
            }
            else if (i == 1)
            {
                button->setOpacity(200);
                button->setColor(Color3B(0, 255, 0));
            }
            else if (i == 2)
            {
                button->setOpacity(100);
                button->setColor(Color3B(0, 0, 255));
            }
            
            button->setPosition(total_width + button->getContentSize().width / 2, button->getContentSize().height / 2);
            layer->addChild(button);
            
            // Compute the size of the layer
            height = button->getContentSize().height;
            total_width += button->getContentSize().width;
            i++;
        }

        layer->setAnchorPoint(Vec2 (0.5, 0.5));
        layer->setContentSize(Size(total_width, height));
        layer->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f);
        
        // Add the black background
        auto background = ui::Scale9Sprite::create("extensions/buttonBackground.png");
        background->setContentSize(Size(total_width + 14, height + 14));
        background->setPosition(screenSize.width / 2.0f, screenSize.height / 2.0f);
        addChild(background);
        return true;
    }
    return false;
}
Exemplo n.º 5
0
// on "init" you need to initialize your instance
bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();
	
	//	LabelTTF * tip = LabelTTF::create("很抱歉 答错啦", "AmericanTypewriter", 30);//添加文字

    guanka_index = LabelTTF::create("AmericanTypewriter", "Arial", 40);
    
    // position the label on the center of the screen
    guanka_index->setPosition(Point(origin.x + visibleSize.width/2,
							 origin.y + visibleSize.height - guanka_index->getContentSize().height/4*3));
	
    // add the label as a child to this layer
	 this->addChild(guanka_index, 1);
//	topic_title->retain();
	
	topic_title = LabelTTF::create("Hello World", "Arial", 24);
    topic_title->setPosition(Point(origin.x + visibleSize.width/2,
								   origin.y + visibleSize.height - topic_title->getContentSize().height- guanka_index->getContentSize().height));
	
    // add the label as a child to this layer
	this->addChild(topic_title, 1);
	
	gold = LabelTTF::create("209", "Arial", 36);
	gold->setHorizontalAlignment(TextHAlignment::RIGHT);
	gold->setAnchorPoint(Point(1,0.5f));
    gold->setPosition(Point(origin.x + visibleSize.width - 20,
								   origin.y + visibleSize.height - 45));
	
    // add the label as a child to this layer
	this->addChild(gold, 1);
	
	
 
	Layer* backLayer =	createCommonBackLayer();
	//nav
	Sprite* nav = createGameNavBg();
	backLayer->addChild(nav);
	
	//back
	
	auto backItem = MenuItemImage::create(
                                           "play_back.png",
                                           "play_back_selected.png",
                                           CC_CALLBACK_1(GameScene::menuBackCallback, this));
	backItem->setAnchorPoint(Point(0.0f, 0.0f));
	
	backItem->setPosition(Point(origin.x + 10,  nav->getPosition().y + 50));
	
    // create menu, it's an autorelease object
    auto menu = Menu::create(backItem, NULL);
    menu->setPosition(Point::ZERO);
    backLayer->addChild(menu, 1);
	
	// coin bg
	
	auto coinBgkItem = MenuItemImage::create(
										  "play_navbar_getcoin.png",
										  "play_navbar_getcoin_selected.png",
										  CC_CALLBACK_1(GameScene::menuGoldCallback, this));
	coinBgkItem->setAnchorPoint(Point(0.0f, 0.0f));
	
	coinBgkItem->setPosition(Point(origin.x + visibleSize.width - coinBgkItem->getContentSize().width - 10, nav->getPosition().y + 60));
	
    // create menu, it's an autorelease object
    auto menu1 = Menu::create(coinBgkItem, NULL);
    menu1->setPosition(Point::ZERO);
    backLayer->addChild(menu1, 1);
	
	//help
	//
	
	auto shareItem = MenuItemImage::create(
											 "play_share.png",
											 "play_share_selected.png",
											 CC_CALLBACK_1(GameScene::menuShareCallback, this));
	shareItem->setAnchorPoint(Point(0.5f, 0.5f));
	
	shareItem->setPosition(Point(origin.x + 10 + shareItem->getContentSize().width/2, origin.y + visibleSize.height/2 + shareItem->getContentSize().height * 2));
	
    // create menu, it's an autorelease object
    auto menu2 = Menu::create(shareItem, NULL);
    menu2->setPosition(Point::ZERO);
    backLayer->addChild(menu2, 1);
	
	//bomb
	auto bombItem = MenuItemImage::create(
										   "play_bombtool.png",
										   "play_bombtool_selected.png",
										   CC_CALLBACK_1(GameScene::menuBombCallback, this));
	bombItem->setAnchorPoint(Point(0.5f, 0.5f));
	bombItem->setPosition(Point(origin.x + visibleSize.width - bombItem->getContentSize().width/2 -10, origin.y + visibleSize.height/2  + bombItem->getContentSize().height * 2));
	
    // create menu, it's an autorelease object
    auto menu3 = Menu::create(bombItem, NULL);
    menu3->setPosition(Point::ZERO);
    backLayer->addChild(menu3, 1);
	
	// tv Bg
	Sprite* tvBg = Sprite::create("play_video_bg.png");
	bombItem->setAnchorPoint(Point(0.5f, 0.5f));
	tvBg->setPosition(origin.x + visibleSize.width/2 + 20, origin.y + visibleSize.height/2 + tvBg->getContentSize().height/2); // nav->getPositionY() - 260);
	
	backLayer->addChild(tvBg);

	//play
	auto playItem = MenuItemImage::create(
                                           "play_video_button.png",
                                           "play_video_button.png",
                                           CC_CALLBACK_1(GameScene::menuPlayCallback, this));
	playItem->setAnchorPoint(Point(0.5f, 0.5f));
	
	playItem->setPosition(Point(origin.x + 485, origin.y + visibleSize.height/2 + 70));
	
    // create menu, it's an autorelease object
    auto menu4 = Menu::create(playItem, NULL);
    menu4->setPosition(Point::ZERO);
    backLayer->addChild(menu4, 1);

	// question
	String* str = String::createWithFormat("");
	
	question = LabelTTF::create(str->getCString(), "AmericanTypewriter-Bold", 30, Size(300, 200), TextHAlignment::CENTER, cocos2d::TextVAlignment::CENTER);
	question->setAnchorPoint(Point(0.5f,0.5f));
	question->setPosition(origin.x + visibleSize.width/2, tvBg->getPosition().y);

	backLayer->addChild(question);
	
	string title1 = "";
	string title2 = "";
	string title3 = "";
	string title4 = "";
	
//522 88

	btn1 = standardButtonWithTitle("play_answer_a_bg.png", "play_answer_a_bg.png", "");
	btn2 = standardButtonWithTitle("play_answer_b_bg.png", "play_answer_b_bg.png", "");
	btn3 = standardButtonWithTitle("play_answer_c_bg.png", "play_answer_c_bg.png", "");
	btn4 = standardButtonWithTitle("play_answer_d_bg.png", "play_answer_d_bg.png", "");
	
	int ybase = origin.y + visibleSize.height/2 - btn1->getContentSize().height/2;
	int offset = 20;

	btn1->setAnchorPoint(Point(0.5f, 0.5f));
	btn2->setAnchorPoint(Point(0.5f, 0.5f));
	btn3->setAnchorPoint(Point(0.5f, 0.5f));
	btn4->setAnchorPoint(Point(0.5f, 0.5f));

	btn1->setPosition( visibleSize.width/2 ,ybase - (btn1->getContentSize().height + offset) * 0 );
	btn2->setPosition( visibleSize.width/2 ,ybase - (btn1->getContentSize().height + offset) * 1 );
	btn3->setPosition( visibleSize.width/2 ,ybase - (btn1->getContentSize().height + offset) * 2 );
	btn4->setPosition( visibleSize.width/2 ,ybase - (btn1->getContentSize().height + offset) * 3);
	
	btn1->addTargetWithActionForControlEvents(this, cccontrol_selector( GameScene::btn1callBack),Control::EventType::TOUCH_UP_INSIDE);
	btn2->addTargetWithActionForControlEvents(this, cccontrol_selector( GameScene::btn2callBack),Control::EventType::TOUCH_UP_INSIDE);
	btn3->addTargetWithActionForControlEvents(this, cccontrol_selector( GameScene::btn3callBack),Control::EventType::TOUCH_UP_INSIDE);
	btn4->addTargetWithActionForControlEvents(this, cccontrol_selector( GameScene::btn4callBack),Control::EventType::TOUCH_UP_INSIDE);

	backLayer->addChild(btn1);
	backLayer->addChild(btn2);
	backLayer->addChild(btn3);
	backLayer->addChild(btn4);

	this->addChild(backLayer);
    
	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = NULL;
	listener->onTouchEnded = NULL;
	
	EventDispatcher* eventDispatcher = backLayer->getEventDispatcher();
	//eventDispatcher->addEventListenerWithSceneGraphPriority( listener, backLayer);
	
//	answerStateArr_
//	GameAnswerState1* 
	
	for (int i = 0; i<4; i++) {
		answerStateArr_[i] = GameAnswerState::GameStateAnswerStateInit;
	}
	
	return true;
}