绘制tilemap android时抛出如果循环错误

时间:2015-09-05 16:46:25

标签: android-canvas render android-bitmap

我无法将游戏的拼贴图加载到屏幕上。我的代码似乎很好,并且它不包含任何错误。我是Android游戏开发的新手,我似乎无法弄清楚我做错了什么。我已经了解了如何访问资产文件夹中的内容,但我不认为我做对了。对于此代码,我得到了throwifrecycled错误,请帮忙。

public class PlayState extends State {

private Amani amani;
private Background background, background2;

private static final int PLAYER_WIDTH = 66;
private static final int PLAYER_HEIGHT = 100;
InputStream is;

private float recentTouchY;
private ArrayList<Tile> tilearray = new ArrayList<Tile>();

@Override
public void init() {

    background = new Background(0, 0);
    background2 = new Background(800, 0);
    amani = new Amani(145, MainActivity.GAME_HEIGHT - 45 - PLAYER_HEIGHT,
            PLAYER_WIDTH, PLAYER_HEIGHT);

    try {
        is = MainActivity.assets.open("map.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    loadMap();

}

private void loadMap() {

    ArrayList lines = new ArrayList();
    int width = 0;
    int height = 0;

    BufferedReader reader = new BufferedReader(new InputStreamReader((is)));
    while (true) {
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (line == null) {
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        if (!line.startsWith("!")) {
            lines.add(line);
            width = Math.max(width, line.length());

        }
    }

    height = lines.size();

    for (int j = 0; j < 12; j++) {
        String line = (String) lines.get(j);
        for (int i = 0; i < width; i++) {

            if (i < line.length()) {
                char ch = line.charAt(i);
                Tile t = new Tile(i, j, Character.getNumericValue(ch));
                tilearray.add(t);
            }

        }
    }


}

@Override
public void update(float delta) {
    background.update(delta);
    background2.update(delta);
    Assets.runAnim.update(delta);
    amani.update(delta);
    for (int i = 0; i < tilearray.size(); i++) {
        Tile t = (Tile) tilearray.get(i);
        t.update();
    }
}

@Override
public void render(Painter g) {
    renderBackground(g);
    renderTiles(g); // 114
    renderAmani(g);
}

private void renderTiles(Painter g) {

    for (int i = 0; i < tilearray.size(); i++) {
        Tile t = (Tile) tilearray.get(i);
        g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY()); //122
    }
}

public void renderAmani(Painter g) {
    if (amani.isGrounded()) {
        Assets.runAnim.render(g, (int) amani.getX(), (int) amani.getY(),
                amani.getWidth(), amani.getHeight());
    } else {
        g.drawImage(Assets.jump, (int) amani.getX(), (int) amani.getY(),
                amani.getWidth(), amani.getHeight());
    }
}

public void renderBackground(Painter g) {
    g.drawImage(Assets.savanah, (int) background.getX(),
            (int) background.getY(), MainActivity.GAME_WIDTH,
            MainActivity.GAME_HEIGHT);
    g.drawImage(Assets.savanah, (int) background2.getX(),
            (int) background2.getY(), MainActivity.GAME_WIDTH,
            MainActivity.GAME_HEIGHT);
}

public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
    if (e.getAction() == MotionEvent.ACTION_DOWN) {
        recentTouchY = scaledY;
    } else if (e.getAction() == MotionEvent.ACTION_UP) {
        if (scaledY - recentTouchY < -50) {
            amani.jump();
        }
    }
    return true;
}

}

Painter类如下:

public class Painter {

private Canvas canvas;
private Paint paint;
private Rect srcRect;
private Rect dstRect;
private RectF dstRectF;

public Painter(Canvas canvas) {
    this.canvas = canvas;
    paint = new Paint();
    srcRect = new Rect();
    dstRect = new Rect();
    dstRectF = new RectF();
}

public void setColor(int color) {
    paint.setColor(color);
}

public void setFont(Typeface typeface, float textSize) {
    paint.setTypeface(typeface);
    paint.setTextSize(textSize);
}

public void drawString(String str, int x, int y) {
    canvas.drawText(str, x, y, paint);
}

public void fillRect(int x, int y, int width, int height) {
    dstRect.set(x, y, x + width, y + height);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawRect(dstRect, paint);
}

public void drawImage(Bitmap bitmap, int x, int y) {
    canvas.drawBitmap(bitmap, x, y, paint); // line 46
}

public void drawImage(Bitmap bitmap, int x, int y, int width, int height) {
    srcRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
    dstRect.set(x, y, x + width, y + height);
    canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
}

public void fillOval(int x, int y, int width, int height) {
    paint.setStyle(Paint.Style.FILL);
    dstRectF.set(x, y, x + width, y + height);
    canvas.drawOval(dstRectF, paint);
}

}

logcat的:

09-06 01:26:42.538: W/dalvikvm(3054): threadid=11: thread exiting with uncaught exception (group=0x40a741f8)
09-06 01:26:42.538: E/AndroidRuntime(3054): FATAL EXCEPTION: Game Thread
09-06 01:26:42.538: E/AndroidRuntime(3054): java.lang.NullPointerException
09-06 01:26:42.538: E/AndroidRuntime(3054):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:1037)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at android.graphics.Canvas.drawBitmap(Canvas.java:1078)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at com.game.util.Painter.drawImage(Painter.java:46)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at com.game.state.PlayState.renderTiles(PlayState.java:122)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at com.game.state.PlayState.render(PlayState.java:114)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at com.game.framework.GameView.updateAndRender(GameView.java:127)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at com.game.framework.GameView.run(GameView.java:113)
09-06 01:26:42.538: E/AndroidRuntime(3054):     at java.lang.Thread.run(Thread.java:856)

0 个答案:

没有答案