Lag spike when moving player(移动玩家时的延迟峰值)
问题描述
播放器是一个面板,它被移除,位置改变,然后重新添加到另一个面板(包含此方法),该面板被绘制到主框架.还有许多其他包含草精灵的小面板被绘制到主面板作为地形图块.我认为问题在于,当我调用 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();
似乎是一个密集的操作,你从 EDT(GUI 线程).相反,从一个新线程或 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.
阅读此答案以了解更多关于SwingWorker.
这篇关于移动玩家时的延迟峰值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:移动玩家时的延迟峰值
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01