0

最近我一直在尝试完成一个西蒙说的安卓游戏,并且比预期的要困难。

现在游戏差不多完成了,引擎工作正常,游戏逻辑运行良好等等......问题来了,当序列必须自己显示在显示器中以便玩家可以复制时。

声音工作正常,但是当必须完成序列时,在方法完成之前什么都不会绘制。

重要的是说必须在每一步之间暂停,为此我尝试了很多东西,每一个都得到相同的结果:

  • 线程.sleep(...);
  • SystemClock.sleep(...);
  • AsyncTask 只是做了一些睡眠然后升旗......
  • View.invalidate();
  • View.postInvalidate();

等等...

最好的结果是使用 SystemClock.sleep(...) 屏幕不显示任何内容,但其他一切正常。

同样重要的是要解释我没有使用drawables或xml文件来绘制按钮,而不是我在Canvas上使用Path和Paint绘制形状,所有这些都在SurfaceView中。

我把我认为不能正常工作的方法放在这里,项目中有更多代码,但它似乎正在工作并且没有搞砸任何东西。

这是我们正在做的最重要的方法的表面视图类:

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

//Elementos del thread
public MySurfaceThread thread;
public boolean touched;
public boolean reproduciendo;
public boolean juega;
public boolean despierta;
public boolean llamaPausa;


//Colores
public int mediumOrchid = 0xBA55D3;
public int crimson = 0xDC143C;
public int gold = 0xFFD700;
public int cornFlowerBlue = 0x6495ED;
public int limeGreen = 0x32CD32;

public int darkOrchid = 0x9932CC;
public int fireBrick = 0xB22222;
public int goldenRod = 0xDAA520;
public int midNightBlue = 0x191970;
public int mediumSeaGreen = 0x3CB371;

public int[]colores = {mediumOrchid, crimson, gold, cornFlowerBlue, limeGreen};
public int[]tocados = {darkOrchid, fireBrick, goldenRod, midNightBlue, mediumSeaGreen};


//Coordenadas de pulsación
PointF click;

public int indice = 0;
public int repId;


private int correctas;
private Vector<Integer> secuencia = new Vector<Integer>();
private Random aleatorio;


//Sonido
SoundPool mp;
int idBell1;
int idBell2;
int idBell3;
int idBell4;


Activity padre;


public MySurfaceView(Context context, SoundPool mpObj, Random rnd) {
    super(context);
    getHolder().addCallback(this);
    aleatorio = rnd;

    //Soundloop
    mp = mpObj;
    idBell1 = mp.load(context, R.raw.sy01, 1);
    idBell2 = mp.load(context, R.raw.sy02, 1);
    idBell3 = mp.load(context, R.raw.sy04, 1);
    idBell4 = mp.load(context, R.raw.sy06, 1);


}



@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread = new MySurfaceThread(getHolder(), this);
    thread.setRun(true);
    thread.start();
    Log.e("surfaceCreated", "Creado");






}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    boolean retry = true;
    //Detenemos el hilo
    thread.setRun(false);
    while (retry){
        try{
            thread.join();
            retry = false;
        }
        catch (InterruptedException e){}
    }

}


 @Override
public  void onDraw(Canvas canvas){
    if(reproduciendo){Log.e("onDraw", "Entro a onDraw desde repsec");}
    //Fondo negro
    canvas.drawColor(Color.BLACK);

    //Creamos el pinceles
    Paint pincel = new Paint();
    pincel.setAntiAlias(true);
    pincel.setStyle(Paint.Style.FILL);

    if (touched){//Activates when user is touching the screen
        int id = quePieza(click, canvas);
        //Log.e("onDraw", touched + " " + id);
        if(id != -1) colores[id] = tocados[id];
    }

    if(reproduciendo){//Activates when sequence must be shown, just changes colours in the  color array conveniently
        Log.e("onDraw", "repId = " + repId);
        if(repId != -1){colores[repId] = tocados[repId];}
        repId = -1;
        correctas++;
        Log.e("onDraw", "Correctas = " + correctas);
    }
    //Pintamos las piezas
    for(int i = 0; i < colores.length; i++){
        pincel.setColor(Color.rgb(Color.red(colores[i]), Color.green(colores[i]), Color.blue(colores[i])));
        pintarPiezas(i, canvas, pincel);//Paint each button according to an int code (0 to 4)
    }

    //Pintamos el texto
    pincel.setColor(Color.WHITE);
    dibujarTexto(canvas, pincel);

    //Reestablecemos colores originales
    resetColor();
    //Log.e("onDraw", "He terminado de pintar");



}

