Thoughts on textbutton in libgdx

Source: Internet
Author: User
Tags libgdx

This is because you need to implement the following textbutton. Then I went to the libgdx documentation. Buttons in the game are often used to changing images. Few people directly use textbutton in libgdx. If not, they write a textbutton class.

With "does it really have such a scum attitude?", I took a look at the textbutton that comes with libgdx. The following are my thoughts. As follows:

At present, there are so few libgdx materials, and some of them are also relatively basic. It is better to go to the official documents on your own to read other people's documents ." You have started the following processes...

1. First, I saw the following usage in his GDX-test example:

new TextButton("Flick Scroll", skin.get("toggle", TextButtonStyle.class));

2. Skin is used here, which has been criticized by colleagues. However, I read the official documents of the skin class with my learning attitude.

The following are my thoughts and notes on skin learning:


3. Learning and analyzing textbutton.

Follow these steps to learn a class: "If you have an official demo, go to the official demo first. After you have mastered the basic usage, go to the source code of the class "..

1) The following are the basic usage instructions:

// Use skin to store the styletextbuttonstyle textbuttonstyle = new textbuttonstyle (); textbuttonstyle. fontcolor = color. red; // textbuttonstyle does not work. font = new bitmapfont (GDX. files. internal ("hjd. fnt "), GDX. files. internal ("hjd.png"), false); // textbuttonstyle. font. setcolor (color. red); // does not work // textbuttonstyle. downfontcolor = color. blue; skin. add ("style", textbuttonstyle); textbutton = new textbutton ("Hello textbutton", skin, "style"); // textbutton. getlabel (). getstyle (). fontcolor = color. yellow; // does not work // textbutton. getlabel (). setcolor (color. red); // textbutton does not work. setposition (50, 50); // textbutton. setcolor (color. red); // stage does not work. addactor (image); stage. addactor (textbutton );

2) Source Code Analysis

First paste the source code of textbutton:

Here we only look at three functions :. After textbutton (string, skin, string) is called, it calls the constructor textbutton (string, textbuttonstyle) internally. This constructor removes the label constructor. Therefore, the style. font and fontcolor objects must be initialized. Otherwise, an exception is reported...

public TextButton (String text, Skin skin, String styleName) {this(text, skin.get(styleName, TextButtonStyle.class));setSkin(skin);}public TextButton (String text, TextButtonStyle style) {super(style); = style;label = new Label(text, new LabelStyle(style.font, style.fontColor));label.setAlignment(;add(label).expand().fill();setWidth(getPrefWidth());setHeight(getPrefHeight());}

As for the draw () function, I think that's why textbutton fails to write .... It's too scum ..... Grace, yes. It turns out that the official textbutton is indeed too scum... You cannot set the font color .... The provided API is ineffective. Click here to see if some functions have not been implemented yet... Grass .....
public void draw (SpriteBatch batch, float parentAlpha) {Color fontColor;if (isDisabled && style.disabledFontColor != null)fontColor = style.disabledFontColor;else if (isPressed() && style.downFontColor != null)fontColor = style.downFontColor;else if (isChecked && style.checkedFontColor != null)fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;else if (isOver() && style.overFontColor != null)fontColor = style.overFontColor;elsefontColor = style.fontColor;if (fontColor != null) label.getStyle().fontColor = fontColor;super.draw(batch, parentAlpha);}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.