2014-02-20 4 views
1

libgdx에서 스크롤 창을 사용하는 데 문제가 있습니다. 그것은 chatwindow 클래스에 사용될 것입니다. Enter 키를 누르면 메시지가 창에 추가되고 최근 게시 된 메시지로 스크롤됩니다. 그렇지 않은 경우. 하나의 메시지를 놓친 다음 최신 메시지보다 먼저 스크롤합니다. 아래에서는 chatwindow 클래스와 그 클래스에 입력을 추가하는 메서드를 게시했습니다. textAreaholder는 모든 것을 저장하는 테이블입니다. chatField는 채팅에 게시 할 내용을 입력하는 곳입니다. chatarea는 테이블에 추가되는 텍스트 필드입니다. 그러나 명시된 바와 같이 .it 제대로 스크롤되지 않습니다, 오류가 제대로 keyTyped 메서드 어딘가에 놓여 있습니다. 당신이 scrollPane.scrollToCenter(float x, float y, float width, float height) 0 인 매개 변수를 사용하고 있기 때문에chatwindow 클래스의 Libgdx scrollpane 문제

public ChatWindow(final Pipe<String> chatPipe) { 
    this.chatPipe = chatPipe; 
    messageFieldCounter = 0; 

    white = new BitmapFont(Gdx.files.internal("fonts/ChatWindowText.fnt"), false); 
    fontSize = white.getLineHeight(); 
    white.scale(TEXT_SCALE); 
    final TextFilter filter = new TextFilter(); 

    /* Making a textfield style */ 
    textFieldStyle = new TextFieldStyle(); 
    textFieldStyle.fontColor = Color.WHITE; 
    textFieldStyle.font = white; 
    textFieldStyle.focusedFontColor = Color.CYAN; 

    /*Area where all chat appears*/ 
    textAreaHolder = new Table(); 
    textAreaHolder.debug(); 

    /*Applies the scrollpane to the chat area*/ 
    scrollPane = new ScrollPane(textAreaHolder); 
    scrollPane.setForceScroll(false, true); 
    scrollPane.setFlickScroll(true); 
    scrollPane.setOverscroll(false, false); 

    /*Input chat*/ 
    chatField = new TextField("", textFieldStyle); 
    chatField.setTextFieldFilter(filter); 

    /*Tries to make the textField react on enter?*/ 
    chatField.setTextFieldListener(new TextFieldListener() { 
     @Override 
     public void keyTyped(final TextField textField, final char key) { 
      if (key == '\n' || key == '\r') { 
       if (messageFieldCounter <= 50) { 
        textAreaHolder.row(); 
        StringBuilder message = new StringBuilder(); //Creates the message 
        message.append(chatField.getText());   //Appends the chatfield entry 
        TextArea chatArea = new TextArea(message.toString(), textFieldStyle); //Creates a chatArea with the message 
        chatArea.setHeight(fontSize + 1); 
        chatArea.setDisabled(true); 
        chatArea.setTextFieldFilter(filter); 
        textAreaHolder.add(chatArea).height(CHAT_INPUT_HEIGHT).width(CHAT_WIDTH); 

        scrollPane.scrollToCenter(0, 0, 0, 0); 



         //Scrolls to latest input 


       chatField.setText(""); 
       //InputDecider.inputDecision(message.toString(), chatPipe); //TODO: Change the filter 
       //chatPipe.put(message.toString()); //TODO: testing 
       } 
      } 
     } 
    }); 

답변

2

문제가 발생할 수 :

scrollPane.scrollToCenter(0, 0, 0, 0); 

scrollToCenter 방법은 매개 변수가 제대로 공급 될 것을 요구한다. 따라서 메시지 경계를 제공하십시오.

두 번째 이유는 테이블 레이아웃 자체 앞에 scrollToCenter을 호출했기 때문일 수 있습니다. 따라서 테이블의 layout 메서드를 덮어 쓰고

@Override 
public void layout() 
{ 
    super.layout(); 
    if (new_messages_added) 
    { 
     scrollPane.scrollToCenter(...) 
    } 
} 
다음에 scrollToCenter을 호출하십시오.