0

我正在制作一个应用程序,并且它可以正常工作,但幸运的是我知道我的问题是什么。我有一个围绕边界移动的精灵,我希望精灵在特定位置射球。目前,我的应用程序在位置 (0,0) 处显示球,然后在应该的时候将其射过去。它通过在更新方法中向 x 添加 10 来实现这一点,因此它是一个动画,我可以通过更改 10 来加快速度以减慢速度。变量 x 和 y 的默认值为 0。我比按照上面指定的方式更改它们。我希望它们默认保持为 0,但我不希望球在移动之前显示。现在它在启动时显示球,然后移动。一旦我给出 x 和 y 值,我希望它显示球。

这是当前代码的片段。

        // This moves the ball down the right side of screen
        if (x > ov.getWidth() - width - xSpeed){
            xSpeed = 0;
            ySpeed = 10;
            direction = 1;
            shootL(); // shoots the ball left towards the left side of screen

        }

              // moves ball up the screen on left side
        if (x + xSpeed < 0){
            x = 0;
            xSpeed = 0;
            ySpeed = -10;
            direction = 3;
            shootR();  // shoots the ball towards right side of screen
        }


    private void shootR() {
        // TODO Auto-generated method stub
        // need to manipulate variable q to go across screen
        q += 10;
           // eventually manipulate r so that it shoots at the right spot on screen, not my major problem right now.

    }


    private void shootL() {
        // TODO Auto-generated method stub
        // need to manipulate variable q to go across screen
        q += -10;
           // eventually manipulate r so that it shoots at the right spot on screen, not my major problem right now.
    }


    public void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        update();
        int srcY = direction * height;
        Rect src = new Rect (0, srcY, width, srcY + height);
        Rect dst = new Rect (x, y, x+width, y+height);
        canvas.drawBitmap(b, src,  dst, null); // draws sprite around border
        Paint p = new Paint();
        canvas.drawBitmap(fire, q, r, p); // draws ball that shoots across


    }   

有人有什么想法或建议吗?它只是在错误的时间显示球,我需要解决这个问题。一定是错误什么的。

4

1 回答 1

0

哇,我一无所知。我想到了。我在 onDraw 方法中调用的更新方法中更改了坐标。onDraw 方法遍历每一行代码,然后再次调用,一次更新一个。通过在 onDraw 中添加 if 语句并在 update 中删除 q 的更改并将其移动到 if 语句中,直到 if 为真时它才绘制。

我在改变之前正在画画。您需要更改变量,然后绘制!

于 2013-07-18T05:33:59.897 回答