0

When I restart my App, I mean when I press the Homebutton and start it from the Task window, or when I debug it while it is still runnning the whole screen went black!

This is my MainView:

package net.kex.toll;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


public class MainView extends SurfaceView implements SurfaceHolder.Callback{

public GameLoop gameLoop;
public Player player;
private int playerX = 50;
private int playerY = 50;

public MainView(final Context context) {
    super(context);
    getHolder().addCallback(this);
    player = new Player();
    this.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            playerX = playerX + 20;
            playerY = playerY + 20;
            player.play();
            return false;
        }
    }
    );
    setFocusable(true);

}


public void surfaceChanged(SurfaceHolder surfaceHolder, int foramt, int width, int height){

}
public void surfaceDestroyed(SurfaceHolder surfaceHolder){
    boolean retry = true;
    while (true) {
        try {
            gameLoop.setRunning(false);
            gameLoop.join();
        } catch (Exception e) { e.printStackTrace();}
        retry = false;
    }
}
public void surfaceCreated(SurfaceHolder surfaceHolder){
    gameLoop = new GameLoop(getHolder(), this);

    gameLoop.setRunning(true);
    gameLoop.start();
}
public void update(){
    player.update();
}
public void draw(Canvas canvas){
    if(canvas == null) {
        return;
    }
    canvas.drawColor(Color.WHITE);
    player.draw(canvas);
}
}

This is my GameLoop:

package net.kex.toll;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;


public class GameLoop extends Thread {
public static final int MAX_FPS = 30;
private double averageFPS;
private MainView mainView;
private SurfaceHolder surfaceHolder;
private boolean running;
public static Canvas canvas;

public void setRunning(boolean running) {
    this.running = running;
}

public GameLoop(SurfaceHolder surfaceHolder, MainView mainView) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.mainView = mainView;
}

@Override
public void run(){
    long startTime;
    long timeMillis = 1000/MAX_FPS;
    long waitTime;
    long frameCount = 0;
    long totalTime = 0;
    long targetTime = 1000/MAX_FPS;

    while (running) {
        startTime = System.nanoTime();
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.mainView.update();
                this.mainView.draw(canvas);
            }
        }catch (Exception e){ e.printStackTrace();}
        finally {
            if (canvas != null) {
                try {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }catch (Exception e){ e.printStackTrace();}

            }
        }
        timeMillis = (System.nanoTime() - startTime)/1000000;
        waitTime = targetTime - timeMillis;
        try {
            if (waitTime >= 0){
                this.sleep(waitTime);
            }
        }catch (Exception e){ e.printStackTrace();}

        totalTime += System.nanoTime() - startTime;
        frameCount++;

        if (frameCount == MAX_FPS) {
            averageFPS = 1000/((totalTime/frameCount)/1000000);
            frameCount = 0;
            totalTime = 0;
            Log.i("GameLoop", "averageFPS: " + Double.toString(averageFPS));
        }
    }
}

}

I don't know what to do! If you want some other class please tell me and I will send it to you!

Please help I´m sitting here and trying to solve this problem for hours!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ukex
  • 3
  • 3

1 Answers1

0

Repaint the object, it will render it again. For example in onResume add Repaint()

Hamza rasaee
  • 362
  • 4
  • 12
  • What do you mean with Repaint()? Can you show me a example? – Ukex Dec 09 '17 at 16:01
  • To repaint a object needs to use "Object.invalidate();". but it would be better to use "onDraw() ": 'protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.draw....; }' – Hamza rasaee Dec 09 '17 at 17:00
  • I'm sorry, but I dont understand what you mean! Can you please write me the code I have to instert? – Ukex Dec 11 '17 at 14:12
  • Where do you call the MainView? In which activities,...? – Hamza rasaee Dec 11 '17 at 15:21
  • '@Override protected void onResume() { super.onResume(); if (mainView != null) { mainView.invalidate(); } }' – Hamza rasaee Dec 11 '17 at 15:28
  • I use it in public static MainView mainView; @Override protected void onCreate(Bundle savedInstanceState) { mainView = new MainView(this); super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(mainView); Constants.CURRENT_CONTEXT = getApplicationContext(); } – Ukex Dec 11 '17 at 15:42
  • the program will stay on surfaceDestroyed: while (true) { try { gameLoop.setRunning(false); gameLoop.join(); System.out.println("Syay Here"); } catch (Exception e) { e.printStackTrace();} retry = false; } – Hamza rasaee Dec 11 '17 at 16:08
  • Isn't this what I posted before? – Ukex Dec 11 '17 at 16:32
  • Yes, I picked it up from your code. Your program get stuck in this function, so it will not get out of the "while()". It means that you will not refresh anything. I don't know what is your reason for this, but you need to break this "while()" – Hamza rasaee Dec 11 '17 at 16:47