Hello i have a little problem with moving ball around screen, also i'm confused why i cannot put this variables double x = 0, y = 16, muvx = 0, muvy = 0;
at the public class MainActivity to be local variables and also i don't know how to set the timer to be on 0.5 sec. Timer t = new Timer(this, 5);
its shows me error at this line too. Here is the code please explain me what causes the errors and how do i make the ball move, Thank you
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;
}
}
}}