移动播放器时出现峰值滞后 [英] Lag spike when moving player

查看:127
本文介绍了移动播放器时出现峰值滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

播放器是一个面板,将其删除,将其位置更改,然后重新添加到绘制到主框架的另一个面板(包含此方法的面板)中.还有许多其他小的面板,其中包含草精灵作为地形图块绘制到主面板上.我认为问题在于,当我调用revalidate()时,它也会重新验证所有这些小面板.我该如何解决?

The player is a panel, and it is getting removed, its position changed, and then re-added to another panel (which is what contains this method) which is drawn to the main frame. There are also a lot of other small panels containing a grass sprite being drawn to the primary panel as terrain tiles. I think the problem is that when I call revalidate(), it revalidates all those little panels as well. How can I solve this?

我应该提到我正在使用RelativeLayout将播放器放置在主面板上.

I should mention that I am using RelativeLayout to position the players on the primary panel.

private class MainKeyAdapter extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent evt) {
        // TODO add your handling code here:
        if(AttentionedPlayer != null)
        {
            if (evt.getKeyCode() == KeyEvent.VK_UP) {
                AttentionedPlayer.Ypos -= 16;
            }
            if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                AttentionedPlayer.Ypos += 16;
            }
            if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
                AttentionedPlayer.Xpos -= 16;
            }
            if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                AttentionedPlayer.Xpos += 16;
            }
            remove(AttentionedPlayer);
            AttentionedPlayer.movePlayer();
            System.out.println("!!!!"+AttentionedPlayer.constraints.toString());
            add(AttentionedPlayer, AttentionedPlayer.constraints, AttentionedPlayer.Zpos);
            AttentionedPlayer.revalidate();
        }
    }

}

推荐答案

AttentionedPlayer.movePlayer();似乎是一项繁琐的操作,您可以在 SwingWorker执行.

AttentionedPlayer.movePlayer(); seems to be an intensive operation, and you execute it from within EDT (the GUI thread). Instead, execute it from within a new thread or a SwingWorker.

阅读此答案以了解有关 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