繁体   English   中英

聊天窗口类的Libgdx滚动窗格问题

[英]Libgdx scrollpane issue for a chatwindow class

我在libgdx中使用滚动窗格时遇到问题。 它将用于聊天窗口类。 当您按Enter键时,该消息将被添加到窗口中,并且您将滚动至最新发布的消息。 它会漏掉一条消息,并滚动到最新消息之前的一条消息。 在下面,我发布了chatwindow类和向其添加输入的方法。 textAreaholder是一个包含所有内容的表。 在chatField中,您可以输入要发布到聊天室中的内容。 chatarea是文本字段,然后添加到表中。 但是,如前所述,它无法正确滚动,该错误正确地位于keyTyped方法中。

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
                }
            }
        }
    });

可能会出现问题,因为您使用的是带有零参数的scrollPane.scrollToCenter(float x, float y, float width, float height)

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

scrollToCenter方法要求正确提供参数。 因此,尝试提供消息范围。

第二个原因可能是因为您在表格进行布局之前调用了scrollToCenter 因此,尝试覆盖表的layout方法,并在执行以下操作后调用scrollToCenter

@Override
public void layout()
{
    super.layout();
    if (new_messages_added)
    {
       scrollPane.scrollToCenter(...)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM