I am making a app that requires dynamic animations. (Player movements) I'm using the Canvas
object to do this. My first question is "Is the Canvas
really the best way to handle these animations?",
and my second question is "How do I re-draw the player(s) to the Canvas
?" Here is my code:
theGame.java:
package birdprograms.freezetag;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class theGame extends Activity {
players[] arr = {
new player(),
new player(),
new player(),
new player()
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
}
public class myView extends View {
Paint paint = new Paint();
public myView(Context context) {
super(context);
paint.setColor(Color.YELLOW);
}
@Override
public void onDraw(final Canvas canvas) {
arr[0].update(true, true);
arr[0].draw(canvas, paint);
}
}
}
player.java
package birdprograms.freezetag;
import android.graphics.*;
public class player {
int y = 0;
int x = 0;
int vy = 5;
int vx = 5;
int height = y + 15;
int width = x + 15;
public void draw(Canvas canvas, Paint paint){
canvas.drawRect(x,y,width,height,paint);
}
public void update(boolean left, boolean top){
if(left){x += vx; width = x + 15;}
else{x -= vx; width = x + 15;}
if(top){y += vy; height = y + 15;}
else{y -= vy; height = y + 15;}
}
}