【问题标题】:Cocos2d-x how to create image button with title and fontCocos2d-x 如何创建带有标题和字体的图片按钮
【发布时间】:2018-03-09 15:23:32
【问题描述】:

您好,我正在将我的游戏从 Corona SDK 转换为 Cocos2d-x 3.0 alpha。

我需要创建一个带有文本的图像按钮。在带有 widget.newButton 的 Corona SDK 中非常简单,它将所有 x, y, size, font, image 等放在一个函数中。

现在我在 Cocos2d-x 中找不到任何替代品。我在其中找到的最接近的东西是MenuItemImage

auto closeItem = MenuItemImage::create(
                                    "blank.png",
                                    "blank-selected.png",
                                     CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2));

auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);

它需要imagesevent,但我不能在上面设置titlefont。有人知道如何设置标题和字体吗?

【问题讨论】:

    标签: ios iphone cocos2d-x game-engine


    【解决方案1】:

    您可以使用MenuItemFontMenuItemLabel

    例如:

    MenuItemFont::setFontName( "Marker Felt" );
    MenuItemFont::setFontSize( 34 );
    
    auto label = LabelBMFont::create( "go back", "fonts/bitmapFontTest3.fnt" );
    auto back = MenuItemLabel::create(label, CC_CALLBACK_1(MenuLayer4::backCallback, this) );
    

    MenuItemFont::setFontSize( 34 );
    MenuItemFont::setFontName("Marker Felt");
    auto item6 = MenuItemFont::create("Bugs", CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackBugsTest, this));
    

    欲了解更多信息,请参阅MenuTest.ccp


    更新

    您可以简单地新建一个Label,并将其添加到您MenuItemImage,例如:

    LabelTTF* closeLabel = LabelTTF::create("close", "Marker Felt", 28);
    closeItem->addChild(closeLabel);
    

    您可能需要调整标签的位置。

    【讨论】:

    • 感谢回复,但是这个例子中怎么设置图片呢?根据我的问题,我需要一种方法来创建带有标题和字体的图像。我创建了一个“空白”图像,需要在整个应用程序中使用。我只需要用代码在上面添加标题。
    • 所以你的意思是我需要分别创建 LabelTTF 和 MenuItemImage 并调整两者的位置?
    • 谢谢@pktangyue,我遇到了另一个问题。你能调查一下吗? stackoverflow.com/questions/20139335/…
    【解决方案2】:

    这相当简单,至少在 cocos2d-x 3.10 和 3.16 中有效,但如果你想用 cocos2d 制作按钮,请使用 cocos2d::ui 的内置命名空间Button

    cocos2d::ui::Button* button = cocos2d::ui::Button::create();
    //button textures
    button->loadTextures(
            "<your default texture>.png",
            "<your pressed texture>.png",
            "<your disabled texture>.png",
            cocos2d::ui::Widget::TextureResType::PLIST
        );
    
    //text content, font, and size
    button->setTitleText("Touch me!");
    button->setTitleFontName("arial");
    button->setTitleFontSize(24.0f);
    
    //bind a callback with a lambda
    auto touch_handler = [](cocos2d::Ref* ref, cocos2d::ui::Widget::TouchEventType evt)
    {
        if (evt == cocos2d::ui::Widget::TouchEventType::ENDED)
        {
            //do your callback logic here, ie play a sound or vibrate
        }
    };
    button->addTouchEventListener(touch_handler);
    
    //then finally, like a Nodes, add it to your scene
    my_scene->addChild(button);
    

    理想情况下,您将加载的纹理使用 TexturePacker 之类的东西打包到一个纹理中,否则您将其更改为 LOCAL 而不是 PLIST。当按钮是默认的,或者有触摸,或者按钮被禁用时,纹理将被使用。

    【讨论】:

      猜你喜欢
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多