这是负责在显示器中显示序列的方法:

public void reproducirSecuencia(final Canvas canvas){
    reproduciendo = true;
    //TODO: HACER QUE ESTO FUNCIONE!!
    Log.e("reproducirSecuencia", "Entro a reproducir");

    int i = 0;

    while(i < secuencia.size()){

        Object o = secuencia.elementAt(i);
        int num = 0;
        if (o instanceof Integer) {num = (Integer) o;}


        SystemClock.sleep(1000);
            i++;
            reproducirSonido(num);
            repId = num;
            onDraw(canvas);
            i++;



        //Log.e("reproducirSecuencia", "repId = " + repId);
        //Log.e("reproducirSecuencia", "Invoco a pintarPiezas");





        //SystemClock.sleep(1000);
        //try {Thread.sleep(1000);}
        //catch (InterruptedException e) {e.printStackTrace();}
    }

    reproduciendo = false;
}

最后这是游戏线程中的主要方法:

public void Play(Canvas canvas){


    /*if(juega){
        int piezaTocada = quePieza(click, canvas);
        reproducirSonido(piezaTocada);
        juega = false;
    }
    onDraw(canvas);*/

    onDraw(canvas);

    if (secuencia.isEmpty()){//Creamos el primer movimiento
        crearMovimiento();
        reproducirSecuencia(canvas);
    }
    else{//Sigue el juego
        if(juega){//Esperamos a que haya una jugada
            int piezaTocada = quePieza(click, canvas);//Method to find which button was pressed
            //¿Coincide con lo que buscamos?
            if(piezaTocada != 0){//Que no se cuente el botón central en el modo classic
                reproducirSonido(piezaTocada);
                if(esCorrecto(piezaTocada, secuencia.elementAt(indice), canvas)){//Check if user's move was correct or not
                    //Aumentamos el indice
                    indice++;
                    juega = false;//Acabamos con la jugada
                    if(indice > secuencia.size() - 1){//Hemos hecho toda la secuencia, ponemos un nuevo elemento y reiniciamos
                        //SystemClock.sleep(3000);

                        indice = 0;
                        crearMovimiento();

                        int buc = 0;
                        reproducirSecuencia(canvas);
                    }

                }
                else{//No es correcto...
                    gameOver(canvas);
                }
            }
        }
    }
}

我也加入了我的 Thread 类,因为它在播放序列时似乎被阻塞了:

public class MySurfaceThread extends Thread {

private SurfaceHolder surface;
private MySurfaceView view;
private boolean run;
public boolean pausa;


//Metodo constructor
public MySurfaceThread(SurfaceHolder sh, MySurfaceView v){
    this.surface = sh;
    this.view = v;
}



public synchronized void pausar() {
    pausa = true;
}

public synchronized void reanudar() {
    pausa = false;
    notify();
}

public synchronized void detener() {
    run = false;
    if (pausa) reanudar();
}


public void setRun(boolean r){
    this.run = r;
}



public  void run(){





    //...
    //Creamos un canvas
    Canvas canvas;
    //Mientras run sea true pintamos
    while (run){


        canvas = null;
        try {

            canvas = surface.lockCanvas(null);
            //Usamos syncro
            synchronized (surface){

                if(canvas != null){//Si el canvas existe pintamos
                    view.Play(canvas);

                }
                while (pausa) {
                    try {
                        wait();
                    }
                    catch (Exception e) {
                    }
                }
            }
        }
        finally {
            if (canvas != null){
                //Liberamos el canvas y el soundPool
                surface.unlockCanvasAndPost(canvas);
            }
        }


    }

}

}

就是这样,我会尽可能清楚地回答答案,因为这是我的第一个应用程序,我有一年的早期编码和几个月的 android 尝试。

非常感谢!

路易斯。

4

1 回答 1

1

不不不。不要睡在你的主线程中,它会阻塞你的用户界面——这正是你正在经历的。您应该播放一种声音,然后设置一个计时器。一旦计时器到期,播放下一个声音,设置另一个计时器等等。

设置计时器的一种简单方法是使用Handler.sendMessageAtTime

于 2013-09-01T17:21:22.680 回答