我该如何移动球?

时间:2014-10-13 17:12:45

标签: android variables

您好我在屏幕上移动球有一点问题,我也很困惑为什么我不能将公共类double x = 0, y = 16, muvx = 0, muvy = 0;中的这些变量MainActivity更改为局部变量而且我不知道#39;知道如何将计时器设置为0.5秒。 Timer t = new Timer(this, 5);这也向我显示了这一行的错误。以下是代码,请向我解释导致错误的原因以及如何让球移动。

public class MainActivity extends Activity  {

static int F_WIDTH = 800;
static int F_HEIGHT = 600;
static int MIN_X = 0; // left - ball
static int MIN_Y = 16; // up - ball
static int MAX_X = 742; // right - ball
static int MAX_Y = 521; // down - ball
static int BALL_SIZE_X = 100;
static int BALL_SIZE_Y = 100;
Timer t = new Timer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new ArcView(this));
    RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.main_layout);

    rlayout.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            right(); // <---- this is the error
        }

    });
}
static class ArcView extends View {
    Paint paint;
    Path path;
    RectF rect;

    public ArcView(Context context) {
        super(context);
        rect = new RectF(50, 50, BALL_SIZE_X, BALL_SIZE_Y);

        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.CYAN);
        paint.setStrokeWidth(30);

        path = new Path();
        path.addArc(rect, 360, 360);
        setBackgroundColor(Color.WHITE);
    }
    float x = 0, y = 16, muvx = 0, muvy = 0;

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x,y,40, paint);
    }

    public void right() {
        muvx = 2;
        muvy = 0;
    }

    public void actionPerformed() {
        invalidate();
        x += muvx;
        y += muvy;
        if (x < MIN_X) {
            x = MIN_X;
        }
        if (x > MAX_X) {
            x = MAX_X;
        }
        if (y < MIN_Y) {
            y = MIN_Y;
        }
        if (y > MAX_Y) {
            y = MAX_Y;
        }
    }
}}

0 个答案:

没有答案
相关问题