【问题标题】:Libgdx scrollpane issue for a chatwindow class聊天窗口类的 Libgdx 滚动窗格问题
【发布时间】:2014-02-20 13:14:51
【问题描述】:

我在 libgdx 中使用滚动窗格时遇到问题。它将用于聊天窗口类。当您按 Enter 时,消息将被添加到窗口中,您将滚动到最新发布的消息。但是它没有。它错过了一条消息并滚动到最新消息之前的一条。下面我发布了聊天窗口类和向它添加输入的方法。 textAreaholder 是一个包含所有内容的表格。 chatField 是您输入要发布到聊天的内容的地方。聊天区域是随后添加到表中的文本字段。但如前所述..它不能正确滚动,错误正确地存在于 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
                }
            }
        }
    });

【问题讨论】:

    标签: java libgdx scrollpane


    【解决方案1】:

    可能会出现问题,因为您使用的是带有零参数的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(...)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-18
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多