Java 图像未被绘制到屏幕

Java 图像未被绘制到屏幕,java,android,Java,Android,我有一个爆米花的数组列表(一个包含x和y值的类),我正试图从中绘制一个名为happyPopcorn的图像。然而,这是行不通的。我的arraylist size从updateRunning()中的Log.d返回1,它应该返回1,但它没有绘制图像。这是我的密码: public class GameScreen extends Screen { enum GameState { Ready, Running, Paused, GameOver } GameState state = Game

我有一个爆米花的数组列表(一个包含x和y值的类),我正试图从中绘制一个名为happyPopcorn的图像。然而,这是行不通的。我的arraylist size从updateRunning()中的Log.d返回1,它应该返回1,但它没有绘制图像。这是我的密码:

public class GameScreen extends Screen {
enum GameState {
    Ready, Running, Paused, GameOver
}

GameState state = GameState.Ready;

// Variable Setup
// You would create game objects here.

Paint paint;
Graphics g = game.getGraphics();
ArrayList <Popcorn> popcorns = new ArrayList<Popcorn>();
Image happyPopcorn = Assets.happyPopcorn;
Random r = new Random();
int livesLeft = 1;
int popX = r.nextInt(g.canvasWidth() - 50);
int popY = 10;
long start = System.currentTimeMillis();

boolean isMoving = false;


public GameScreen(Game game) {
    super(game);

    // Initialize game objects here

    // Defining a paint object
    paint = new Paint();
    paint.setTextSize(30);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);

}

@Override
public void update(float deltaTime) {
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    // We have four separate update methods in this example.
    // Depending on the state of the game, we call different update methods.
    // Refer to Unit 3's code. We did a similar thing without separating the
    // update methods.

    if (state == GameState.Ready)
        updateReady(touchEvents);
    if (state == GameState.Running)
        updateRunning(touchEvents, deltaTime);
    if (state == GameState.Paused)
        updatePaused(touchEvents);
    if (state == GameState.GameOver)
        updateGameOver(touchEvents);
}

private void updateReady(List<TouchEvent> touchEvents) {

    // This example starts with a "Ready" screen.
    // When the user touches the screen, the game begins. 
    // state now becomes GameState.Running.
    // Now the updateRunning() method will be called!

    if (touchEvents.size() > 0)
        state = GameState.Running;
}

private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {

    //This is identical to the update() method from our Unit 2/3 game.
    isMoving = true;

    if (System.currentTimeMillis() - start > 300) {
        Popcorn p = new Popcorn(popX, popY);
        popcorns.add(p);
        start = System.currentTimeMillis();
    }

    Log.d("GameScreen", Integer.toString(popcorns.size()));

    for (int i = 0; i < popcorns.size(); i++) {
        if (popcorns.get(i).getY() > g.canvasHeight()) {
            popcorns.remove(i);
        }

        while (isMoving) {
            popcorns.get(i).tick();
        }
    }

    // 3. Call individual update() methods here.
    // This is where all the game updates happen.
    // For example, robot.update();
}

private void updatePaused(List<TouchEvent> touchEvents) {
    int len = touchEvents.size();
    for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {

        }
    }
}

private void updateGameOver(List<TouchEvent> touchEvents) {
    int len = touchEvents.size();
    for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {
            if (event.x > 300 && event.x < 980 && event.y > 100
                    && event.y < 500) {
                nullify();
                game.setScreen(new MainMenuScreen(game));
                return;
            }
        }
    }

}

@Override
public void paint(float deltaTime) {

    // First draw the game elements.

    // Example:
    // g.drawImage(Assets.background, 0, 0);
    // g.drawImage(Assets.character, characterX, characterY);

    g.clearScreen(Color.BLUE);

    for (int i = 0; i < popcorns.size(); i++) {
        g.drawImage(happyPopcorn, popcorns.get(i).getX(), popcorns.get(i).getY());
    }

    // Secondly, draw the UI above the game elements.
    if (state == GameState.Ready)
        drawReadyUI();
    if (state == GameState.Running)
        drawRunningUI();
    if (state == GameState.Paused)
        drawPausedUI();
    if (state == GameState.GameOver)
        drawGameOverUI();

}

private void nullify() {

    // Set all variables to null. You will be recreating them in the
    // constructor.
    paint = null;

    // Call garbage collector to clean up memory.
    System.gc();
}

private void drawReadyUI() {
    Graphics g = game.getGraphics();

    g.drawARGB(155, 0, 0, 0);
    g.drawString("Tap to start",
            400, 240, paint);

}

private void drawRunningUI() {
    Graphics g = game.getGraphics();
}

private void drawPausedUI() {
    Graphics g = game.getGraphics();
    // Darken the entire screen so you can display the Paused screen.
    g.drawARGB(155, 0, 0, 0);

}

private void drawGameOverUI() {
    Graphics g = game.getGraphics();
    g.drawRect(0, 0, 1281, 801, Color.BLACK);
    g.drawString("GAME OVER.", 640, 300, paint);

}

@Override
public void pause() {
    if (state == GameState.Running)
        state = GameState.Paused;

}

@Override
public void resume() {
    if (state == GameState.Paused)
        state = GameState.Running;
}
}
增加:


从我自己的混乱中,我发现问题可能在于paint()中的for语句,因为每当我在语句中发布以绘制对象时,它都不会绘制对象,但当我将其从语句中取出时,对象就被绘制了。

在进一步查看我的代码之后,我发现图像的y值是巨大的,这让我想到了
while(isMoving)
语句,并让我意识到它应该是
if(isMoving)
,在改变它之后,我的图像现在被绘制并移动了

public class Popcorn
{
int posX;
int posY;
int speed;

public Popcorn(int x, int y)
{
    posX = x;
    posY = y;
    speed = 2;
}

public void tick()
{
    posY -= speed;
}

public int getX()
{
    return posX;
}

public int getY()
{
    return posY;
}
}