- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为 Java 中的 2d 平台游戏制定基本轮廓...我遇到了一个最近才开始出现的问题!有时,当我运行我的程序时,我的框架出现,一切都设置好了,你可以看到那个家伙,但由于某种原因,地面没有被“添加”(我有一个 addGround() 方法,我在运行的第一个段中调用它)。随着我对游戏的深入,这种情况开始越来越多地发生,直到现在,大多数时候它都会失败并且不会显示/添加地面!但有时,它可以完美地完成任务。
这是它工作时的样子:
大多数时候,当它不起作用时,它看起来就是这样:
所以,这是我的主类代码:(我没有包括所有的进口和东西,只是核心)
public BusiWorld()
{
this.setFocusable(true);
if(init)
{
ground.addGround(x-300, y+100, 35, 1);
ground.addGround(x-100, y-360, 1, 46);
ground.addGround(x+100, y+90, 1, 1);
ground.addGround(x+400, y+20, 2, 1);
ground.addGround(x+460, y-50, 2, 1);
ground.addGround(x+520, y-120, 2, 1);
ground.addGround(x+460, y-190, 2, 1);
ground.addGround(x+140, y-260, 15, 1);
ground.addGround(x, y-280, 1, 1);
init = false;
}
t = new Timer(16, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
});
t.start();
}
public static void main(String[] args)
{
mainFrame.setTitle("Busiworld");
mainFrame.setSize(600,500);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBackground(Color.WHITE);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
}
这是地面类代码:
public class Ground extends JFrame
{
private Image groundImg = Toolkit.getDefaultToolkit().getImage("ground.png");
private ArrayList<Point> groundLocs = new ArrayList<Point>();
public Ground()
{
}
public void addGround(int xCoord, int yCoord, int howManyWide, int howManyTall)
{
for(int i = 0; i < howManyWide; i++)
{
for(int j = 0; j < howManyTall; j++)
{
groundLocs.add(new Point(xCoord+i*groundImg.getWidth(null), yCoord+j*groundImg.getHeight(null)));
}
}
}
public void drawGround(Graphics g)
{
for(Point p: groundLocs)
{
g.drawImage(groundImg, p.x, p.y, this);
}
}
public int groundArraySize()
{
return groundLocs.size();
}
public Point getGroundArray(int index)
{
return groundLocs.get(index);
}
}
最佳答案
这是 Swing 中的 JFrame
?例如,当您调用 repaint()
时,我没有看到 invokeLater()
。在 Swing 应用程序中不正确管理线程可能会导致各种各样的问题。鉴于您发布的代码量,我建议您首先处理线程 - 然后如果问题仍然存在,您可以缩小问题范围。
这是一个tutorial on how to manage threads in Swing .
基本上,任何修改或导致任何绘制发生的事情都必须在 Swing 组件初始化之前或在事件分派(dispatch)线程上发生。事件监听器中的代码或通过 invokeLater()
或 invokeAndWait()
调用的代码在事件分派(dispatch)线程上运行。 Timer
中的代码不会在事件分派(dispatch)线程上调用,但会与 Swing 组件交互,因此可能会导致各种错误(例如删除一半的绘图)。
关于Java 平台游戏不运行最初的几行代码...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11376807/
我有一个使用 Sinch SDK 和 CallKit 实现的 VOIP 应用程序。一切正常,除了设备插入耳机时。在后一种情况下,当通话开始时,音频仍通过设备的主扬声器路由。如果我在通话期间拔下并重
我需要将大量复杂(20 多个属性)对象过滤成多个子列表。要创建子列表,我有一个过滤器规范列表。要求是:a) 不允许一个项目成为两个子列表的一部分,并且 b) 必须能够在处理完成后获取所有未分割的项目。
我有一个简单的 .share-toggle 切换链接。当您点击它时,Facebook 评论插件应该会显示。它最初应该被 Conceal 问题是,如果我使用 css Conceal 它,然后单击切换按钮
我正在尝试制作一个功能类似于 android 市场的图库,您可以在其中滚动(左/右)以查看免费或付费应用程序等...还可以在布局中上下滚动。 到目前为止,我只是让它加载了两个布局,它们都有一个简单的“
大家好,我是网络开发的新手,最近刚刚完成我对网络设计的第一次尝试。我偶然发现了一个我找不到解决方案的问题。在 webkit-browsers 中,我的一个 ul-lists(导航)最初呈现在错误的位置
我遇到了这个问题。我在 ISP 上使用 Umbraco CMS 启动了一个 ASP.NET 网站。(这只是一个非常基本的信息网站。没什么特别的。) 但是,当我想要访问该网站时,第一个页面加载需要很长时
在我的 iPhone 应用程序中,我使用 UITabBarController 布置了三个选项卡。第一个选项卡(在应用启动时加载)使用本地数据加载,速度非常快。 虽然第二个选项卡从网络下载 XML 文
我有这样的风格: #cytoscape-container { width: 100%; height: 100%; margin: 0 aut
我在这里看到了几篇关于 SO 的帖子,但它们在功能和结构上过于具体,而我正在寻找的是我或任何人都可以在任何地方使用的更通用的东西。 我只需要有一个按钮,单击该按钮可以在 3 类之间循环。但如果出现必须
我在 http://www.raven.dima.neoturbine.net/ 有一个网页我正在努力。顶部导航在 IE 8、Firefox 3.6 和适用于 Android 的 Dolphin 浏览
我想将主机的用户/组与 docker 机器同步,以使(开发人员)能够编辑容器内部或外部的文件。有一些这样的想法:Handling Permissions with Docker Volumes这会创建
向通知列表/栏发布推送通知时,.contentText 和 .number 最初不显示(.ticker、.icon 和 .contentTitle 显示正常)。但是,在发布另一个通知(具有不同的 ID
我实现了以下 MKMapView 方法,该方法在添加注释后运行。我在 Interface Builder 中将我的 MKMapView map (parishMap) 设置为“显示用户位置”,并且在加
我是一名优秀的程序员,十分优秀!