Libgdx中TextButton的一些思考,libgdxbutton
因為有要實現以下TextButton的這個需求。然後就去看了一下Libgdx中文檔。遊戲中的按鈕,很多人都比較習慣使用換圖片的方式來實現。很少有人會直接使用libgdx中的TextButton,如果實在不行也是自己去寫一個TextButton的類。
抱著“它真的有那麼渣的態度嗎”,我去看了一下libgdx內建的TextButton。以下是我的思考的軌跡。整理如下:
在現在,libgdx的資料那麼少,有的那些資料也是比較基礎的。抱著“看別人的,還不如自己去官方文檔。”的態度,自己就開始了以下的曆程。。。
1、首先是看了他官方提供的Gdx-test的例子中有以下的這個用法:
new TextButton("Flick Scroll", skin.get("toggle", TextButtonStyle.class));
2、這裡面用到了Skin這個類,Skin這個類一直被同事詬病。但是我還是抱著學習的態度去看了一下Skin這個類的官方文檔。
以下是自己對Skin類學習以後的一些思考與筆記:
http://blog.csdn.net/hjd_love_zzt/article/details/37566435
3、 對TextButton的學習與分析。
對一個類的學習還是按照以下思路:“如果有官方demo,就先去看官方的demo。掌握基本使用以後,然後去看那個類的源碼”。。
1)以下是自己整理出來的基本使用:
//使用Skin來儲存群組件的styleTextButtonStyle textButtonStyle = new TextButtonStyle();textButtonStyle.fontColor = Color.RED;//不起作用textButtonStyle.font = new BitmapFont(Gdx.files.internal("hjd.fnt"), Gdx.files.internal("hjd.png"),false);//textButtonStyle.font.setColor(Color.RED);//不起作用//textButtonStyle.downFontColor = Color.BLUE;skin.add("style", textButtonStyle);textButton = new TextButton("hello textButton", skin, "style");//textButton.getLabel().getStyle().fontColor = Color.YELLOW;//沒起作用//textButton.getLabel().setColor(Color.RED);//沒起作用textButton.setPosition(50, 50);//textButton.setColor(Color.RED);//不起作用stage.addActor(image);stage.addActor(textButton);
2)源碼分析
先貼出TextButton的源碼相關源碼:
這裡只看3個函數:。調用TextButton(String,Skin,String)後,它內部會調TextButton(String,TextButtonStyle)這個建構函式。而這個建構函式中掉了Label的建構函式,所以Style.font、fontColor對象一定要初始化,否則會報相應的異常。。。
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);this.style = style;label = new Label(text, new LabelStyle(style.font, style.fontColor));label.setAlignment(Align.center);add(label).expand().fill();setWidth(getPrefWidth());setHeight(getPrefHeight());}
至於draw()函數,我想這就是為什麼這個TextButton為什麼寫的失敗的原因了吧。。。。實在是太渣了。。。。。恩恩,是的。事實證明官方的TextButton確實是太渣了。。。想要設定個字型的顏色都做不到。。。。使用提供的API沒有效果。點進去看,有的函數居然還沒有實現。。。草。。。。。
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);}
android遊戲引擎libGDX如何添加持續按鍵響應?應該有例子多看看類似androidontouch事件我也沒看libgdxapi過正準備學呢官方有super jumper遊戲例子裡面好像
LIBGDX的問題難道更改圖片之沒項目上按F5