0
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 on and off the screen it goes back to the initial state, ie it does not save the positions/state, actually, I was wondering how to implement a break menu ...
IMAGERY
before pressing the Off Screen button (I tapped the screen to [circle/bird] jump)
after pressing the button on screen (the [circle/bird] back to fall position)
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 (Gameview)
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;
}
}
Class Colors
public class Cores {
public static Paint getCorDoPassaro() {
Paint vermelho = new Paint();
vermelho.setColor(0xFFFF0000);
return vermelho;
}
}
Be more specific in your question, put what you already have of code. Screenshots of the problem on the screen, etc.
– hugocsl
@hugocsl I just put
– MickLoko