当我重新启动应用程序时,我的意思是,当我按Homebutton
并从任务窗口启动它,或者在仍在运行整个屏幕时调试时,将其变为黑色!
这是我的 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);
}
}
这是我的 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));
}
}
}
}
我不知道该怎么办!如果您想要其他课,请告诉我,我会将其发送给您!
请帮助我坐在这里试图解决这个问题几个小时!
重新粉刷对象,它将再次渲染。例如,在onResume中添加repaver()