【问题标题】:Eclipse program runs faster than after it is exportedEclipse程序运行速度比导出后快
【发布时间】:2015-06-21 18:20:50
【问题描述】:

问题:我有一个程序涉及从一侧移动到另一侧(例如在砖块中)。问题是它在 Eclipse 中运行时的移动速度与导出到桌面时不同。

研究:

阅读本文时,我觉得我可能没有正确创建我的 delta 值,这可以解释为什么它以不同的速度运行。 my computer is carrying out java programs which i have created to fast [closed]

阅读这篇文章后,我发现Eclipse 编译器可能只是更快,这可能会导致速度差异。 Application runs faster in eclipse

    // run() is being ran with a while(true) game loop
    public void run(Input input)
    {
         init();
         // Ran at 60 FPS
         if(fps.tick())
              render();
         // Ran as fast as possible
         update(input);
    }

    private void render()
    {
         start = System.nanoTime();     

         if(render.getGraphics() != null)
         {
             Graphics g = render.getBS().getDrawGraphics();
             g.drawImage(new BufferedImage(Reference.WIDTH*Reference.SCALE, Reference.HEIGHT*Reference.SCALE, 
                BufferedImage.TYPE_INT_RGB), 0,0, AppGameContainer.canvas);
             activeState.render(render.getGraphics(), AppGameContainer.canvas);
             render.getGraphics().dispose();
             render.getBS().show();
         }
         end = System.nanoTime();

         // Time in ms
         delta =  (end-start)/1000000.0;
    }

我有一个视频如果有兴趣可以直观地展示问题:Video of issue

【问题讨论】:

  • 禁用任何限制 (fps.tick()) 并查看它是否仍然较慢。如果不是,那么您的代码就是问题所在。
  • 另外,你真的需要每次都 init() 吗?
  • init 方法如果已经完成则返回,并且不会初始化每次迭代。它是这样设置的,因此如果它改变游戏状态,它将初始化新的游戏状态。
  • 非常感谢 tilpner,我发现我应该在 run 方法中计算它,而不是在 render 方法中计算 delta,同时包含 fps.tick() 方法。感谢您帮助我解决我的问题。

标签: java eclipse game-engine delta


【解决方案1】:
    // run() is being ran with a while(true) game loop
    public void run(Input input)
    {
         start = System.nanoTime();
         init();
         // Ran at 60 FPS
         if(fps.tick())
              render();
         // Ran as fast as possible
         update(input);
         end = System.nanoTime();

         // Time in ms
         delta =  (end-start)/1000000.0;
    }

    private void render()
    {       
         if(render.getGraphics() != null)
         {
             Graphics g = render.getBS().getDrawGraphics();
             g.drawImage(new BufferedImage(Reference.WIDTH*Reference.SCALE, Reference.HEIGHT*Reference.SCALE, 
                BufferedImage.TYPE_INT_RGB), 0,0, AppGameContainer.canvas);
             activeState.render(render.getGraphics(), AppGameContainer.canvas);
             render.getGraphics().dispose();
             render.getBS().show();
         }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2017-11-20
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多