I am creating a game in Android studio using the SurfaceView class that is instantiated in MainActivity and a thread that is instantiated in SurfaceView, the problem is when I turn the screen on and it goes back to the initial state, ie it does not save the positions / state, in real, wanted to know how to implement a pause menu ...
IMAGES
Before pressing the off screen button (I tapped the screen to jump the [circle / bird])
afterpressingtheonscreenbutton(the[circle/bird]returnstothefallposition)
Source code below
MainActivity
public class MainActivity extends Activity {
private Game game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout container = findViewById(R.id.container);
game = new Game(this);
container.addView(game);
}
@Override
protected void onPause() {
super.onPause();
game.cancela();
}
@Override
protected void onResume() {
super.onResume();
game.inicia();
new Thread(game).start();
}
}
Game Class
public class Game extends SurfaceView implements Runnable,View.OnTouchListener{
private final SurfaceHolder holder = getHolder();
private boolean isRunning = true;
private Player passaro;
private Bitmap background;
private Tela tela;
public Game(Context context) {
super(context);
setOnTouchListener(this);
inicializaElementos();
}
private void inicializaElementos() {
tela = new Tela(getContext());
this.passaro = new Player();
Bitmap back = BitmapFactory.decodeResource(getResources(),
R.drawable.background);
this.background = Bitmap.createScaledBitmap(back,
tela.getLargura(), tela.getAltura(), false);
}
public void update() {
passaro.update();
}
@Override
public void run() {
while (isRunning) {
if (!holder.getSurface().isValid()) continue;
//Neste loop vamos gerenciar os elementos do Jumper.
//
Canvas canvas = holder.lockCanvas();
update();
//
//Aqui vamos desenhar os elementos do jogo!
canvas.drawBitmap(background, 0, 0, null);
passaro.desenhaNo(canvas);
//
holder.unlockCanvasAndPost(canvas);
}
}
public void cancela() {
this.isRunning = false;
}
public void inicia() {
this.isRunning = true;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
passaro.pula();
return false;
}
}
Player class (bird)
public class Player {
private static final Paint vermelho = Cores.getCorDoPassaro();
private int x;
private static final int RAIO = 50;
private int y, velY = 0;
private int gravity = 1;
//private float fric = 0.3f;
//private boolean jump = false;
public Player() {
this.y = 0;
this.x = 100;
}
public void pula() {
this.velY = -30;
cai();
}
public void update() {
if (chao()) {
this.y = 1080 - RAIO;
this.velY = 0;//(int) (-this.velY*this.fric);
} else {
cai();
}
}
private boolean chao() {
if (this.y >= 1080 - RAIO) {
return true;
}
return false;
}
private void cai() {
this.y += this.velY;
this.velY += this.gravity;
}
public void desenhaNo(Canvas canvas) {
canvas.drawCircle(x, y, RAIO, vermelho);
}
}
Screen Class
public class Tela {
private DisplayMetrics metrics;
public Tela(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
metrics = new DisplayMetrics();
display.getMetrics(metrics);
}
public int getAltura() {
return metrics.heightPixels;
}
public int getLargura() {
return metrics.widthPixels;
}
}
Colors Class
public class Cores {
public static Paint getCorDoPassaro() {
Paint vermelho = new Paint();
vermelho.setColor(0xFFFF0000);
return vermelho;
}
}