2014-12-28 43 views
0

我創建了一個自定義Actor,調用TestScrollActor,它的draw方法調用BitmapFont.drawWraped將BitmapFont.draw從自定義演員添加到ScrollPane /表

CharSequence/Text將不時追加,因此我需要將此自定義Actor添加到我的垂直滾動Table。文本顯示正確,但它根本不滾動。

這裏是我的全工人階級MyStage它實現ScreenCustom 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); 
     } 
    } 
} 

回答

2
  1. 列表項

你的問題是0尺寸(W = 0的TextScrollActor,H = 0 )。

如果你讓你自己的演員,並有自定義繪圖功能,你應該時刻注意它的寬度和高度值,並自己改變它。 Actor默認使用(0,0)的大小,並且不同的實現根據其內容的內容對其進行更改。因此,爲了使滾動工作,你只需要計算文本需要的空間(BitmapFont#getBounds方法中的一些應該可以幫助你)並將該維度設置爲Actor寬度和高度。這應該工作。

其他問題是爲什麼你實施自己的演員?因爲這個案例完全覆蓋了Label與配置的皮膚。

+0

謝謝@Metaphone ......你是完全正確的,沒有必要創建一個自定義的演員,它只用標籤就能很好地工作。 再次感謝 – Devester