我有一个Popcorn的ArrayList(一个包含x和y值的类),我试图从中绘制一个名为happyPopcorn的Image。但是,这不起作用。我的arraylist大小从updateRunning()中的Log.d返回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;
}
}
Popcorn.java:
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;
}
}
增加:
从我自己搞砸了一下,我发现问题可能出在paint()中的for语句,因为每当我在语句中绘制一个对象时,它都不会绘制它,但是当我拿出它时在声明之外,对象被绘制。
答案 0 :(得分:0)
在进一步查看我的代码之后,我发现图像的y值非常大,然后导致我进入while(isMoving)
语句并让我意识到它应该是if (isMoving)
并在更改之后,我的形象现在被涂上了画笔。