I am creating a Memo Game for a school project and I have a problem with my Thread.sleep.
When the player returns two cards, the program checks if they are identical. If they are not, then they turn face down. Since the program does it almost instantly, I'd like to pause so that the player has time to see the second card. The problem is that the break is before the card turns
if (deck1.contains(carte)) {
if (carte.getEtat() == 0) {
carte.setEtat(1);
cartesRetournees1.add(carte);
majAffichage(carte);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
if (cartesRetournees1.size() == 2) {
int nouveauEtat = 0;
if (cartesRetournees1.get(0).getMotif() == cartesRetournees1.get(1).getMotif()) {
nouveauEtat = -1;
this.nbPairsTrouvees++;
}
cartesRetournees1.get(0).setEtat(nouveauEtat);
cartesRetournees1.get(1).setEtat(nouveauEtat);
majAffichage(cartesRetournees1.get(0));
majAffichage(cartesRetournees1.get(1));
cartesRetournees1.remove(1);
cartesRetournees1.remove(0);
if (nbPairsTrouvees == this.plateau.size()) System.out.println("GAGNE !");
}
}
On my game, Thread.sleep works before majAffichage(carte) and I don't understand why. I dont know how to resolve my problem and if you guys can help me I would be very grateful :)
Thank you for your answer ! I have created a second Thread.
package com.example.jeudumemo;
import android.graphics.Canvas;
public class GameLoopThread extends Thread
{
private final static int FRAMES_PER_SECOND = 30;
private final static int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
private final Game view;
private boolean running = false;
public GameLoopThread(Game view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@Override
public void run()
{
long startTime;
long sleepTime;
while (running)
{
startTime = System.currentTimeMillis();
synchronized (view.getHolder()) {view.update();}
Canvas c = null;
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {view.draw(c);}
}
finally
{
try {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
} catch(IllegalArgumentException iae) { iae.printStackTrace(); }
}
sleepTime = SKIP_TICKS-(System.currentTimeMillis() - startTime);
try {
if (sleepTime >= 0) {sleep(sleepTime);}
}
catch (Exception e) {}
}
}
}
and I call this in my main class :
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(gameLoopThread.getState()==Thread.State.TERMINATED) {
gameLoopThread=new GameLoopThread(this);
}
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
}
catch (InterruptedException e) {}
}
}
public void controleJeu(Carte carte) {
if(deck1.contains(carte)) {
if (carte.getEtat() == 0) {
carte.setEtat(1);
cartesRetournees1.add(carte);
majAffichage(carte);
}
if (cartesRetournees1.size() == 2) {
try {
gameLoopThread.sleep(500);
}catch(InterruptedException ie) { ie.printStackTrace(); }
int nouveauEtat = 0;
if (cartesRetournees1.get(0).getMotif() == cartesRetournees1.get(1).getMotif()) {
nouveauEtat = -1;
this.nbPairsTrouvees++;
}
cartesRetournees1.get(0).setEtat(nouveauEtat);
cartesRetournees1.get(1).setEtat(nouveauEtat);
majAffichage(cartesRetournees1.get(0));
majAffichage(cartesRetournees1.get(1));
cartesRetournees1.remove(1);
cartesRetournees1.remove(0);
}
}
else if(deck2.contains(carte)) {
if (carte.getEtat() == 0) {
carte.setEtat(1);
cartesRetournees2.add(carte);
majAffichage(carte);
}
if (cartesRetournees2.size() == 2) {
try {
gameLoopThread.sleep(500);
}catch(InterruptedException ie) { ie.printStackTrace(); }
int nouveauEtat = 0;
if (cartesRetournees2.get(0).getMotif() == cartesRetournees2.get(1).getMotif()) {
nouveauEtat = -1;
this.nbPairsTrouvees++;
}
cartesRetournees2.get(0).setEtat(nouveauEtat);
cartesRetournees2.get(1).setEtat(nouveauEtat);
majAffichage(cartesRetournees2.get(0));
majAffichage(cartesRetournees2.get(1));
cartesRetournees2.remove(1);
cartesRetournees2.remove(0);
}
}
if (nbPairsTrouvees == nbDeck*6) { showPopupVictoire(this); this.victory = true; }
System.out.println(nbDeck);
}
My problem is still perduring. I think I didn't use my GameLoopThread as it must be used. Any remarks ?