点击两次按钮它将在Android游戏中消失

时间:2015-04-27 11:02:39

标签: android android-studio

我正在开发一对卡片记忆匹配游戏 它包含一个4x4矩阵。

当我在任何按钮上单击两次时,它会消失 点击一些按钮后,不幸的是游戏停止并返回菜单屏幕。

相反,当卡片匹配并播放一些声音时,该过程应该使卡片对不可见。

public class Birds
        extends Activity {
    private int[] id_bird = new int[16];
    private Integer[][] img_bird = new Integer[16][2];
    private Button[] myBird = new Button[16];
    private int mc_counter = 0;
    private int firstid = 0;
    private int secondid = 0;
    private Boolean mc_isfirst = false;
    private int correctcounter = 0;
    //private TextView tFeedback;
    private MediaPlayer mp;
    private Boolean b_snd_inc, b_snd_cor, b_new_game;
    public static boolean DEBUG = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
        initGame();
    }

    private void initGame() {
        SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
        b_snd_cor = settings.getBoolean("play_sound_when_correct", true);
        b_snd_inc = settings.getBoolean("play_sound_when_incorrect", true);
        b_new_game = settings.getBoolean("new_game", true);
        if(b_new_game) {
            setContentView(R.layout.birds);
            mc_counter = 0;
            firstid = 0;
            secondid = 0;
            mc_isfirst = false;
            correctcounter = 0;
            //tFeedback = (TextView) findViewById(R.id.mc_feedback);
            // fill arrays with resources
            id_bird[0] = R.id.mc0;
            id_bird[1] = R.id.mc1;
            id_bird[2] = R.id.mc2;
            id_bird[3] = R.id.mc3;
            id_bird[4] = R.id.mc4;
            id_bird[5] = R.id.mc5;
            id_bird[6] = R.id.mc6;
            id_bird[7] = R.id.mc7;
            id_bird[8] = R.id.mc8;
            id_bird[9] = R.id.mc9;
            id_bird[10] = R.id.mc10;
            id_bird[11] = R.id.mc11;
            id_bird[12] = R.id.mc12;
            id_bird[13] = R.id.mc13;
            id_bird[14] = R.id.mc14;
            id_bird[15] = R.id.mc15;
            img_bird[0][0] = R.drawable.bird1;
            img_bird[0][1] = R.mipmap.bird1;
            img_bird[1][0] = R.drawable.bird2;
            img_bird[1][1] = R.mipmap.bird2;
            img_bird[2][0] = R.drawable.bird3;
            img_bird[2][1] = R.mipmap.bird3;
            img_bird[3][0] = R.drawable.bird4;
            img_bird[3][1] = R.mipmap.bird4;
            img_bird[4][0] = R.drawable.bird5;
            img_bird[4][1] = R.mipmap.bird5;
            img_bird[5][0] = R.drawable.bird6;
            img_bird[5][1] = R.mipmap.bird6;
            img_bird[6][0] = R.drawable.bird7;
            img_bird[6][1] = R.mipmap.bird7;
            img_bird[7][0] = R.drawable.bird8;
            img_bird[7][1] = R.mipmap.bird8;
            img_bird[8][0] = R.drawable.bird1;
            img_bird[8][1] = R.mipmap.bird1;
            img_bird[9][0] = R.drawable.bird2;
            img_bird[9][1] = R.mipmap.bird2;
            img_bird[10][0] = R.drawable.bird3;
            img_bird[10][1] = R.mipmap.bird3;
            img_bird[11][0] = R.drawable.bird4;
            img_bird[11][1] = R.mipmap.bird4;
            img_bird[12][0] = R.drawable.bird5;
            img_bird[12][1] = R.mipmap.bird5;
            img_bird[13][0] = R.drawable.bird6;
            img_bird[13][1] = R.mipmap.bird6;
            img_bird[14][0] = R.drawable.bird7;
            img_bird[14][1] = R.mipmap.bird7;
            img_bird[15][0] = R.drawable.bird8;
            img_bird[15][1] = R.mipmap.bird8;
            if(DEBUG == false) {
                Collections.shuffle(Arrays.asList(img_bird));
            }
            for(int i = 0; i < 16; i++) {
                myBird[i] = (Button) findViewById(id_bird[i]);
                myBird[i].setBackgroundResource(img_bird[i][0]);
                myBird[i].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        int i = 0;
                        for(int n = 0; n < 16; n++) {
                            if(id_bird[n] == view.getId()) {
                                i = n;
                            }
                        }
                        doClickAction(view, i);
                    }
                });
            }
        }
    }

    private void doClickAction(View v, int i) {
        v.setBackgroundResource(img_bird[i][1]);
        mc_isfirst = !mc_isfirst;
        // disable all buttons
        for(Button b : myBird) {
            b.setEnabled(false);
        }
        if(mc_isfirst) {
            // turning the first card
            firstid = i;
            // re enable all except this one
            for(Button b : myBird) {
                if(b.getId() != firstid) {
                    b.setEnabled(true);
                }
            }
        } else {
            // turning the second card
            secondid = i;
            doPlayMove();
        }
    }

    private void doPlayMove() {
        mc_counter++;
        if(img_bird[firstid][1] - img_bird[secondid][1] == 0) {
            // correct
            if(b_snd_cor) {
                playSound(R.raw.bird);
            }
            waiting(50);
            myBird[firstid].setVisibility(View.INVISIBLE);
            myBird[secondid].setVisibility(View.INVISIBLE);
            correctcounter++;
        } else {
            // incorrect
            if(b_snd_inc) {
                playSound(R.raw.incorrect);
            }
            waiting(50);
        }
        // re-enable and turn cards back
        for(Button b : myBird) {
            if(b.getVisibility() != View.INVISIBLE) {
                b.setEnabled(true);
                b.setBackgroundResource(R.drawable.questionbox1);
                for(int i = 0; i < 16; i++) {
                    myBird[i].setBackgroundResource(img_bird[i][0]);
                }
            }
        }
        //tFeedback.setText(String.format("%d/%d", correctcounter, mc_counter));
        if(correctcounter > 7) {
            Intent iSc = new Intent(getApplicationContext(), Congrats.class);
            startActivity(iSc);
        }
    }

    public void playSound(int sound) {
        mp = MediaPlayer.create(this, sound);
        mp.setVolume((float) .5, (float) .5);
        mp.start();
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }

    public static void waiting(int n) {
        long t0, t1;
        t0 = System.currentTimeMillis();
        do {
            t1 = System.currentTimeMillis();
        } while((t1 - t0) < (n));
    }
}

1 个答案:

答案 0 :(得分:0)

你真的必须在这里添加一个额外的条件:

 private void doPlayMove() {
    mc_counter++;
    if((secondId != firstId) && (img_bird[firstid][1] - img_bird[secondid][1] == 0)) {
        ...

虽然你做得更好,但只有当它尚未被选中时才能“选择”一个元素。

    if(mc_isfirst) {
        // turning the first card
        firstid = i;
        // re enable all except this one
        for(Button b : myBird) {
            if(b.getId() != firstid) {
                b.setEnabled(true);
            }
        }
    } else {
        // turning the second card
        if(i != firstid) {
            secondid = i;
            doPlayMove();
        }
    }

你应该采用最后一种方法。