0
我創建了一個自定義Actor
,調用TestScrollActor
,它的draw
方法調用BitmapFont.drawWraped
。將BitmapFont.draw從自定義演員添加到ScrollPane /表
CharSequence/Text
將不時追加,因此我需要將此自定義Actor
添加到我的垂直滾動Table
。文本顯示正確,但它根本不滾動。
這裏是我的全工人階級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);
}
}
}
謝謝@Metaphone ......你是完全正確的,沒有必要創建一個自定義的演員,它只用標籤就能很好地工作。 再次感謝 – Devester