当计数标志增加到4时,如何停止android游戏应用程序中的计时器

当计数标志增加到4时,如何停止android游戏应用程序中的计时器,android,Android,我正在使用android编写一个简单的游戏应用程序,其中必须收集一个盒子 从屏幕右侧射向它的球。 收集橙色和粉色的球会得到一些分数,而 收集黑球会减少玩家的生命 目前,只要盒子遇到一个黑球,计时器就会停止 游戏结束了 我希望它做什么: 我希望在禁区结束后,遇到第四个黑球,计时器应该停止,比赛应该结束 我的尝试: 我声明了一个变量 private int countFlagBlack = 0; 然后,每当盒子遇到黑球时,我都递增countFlagBlack。 countFlagBlack增加到

我正在使用android编写一个简单的游戏应用程序,其中必须收集一个盒子 从屏幕右侧射向它的球。 收集橙色和粉色的球会得到一些分数,而 收集黑球会减少玩家的生命

目前,只要盒子遇到一个黑球,计时器就会停止 游戏结束了

我希望它做什么:

我希望在禁区结束后,遇到第四个黑球,计时器应该停止,比赛应该结束

我的尝试:

我声明了一个变量

private int countFlagBlack = 0; 
然后,每当盒子遇到黑球时,我都递增
countFlagBlack
countFlagBlack
增加到3,然后进入if块,计时器停止

 if(0 <= blackCenterX && blackCenterX <= boxSize && boxY <= blackCenterY && blackCenterY <= boxY + boxSize)
        {


         //PLEASE CHECK THIS BLOCK OF 
         //CODE.HERE WE NEED TO CHANGE
         //THE CODE

           countFlagBlack++;

           if(countFlagBlack>3)          
           {

                timer.cancel();
                timer=null;

           }

        }
if(0
package com.example.catcheggs1;

import android.graphics.Point; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;

import java.util.Timer; import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private TextView scoreLabel;
    private TextView startLabel;
    private ImageView box;
    private ImageView orange;
    private ImageView black;
    private ImageView pink;

    //Size
    private int frameHeight;
    private int boxSize;

    private int screenWidth;
    private int screenHeight;




    //Position
    private int boxY;
    private int orangeX;
    private int orangeY;
    private int pinkX;
    private int pinkY;
    private int blackX;
    private int blackY;

    private int countFlagBlack=0;

    //Score
    private int score=0;

    //Initialize Class
    private Handler handler = new Handler();
    private Timer timer = new Timer();

    //Status Check
    private boolean action_flg = false;
    private boolean start_flg = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scoreLabel=(TextView)findViewById(R.id.scoreLabel);
        startLabel=(TextView)findViewById(R.id.startLabel);
        box=(ImageView)findViewById(R.id.box);
        orange=(ImageView)findViewById(R.id.orange);
        pink=(ImageView)findViewById(R.id.pink);
        black=(ImageView)findViewById(R.id.black);

        //Get Screen Size
        WindowManager wm = getWindowManager();
        Display disp = wm.getDefaultDisplay();
        Point size = new Point();
        disp.getSize(size);

        screenWidth=size.x;
        screenHeight=size.y;


        //Move To Out of Screen
        orange.setX(-80);
        orange.setY(-80);
        pink.setX(-80);
        pink.setY(-80);
        black.setX(-80);
        black.setY(-80);

        scoreLabel.setText("Score : 0");

    }

    public void changePos()
    {

        hitCheck();

        //Orange
        orangeX -= 12;
        if(orangeX<0)
        {
            orangeX = screenWidth +20;
            orangeY = (int)Math.floor(Math.random()*(frameHeight-orange.getHeight()));
        }
        orange.setX(orangeX);
        orange.setY(orangeY);

        //Black
        blackX -= 20;
        if(blackX<0)
        {
            blackX=screenWidth+10;
            blackY = (int)Math.floor(Math.random()*(frameHeight-black.getHeight()));
        }
        black.setX(blackX);
        black.setY(blackY);

        //Pink
        pinkX -= 35;
        if(pinkX<0)
        {
            pinkX = screenWidth+20;
            pinkY = (int)Math.floor(Math.random()*(frameHeight-pink.getHeight()));
        }
        pink.setX(pinkX);
        pink.setY(pinkY);


        //Move Box

        if(action_flg == true)
        {
            //Touching
            boxY -= 8;
        }
        else
            {
              boxY += 3;
            }

        if(boxY<0)boxY =0;

        if(boxY > frameHeight-boxSize)boxY=frameHeight-boxSize;

        box.setY(boxY);

        scoreLabel.setText("Score: "+score);

    }

    public void  hitCheck()
    {
        //If the center of the ball is in the box, it counts as a hit.

        //Orange
        int orangeCenterX = orangeX + orange.getWidth()/2;
        int orangeCenterY = orangeY + orange.getHeight()/2;

        //0 <= orangeCenterX <= boxWidth
        //boxY <= orangeCenterY <= boxY + boxHeight

        if(0 <= orangeCenterX && orangeCenterX <= boxSize && boxY <= orangeCenterY && orangeCenterY <= boxY + boxSize)
        {
           score += 10;
           orangeX = -20;
        }

        //Pink
        int pinkCenterX = pinkX + pink.getWidth()/2;
        int pinkCenterY = pinkY + pink.getHeight()/2;

        if(0 <= pinkCenterX && pinkCenterX <= boxSize && boxY <= pinkCenterY && pinkCenterY <= boxY + boxSize)
        {
            score += 30;
            pinkX = -20;
        }

        //Black

        int blackCenterX = blackX + black.getWidth()/2;
        int blackCenterY = blackY + black.getHeight()/2;

        if(0 <= blackCenterX && blackCenterX <= boxSize && boxY <= blackCenterY && blackCenterY <= boxY + boxSize)
        {








         //PLEASE CHECK THIS BLOCK OF 
         //CODE.HERE WE NEED TO CHANGE
         //THE CODE










                timer.cancel();
                timer=null;



        }

    }

    public boolean onTouchEvent(MotionEvent me)
    {

        if(start_flg==false)
        {
            start_flg=true;

            FrameLayout frame = (FrameLayout)findViewById(R.id.frame);
            frameHeight = frame.getHeight();

            boxY = (int)box.getY();

            boxSize = box.getHeight();

            startLabel.setVisibility(View.GONE);

            //Call changePos() every 20 milli seconds
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            changePos();
                        }
                    });
                }
            },0,100);

        }
        else
        {
            if(me.getAction()==MotionEvent.ACTION_DOWN)
            {
                action_flg=true;
            }else if(me.getAction() == MotionEvent.ACTION_UP)
            {
                action_flg = false ;
            }
        }




       return true;
    } }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <TextView
        android:id="@+id/scoreLabel"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text=" : 300"
        android:paddingLeft="10dp"
        android:gravity="center_vertical"  />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/startLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="130dp"/>

        <ImageView

            android:id="@+id/box"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/box"
            android:layout_gravity="center_vertical" />

        <ImageView
            android:id="@+id/orange"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/orange" />

        <ImageView
            android:id="@+id/black"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/black" />

        <ImageView
            android:id="@+id/pink"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/pink" />



    </FrameLayout>

</RelativeLayout>