需要翻转游戏逻辑帮助

时间:2016-04-28 21:03:17

标签: java android

我正在开发一款小游戏,我已经编写了逻辑,游戏显示4行3列意味着共有12个单元格或按钮。

他们出现了2秒钟,我显示了所有图标,之后我消失了,然后用户必须匹配图标才能完成游戏。

我正在使用gridview用于此目的,代码在下面给出了我的整个游戏逻辑,但点击按钮非常慢任何人都指导我为什么我的游戏在点击单元格的速度很慢,我怎样才能改进它? enter image description here

 public class GameScreenFragment extends Fragment implements AdapterView.OnItemClickListener {

    int i = 0;
    private GridView gridView;
    private TextView textViewShowTime;
    public int[] tapherePictureArray = {R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here, R.drawable.tap_here};
    public int[] objectPictureArray = {R.drawable.bettry, R.drawable.htc_logo, R.drawable.uber, R.drawable.uber, R.drawable.htc_name, R.drawable.qualocommi, R.drawable.hi_res_audio, R.drawable.qualocommi, R.drawable.bettry, R.drawable.htc_name, R.drawable.htc_logo, R.drawable.hi_res_audio};
    public int[] intervalObjectArray = {R.drawable.bettry, R.drawable.htc_logo, R.drawable.uber, R.drawable.uber, R.drawable.htc_name, R.drawable.qualocommi, R.drawable.hi_res_audio, R.drawable.qualocommi, R.drawable.bettry, R.drawable.htc_name, R.drawable.htc_logo, R.drawable.hi_res_audio};
    public ArrayList<Integer> listRandomObjectPicture;
    public ArrayList<Integer> listRandomIntervalObjectPicture;
    MainActivity context;
    ImageAdapter imageAdapter;
    int isttId, secondId;
    int oddpos, evenpos;
    Chronometer stopWatch;
    long startTime;
    public ArrayList<Integer> disableposition;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_game_screen, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        gridView = (GridView) view.findViewById(R.id.grid_view);
        textViewShowTime = (TextView) view.findViewById(R.id.textView_show_time);
        stopWatch = (Chronometer) view.findViewById(R.id.chrono);
        gridView.setOnItemClickListener(this);

    }

    CountDownTimer countDownTimer;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        disableposition = new ArrayList<>();
        context = (MainActivity) getActivity();
        imageAdapter = new ImageAdapter(context, intervalObjectArray, disableposition);
        gridView.setAdapter(imageAdapter);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                imageAdapter = new ImageAdapter(context, tapherePictureArray, disableposition);
                gridView.setAdapter(imageAdapter);
                startTime = SystemClock.elapsedRealtime();
                countDownTimer = new CountDownTimer(45000, 1000) {

                    public void onTick(long millisUntilFinished) {
                        textViewShowTime.setText("" + millisUntilFinished / 1000);

                    }

                    public void onFinish() {
                        countDownTimer.cancel();
                        textViewShowTime.setText("00");
                        new AlertDialog.Builder(context)
                                .setTitle("Time Up")
                                .setCancelable(false)
                                .setMessage("Oops! Looks like you ran out of time. try it again")
                                .setPositiveButton("try again", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {

                                        countDownTimer.cancel();
                                        context.replaceFragmentWithoutBackstack(new GameScreenFragment());
                                        dialog.dismiss();
                                    }
                                })

                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .show();
                    }

                };
                countDownTimer.start();
            }
        }, 2000);


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        i++;
        Log.e("counter", i + "");
        if ((i % 2) == 0) {
            secondId = objectPictureArray[position];
            evenpos = position;
            tapherePictureArray[position] = objectPictureArray[position];
            imageAdapter.notifyDataSetChanged();
            gridView.invalidateViews();
            if (isttId == secondId && evenpos != oddpos) {
                tapherePictureArray[oddpos] = objectPictureArray[oddpos];
                tapherePictureArray[evenpos] = objectPictureArray[evenpos];
                disableposition.add(oddpos);
                disableposition.add(evenpos);
                Log.e("Size", disableposition.size() + "");
                imageAdapter.notifyDataSetChanged();
                gridView.invalidateViews();
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (disableposition.size() == 12) {
                            countDownTimer.cancel();
                            context.winTime = textViewShowTime.getText().toString();
                            int temptime = Integer.parseInt(context.winTime);
                            int actualTime = 45 - temptime;
                            context.winTime = String.valueOf(actualTime);

                            context.fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                            context.replaceFragment(new TimerShowFragment());
                        }
                    }
                }, 500);

            } else {
                i = 0;
                tapherePictureArray[position] = objectPictureArray[position];
                imageAdapter.notifyDataSetChanged();
                gridView.invalidateViews();
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tapherePictureArray[evenpos] = R.drawable.tap_here;
                        tapherePictureArray[oddpos] = R.drawable.tap_here;
                        imageAdapter.notifyDataSetChanged();
                        gridView.invalidateViews();

                    }
                }, 300);
            }
        } else {
            oddpos = position;
            isttId = objectPictureArray[position];
            tapherePictureArray[position] = objectPictureArray[position];
            imageAdapter.notifyDataSetChanged();
            gridView.invalidateViews();

        }


    }

    @Override
    public void onResume() {
        super.onResume();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}

0 个答案:

没有答案