我正在尝试在 android 中制作乒乓球游戏,但我卡住了,我不知道为什么球不动

i am trying to make a pong game in android but i am stuck ,i dont know why ball isnt moving

这里是扩展表面视图的 class,在它的 运行 方法中我调用了动画球的方法但是球没有动画

 public  class OurView extends SurfaceView implements Runnable
{
    Canvas c;
    Thread t = null;

    SurfaceHolder holder;
    boolean isItOk = false;
    Rect racketTop,racketBottom;


    int TopLeft ;
    int TopRight ;
    int TopTop ;
    int TopBottom;

    int bottomLeft;
    int bottomRight;
    int bottomTop;
    int bottomBottom;

    GameState state;



    public OurView(Context context) {
        super(context);
        holder = getHolder();
        state = new GameState();
    }

    @Override
    public void run() {

        while (isItOk == true) {

            if (!holder.getSurface().isValid())
                continue;

            c = holder.lockCanvas();
            c.drawARGB(0, 0, 0, 0);
            TopLeft = ((c.getWidth() / 2) / 2) + 50;
            TopRight = (c.getWidth() / 2) + ((c.getWidth() / 2) / 2) - 50;
            TopTop = getTop();
            TopBottom = 10;

            bottomLeft = ((c.getWidth() / 2) / 2) + 50;
            bottomRight = (c.getWidth() / 2) + ((c.getWidth() / 2) / 2) - 50;
            bottomTop = getBottom() - 10;
            bottomBottom = getBottom();


            racketTop = new Rect();                                                                  // for top racket
            racketTop.set(TopLeft, TopTop, TopRight, TopBottom);                                      //left = ((c.getWidth()/2)/2)+50
                                                                                                                //top = getTop()
                                                                                                                //right = (c.getWidth()/2)+((c.getWidth()/2)/2)-50
                                                                                                                //bottom = 10


            racketBottom = new Rect();                                                         // FOR BOTTOM RACKET
            racketBottom.set(bottomLeft, bottomTop, bottomRight, bottomBottom);                                                 //left = ((c.getWidth()/2)/2)+50
                                                                                                                // top = getBottom()-10
                                                                                                                // right = (c.getWidth()/2)+((c.getWidth()/2)/2)-50
                                                                                                                // bottom = getBottom()


            Paint white = new Paint();
            white.setColor(Color.WHITE);
            white.setStyle(Paint.Style.FILL);

            c.drawRect(racketTop, white);
            c.drawRect(racketBottom, white);

这里我调用了方法

            state.update();

            state.draw(c,white);
            holder.unlockCanvasAndPost(c);




        }
    }
     public void pause()
    {
        isItOk = false;
        while(true)
        {
            try
            {
                t.join();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }

            break;
        }

        t = null;
    }

    public void resume()
    {
        isItOk = true;
        t = new Thread(this);
        t.start();


    }

这里是 class 球被定义的地方

     public class GameState {




int ballx,bally;        //ball x and y position
int ball_size = 20;          // size of ball
static int ballvelx = 3;    // velocity x of ball
static int ballvely = 3;    // velocity y of ball



public GameState() {

}




public void update()
{
    ballx += ballvelx;
    bally += ballvely;

}

public void draw(Canvas canvas, Paint paint)
{
     //objects color
    paint.setColor(Color.WHITE);

    ballx = canvas.getWidth()/2;
    bally = canvas.getHeight()/2;

    //ball
    canvas.drawCircle(ballx,bally,ball_size,paint);
}

}

帮助我这是我的第一个 android 游戏

不判断其余代码:

ballx = canvas.getWidth()/2;
bally = canvas.getHeight()/2;

这会在您每次画球时重置球的位置。 难怪当你重置它的位置时它不动。
而是调用 update(),所以球被移动了。