这样的话我一些初始化的图形没法被擦除啊?或者我只要一次初始化改怎么办啊?
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
initGame();
Paint paint=new Paint();
paint.setColor(Color.WHITE);
System.out.println("------------------onDraw()--------------------");
for (int x = 0; x <xcount; x++) {
for (int y = 0; y < ycount; y++) {
if(map[x][y]>0){
canvas.drawBitmap(pics[map[x][y]], 
xoffset+x*size, yoffset+y*size, paint);

}

}
}
}
我做的是一个贪吃蛇的游戏,这是我的onDraw函数
snakeList.remove(snakeList.size()-1);我这句话发现没法实现,我要去掉贪吃蛇的尾巴,这样看起来蛇就往前移动了。
附:
下面是我完整的代码(我是按照土豆网上的视频教程写的):
package com.gushedaoren;import java.util.ArrayList;import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;public class Tile extends View {
private int size=12;
private int xcount;
private int ycount;
private int xoffset;
private int yoffset;
private int[][] map;
private Bitmap[] pics;
public int mn=5;
public static final int GREEN_STAR=1;
public static final int RED_STAR=2;
public static final int YELLOW_STAR=3;
public static final int UP=1;
public static final int RIGHT=2;
public static final int DOWN=3;
public static final int LEFT=4;
public int direction=RIGHT;

private ArrayList<Coordinate> snakeList=new ArrayList<Coordinate>();


public void loadPic(int key,Drawable drawable){
Bitmap bitmap=Bitmap.createBitmap(size, size, Config.ARGB_8888);
Canvas canvas=new Canvas(bitmap);
drawable.setBounds(0, 0, size, size);
drawable.draw(canvas);
pics[key]=bitmap;
} public Tile(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
setFocusable(true);
}
public void clearTile(){
for (int x = 0; x < xcount; x++) {
for (int y = 0; y < ycount; y++) {
setTile(0, x, y);
}

}
}
private Myhandler handler=new Myhandler();
class Myhandler extends Handler{ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);

Tile.this.invalidate();//重绘
update();

}
public void sleep(int delay){
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), 600);
}

}
public void update(){
clearTile();
buildWall();
System.out.println("----------------update()---------------------");
updateSnake();
handler.sleep(600);
}
public void updateSnake(){
//mn++;
//setTile(RED_STAR, 5, mn++);
Coordinate header=snakeList.get(0);
Coordinate newHeader=new Coordinate(1,1);

//newHeader=new Coordinate(header.x+1, header.y);

switch (direction) {
case UP:
newHeader=new Coordinate(header.x, header.y-1);
break; case RIGHT:
newHeader=new Coordinate(header.x+1, header.y);
break;
case DOWN:
newHeader=new Coordinate(header.x, header.y+1);
break;
case LEFT:
newHeader=new Coordinate(header.x-1, header.y);
break;
default:
break;

}
snakeList.add(0,newHeader);
snakeList.remove(snakeList.size()-1);

System.out.println("------------------------updateSnake------------------");
int index=0;
for (Coordinate c : snakeList) {
if(index==0){
setTile(YELLOW_STAR,c.x,c.y);
}
else{
setTile(RED_STAR, c.x, c.y);
}
System.out.println(index);
index++;
}


}







@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
System.out.println("++++++++++++++++ONKEYDWON++++++++++++++");
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
direction = UP;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
direction = RIGHT;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
direction = DOWN;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
direction = LEFT;
break; }

return super.onKeyDown(keyCode, event);

} public void buildWall(){
for (int x= 0; x < xcount; x++) {
setTile(GREEN_STAR,x,0);
setTile(GREEN_STAR,x,ycount-1);
}
for (int y = 0; y < ycount; y++) {
setTile(GREEN_STAR,0,y);
setTile(GREEN_STAR,xcount-1,y);
}
}
public void initGame(){
Resources r=getContext().getResources();
pics=new Bitmap[4];
loadPic(GREEN_STAR,r.getDrawable(R.drawable.greenstar));
loadPic(RED_STAR,r.getDrawable(R.drawable.redstar));
loadPic(YELLOW_STAR,r.getDrawable(R.drawable.yellowstar));
buildWall();

snakeList.add(new Coordinate(5,7));
snakeList.add(new Coordinate(4,7));
snakeList.add(new Coordinate(3,7));
snakeList.add(new Coordinate(2,7));
//direction=RIGHT;
System.out.println("-------------initGame--------------");
update();
}
public void setTile(int picIndex,int x,int y){
map[x][y]=picIndex;
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
xcount=(int) Math.floor(w/size);
ycount=(int) Math.floor(h/size);
xoffset=(w-size*xcount)/2;
yoffset=(h-size*ycount)/2;
map=new int[xcount][ycount];
} @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
initGame();
Paint paint=new Paint();
paint.setColor(Color.WHITE);
System.out.println("------------------onDraw()--------------------");

/* canvas.drawLine(0, 0, 150, 150, paint);
canvas.drawBitmap(pics[RED_STAR], 80, 120, paint);*/

for (int x = 0; x <xcount; x++) {
for (int y = 0; y < ycount; y++) {
if(map[x][y]>0){
canvas.drawBitmap(pics[map[x][y]], 
xoffset+x*size, yoffset+y*size, paint);


}


}
}
}
private class Coordinate{
private int x;
private int y;
public Coordinate(int x, int y) {
super();
this.x = x;
this.y = y;
}
//冲突检查,蛇是否吃到苹果
public  boolean equlals(Coordinate other){
if(x==other.x&&y==other.y){
return true;

}
return false;
}
}}