Programming with SurfaceView and thread strategy for game development

后端 未结 3 841
别那么骄傲
别那么骄傲 2020-12-24 14:55

I am using a SurfaceView and a rendering thread to develop a game based on structure like LunarLander.

However, I faced many problems, and here I want to point them

相关标签:
3条回答
  • 2020-12-24 15:38

    I hope it can help

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    
       //if it is the first time the thread starts
       if(thread.getState() == Thread.State.NEW){
        thread.setRunning(true);//riga originale
        thread.start();//riga originale
       }
    
       //after a pause it starts the thread again
       else
       if (thread.getState() == Thread.State.TERMINATED){
           thread = new MainThread(getHolder(), this);
           thread.setRunning(true);
           thread.start();  // Start a new thread
           }
       }
    

    and this

        @Override
        protected void onPause() {
        Log.d(TAG, "Pausing...");
        MainThread.running=false;//this is the value for stop the loop in the run()
        super.onPause();
        }
    
    0 讨论(0)
  • 2020-12-24 15:49

    I've been dealing with the same issue. I'm learning about game development on Android and also based my first project on the Lunar Lander example. I found that the SpriteMethodTest project created by Chris Pruett implements a much better approach in terms of thread management. It's all about using notify and wait methods, though. I couldn't tell whether it's better than your solution or not.

    0 讨论(0)
  • 2020-12-24 15:53

    the solution is here

    public void surfaceCreated(SurfaceHolder holder){
    
                if (plot_thread.getState() == Thread.State.TERMINATED) {
                    plot_thread = new WaveformPlotThread(getHolder(), this);
                    plot_thread.setRunning(true);
                    plot_thread.start();
                }
                else {
                    plot_thread.setRunning(true);
                    plot_thread.start();
                }
            }
    
    0 讨论(0)
提交回复
热议问题