objectanimator和最终听众的问题

时间:2015-07-23 19:47:14

标签: android canvas

我正在编写代码以使圆圈掉落。问题是,当我调用一个随机放置一个方法的方法时,它表现很好,但是在第二个回合中它放置了两个部分而在第三个转弯中放置了3个部分。我知道当我将行game.juegaMaquina();放在结束侦听器中时会出现问题。

代码是:

 public boolean onTouchEvent(MotionEvent event) {
        final int fila;
        final int columna;

        pixel_x = (int) event.getX();
        pixel_y = (int) event.getY();
        fila = getFila(pixel_y);
        columna = getColumna(pixel_x);

        if (game.tableroLleno()) {
            Toast.makeText(getApplicationContext(), R.string.fin_del_juego,
                    Toast.LENGTH_LONG).show();
            return true;
        }

        if (game.sePuedeColocarFicha(fila, columna) != true) {
            Toast.makeText(getApplicationContext(), R.string.nosepuedecolocarficha,
                    Toast.LENGTH_SHORT).show();
            return true;
        }

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            if(pixel_y > 200) {
                animator.start();
                animator.setIntValues(200, getPixelFromFila(getFila(pixel_y)));
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        // TODO Auto-generated method stub
                        game.juegaMaquina();
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        // TODO Auto-generated method stub
                        game.ponerJugador(fila, columna);
                        if (game.comprobarCuatro(Game.JUGADOR)) {
                            Toast.makeText(getApplicationContext(), "Has ganado " + nombreFinal,
                                    Toast.LENGTH_LONG).show();
                            if (game.fin()) {
                                FragmentoDialogo dialogo = new FragmentoDialogo();
                                dialogo.show(getFragmentManager(), "Alert Dialog");
                            }
                        }
                        game.juegaMaquina();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        // TODO Auto-generated method stub
                    }
                });
            }
        }

        return true;
    }

1 个答案:

答案 0 :(得分:0)

解决方案是写入onAnimationEnd这一行:

animator.removeAllListeners