0
votes
public class GameView extends SurfaceView
{
    private GameThread mThread;
    SurfaceHolder holder;
    Bitmap fon = BitmapFactory.decodeResource(getResources(), R.drawable.fon);
    ArrayList<Integer> lasers = new ArrayList<Integer>();
    ArrayList<Integer> coordYlasers = new ArrayList<Integer>();
    ArrayList<Integer> coordXlasers = new ArrayList<Integer>();
    Bitmap laser = BitmapFactory.decodeResource(getResources(), R.drawable.laser);
    int touchX = 0;
    int touchY = 0;
    int coordX = 0;
    int coordY = 600;
    int _coordX = 0;
    private boolean running = false;
    ArrayList<Bitmap> blocks = new ArrayList<Bitmap>();
    public class GameThread extends Thread
    {
        private GameView view;

        public GameThread(GameView view)
        {
            this.view = view;
        }

        public void setRunning(boolean run)
        {
            running = run;
        }

        public void run()
        {
            int i = 0;
            while (running)
            {
                Canvas canvas = null;
                try
                {
                    canvas = view.getHolder().lockCanvas();
                    synchronized (view.getHolder())
                    {
                        onDraw(canvas);
                    }

                }
                catch (Exception e) { }
                finally
                {
                    if (canvas != null)
                    {
                        view.getHolder().unlockCanvasAndPost(canvas);
                    }
                }
            }
        }
    }
    public GameView(Context context)
    {
        super(context);

        mThread = new GameThread(this);
        getHolder().addCallback(new SurfaceHolder.Callback() {
            public void surfaceDestroyed(SurfaceHolder holder) {
                boolean retry = true;
                mThread.setRunning(false);
                while (retry) {
                    try {
                        mThread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                    }
                }
            }

            public void surfaceCreated(SurfaceHolder holder) {
                mThread.setRunning(true);
                mThread.start();

            }

            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
        });
    }
    public boolean onTouchEvent(MotionEvent event)
    {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            _coordX = coordX;
            X = (int) event.getX();
            Y = (int) event.getY();

        }
        if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            touchX = ((int) event.getX());
            touchY = (int) event.getY();
            //if (Math.abs(touchX - X) > 10){
            coordX = _coordX + (touchX - X);
            invalidate();

            //}
        }
        if(event.getAction() == MotionEvent.ACTION_UP)
        {
            _coordX = coordX;
            Y = touchY;
        }
        return true;
    }
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(20);

        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(fon, 0, 0, null);
        canvas.drawBitmap(player, coordX, coordY, null);
        canvas.drawBitmap(laser, coordLaserX, coordLaserY, null);
        for (int i = 1; i<coordYlasers.size(); ++i){
            canvas.drawBitmap(laser, coordXlasers.get(i), coordYlasers.get(i), null);
        }
        canvas.drawText("" + V, 10, 25, paint);
    }

}

Application is worked excellent, but.... If I hide app, game crashes. Help, please.

Logcat error.

5553-5553/com.example.thetronuo.pregame E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalThreadStateException: Thread already started. at java.lang.Thread.start(Thread.java:1045) at com.example.thetronuo.pregame.GameView$1.surfaceCreated(GameView.java:161) at android.view.SurfaceView.updateWindow(SurfaceView.java:569) at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:231) at android.view.View.dispatchWindowVisibilityChanged(View.java:7618) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1211) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4356) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:532) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5099) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570) at dalvik.system.NativeStart.main(Native Method) It's activity.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(new GameView(this));
}

"wait()" - unhandled exception.
Where to write "resumeThread()"?

public class GameThread extends Thread
        {
            private GameView view;
            private boolean pause = false;

            private synchronized void check(){
                while(pause){
                    wait();
                }
            }

            public void pauseThread(){
                pause = true;
            }

            public void resumeThread(){
                pause = false;
                notify();
            }
            public GameThread(GameView view)
            {
                this.view = view;
            }
1

1 Answers

0
votes

I think I had the same problem. But i fixed it. Put a this in your Thread class:

private boolean pause = false;

private synchronized void check(){
while(pause){
wait();
}
}

public void pauseThread(){
pause = true;
}

public void resumeThread(){
pause = false;
notify();
}

And when onSurfaceDestroyed is called you need to pause the thread with the method youve created: pauseThread();

also call the method check(); in the while-loop, so:

while(true){
 check();
... other code ...
}

and create a onResume() method in the activity class, so whenever the user turns back to the app, it will call this method automatically, and put this code into the method:

resumeThread();

I hope this would help you, im not sure if it is all the code you need as im not at home at the moment, if it doesnt work ill have a look and edit when im at home.