Java 将BitmapFont.draw从自定义参与者添加到滚动窗格/表格
我已经创建了一个名为Java 将BitmapFont.draw从自定义参与者添加到滚动窗格/表格,java,libgdx,Java,Libgdx,我已经创建了一个名为TestScrollActor的自定义Actor,它在draw方法上调用BitmapFont.drawwrapped CharSequence/Text将不时追加,因此我需要将此自定义Actor添加到我的垂直滚动表中。文本显示正确,但根本不会滚动 下面是我的完整工作类MyStage,它实现了Screen和我的Custom Actor: import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputMultiplexer;
TestScrollActor
的自定义Actor
,它在draw
方法上调用BitmapFont.drawwrapped
CharSequence/Text
将不时追加,因此我需要将此自定义Actor
添加到我的垂直滚动表中。文本显示正确,但根本不会滚动
下面是我的完整工作类MyStage
,它实现了Screen
和我的Custom Actor
:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.StretchViewport;
public class MyStage implements Screen{
private BitmapFont font;
private String text = "The modern encyclopedia was developed from the "
+ "dictionary in the 18th century. Historically, both encyclopedias "
+ "and dictionaries have been researched and written by well-educated, "
+ "well-informed content experts, but they are significantly different "
+ "in structure. A dictionary is a linguistic work which primarily focuses "
+ "on alphabetical listing of words and their definitions. Synonymous words "
+ "and those related by the subject matter are to be found scattered around "
+ "the dictionary, giving no obvious place for in-depth treatment. Thus, a dictionary "
+ "typically provides limited information, analysis or background for the word defined."
+ " While it may offer a definition, it may leave the reader lacking in understanding the meaning,"
+ " significance or limitations of a term, and how the term relates to a broader field of knowledge. "
+ "An encyclopedia is, allegedly, not written in order to convince, although one of its goals is indeed "
+ "to convince its reader about its own veracity. In the terms of Aristotle's Modes of persuasion, a "
+ "dictionary should persuade the reader through logos (conveying only appropriate emotions); it will be"
+ " expected to have a lack of pathos (it should not stir up irrelevant emotions), and to have little ethos "
+ "except that of the dictionary itself. To address those needs, an encyclopedia article is typically not limited to "
+ "simple definitions, and is not limited to defining an individual word, but provides a more extensive meaning for a "
+ "subject or discipline. In addition to defining and listing synonymous terms for the topic, the article is able to treat "
+ "the topic's more extensive meaning in more depth and convey the most relevant accumulated knowledge on that subject. "
+ "An encyclopedia article also often includes many maps and illustrations, as well as bibliography and statistics.";
private Stage stage;
public static float myGameWidth = 400;
public static float myGameHeight = 640;
@Override
public void show() {
stage = new Stage(new StretchViewport(myGameWidth,myGameHeight));
InputMultiplexer inputM = new InputMultiplexer();
inputM.addProcessor(stage);
Gdx.input.setInputProcessor(inputM);
font = new BitmapFont();
TextScrollActor textScrollActor = new TextScrollActor(text);
//create scrollable table
LabelStyle labelStyle = new LabelStyle();
labelStyle.font = font;
labelStyle.fontColor = Color.WHITE;
final Table scrollTable = new Table();
scrollTable.top();
scrollTable.add(textScrollActor).height(300);
scrollTable.row();
final ScrollPane scroller = new ScrollPane(scrollTable);
final Table table = new Table();
table.setSize(300, 400);
table.setPosition(25, 300);
table.add(scroller).fill().expand();
this.stage.addActor(table);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Update the stage
stage.draw();
stage.act(delta);
}
@Override
public void resize(int width, int height) {}
@Override
public void dispose() {}
@Override
public void hide() {dispose();}
@Override
public void pause() {}
@Override
public void resume() {}
public class TextScrollActor extends Actor{
private BitmapFont font;
private String text;
public TextScrollActor(String text) {
this.text = text;
font = new BitmapFont();
font.setColor(Color.WHITE);
}
@Override
public void draw(Batch batch, float parentAlpha) {
font.drawWrapped(batch,text,50, 450, 280);
}
}
}
列表项
您的问题是大小为0(W=0,H=0)的TextScrollActor
如果您制作自己的演员,并且它具有自定义绘图功能,则应始终注意其宽度和高度值,并自行更改Actor
默认情况下使用(0,0)的大小,不同的实现会根据其绘图内容对其进行更改。因此,要使滚动工作,您只需计算文本占用的空间(一些BitmapFont#getBounds
方法应该可以帮助您),并将该尺寸设置为演员宽度和高度。这应该行得通
另一个问题是,为什么要实现自己的actor?因为这个案例完全由配置皮肤的Label
覆盖。谢谢@Metaphone…你完全正确,不需要创建自定义演员,只需使用标签就可以很好地工作。再次感谢