类不会在画布上绘制

时间:2014-05-28 07:12:15

标签: android

为什么这个AI类不会绘制到画布?

package com.dragon.game;

import android.graphics.*;
import com.dragon.game.Game.*;
import java.util.*;

public class AI {
    public static Bitmap a;
    int x, y, dirX, dirY;
    int width, height;
    long start, stop;
    boolean fg = true;
    SurView ov;
    Random r = new Random();

    public AI(SurView surView, Bitmap ai){
        a = ai;
        ov = surView;
        width = a.getWidth();
        height = a.getHeight();
        x = Remote.wt / 4;
        y = Remote.wt / 4;
    }

    public void update(){
        x++;
    }

    public void onDraw(Canvas canvas){
        if(fg){
            x = Remote.wt / 4;
            y = Remote.wt / 4;
            fg = false;
        }

        update();
        Rect src = new Rect(0, 0, width, height);
        Rect dst = new Rect(x, y, x + Remote.pw, y + Remote.pw);
        canvas.drawBitmap(a, src, dst, null);
    }
}

1 个答案:

答案 0 :(得分:0)

原因是你已经在一个公共AI方法中初始化了Bitmap,然后你在onDraw方法中使用它,你不能这样做,要么必须全局初始化它,要么你必须给它一个引用