为什么我的程序不从arrayList中绘制房屋?

时间:2014-06-20 19:08:12

标签: java android arraylist libgdx

我希望在我的游戏中画出房子(huis = house),它们会一直在屏幕两侧坠落。 (在草地上,'世界'向下移动。' screenshot

但是,有了这个代码,在logcat中没有麻烦我不知道为什么它不会画房子。 这段代码的“主要流程”如下:我创建了一个包含house.png的arraylist。然后通过for循环,当它之间有一些空间时,它会添加一个房子。

package com.mygdx.Papermadness;

import java.util.ArrayList;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class Papermadness extends InputAdapter implements ApplicationListener {
    private SpriteBatch huisBatch;
    private Texture huisTexture;
    private SpriteBatch batch;
    private Sprite sprite;
    private SpriteBatch spriteBatch;
    private Sprite huisSprite;
    private Texture spriteTexture;
    private float scrollTimer = 0.0f;
    private float huisVelocity = 200f;
    private float huisLinksY = 2100;
    private float huisLinksX = 0;
    private float huisRechtsY = 2100;
    private float huisRechtsX = 903;
    private Sprite huis;
    private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
    Rectangle bounds;
    Player player;
    Paper paper;
    ShapeRenderer sr;

    @Override
    public void create() {
        player = new Player(new Vector2(50, 100), new Vector2(100, 100));
        paper = new Paper(new Vector2(Gdx.input.getX(),Gdx.graphics.getHeight()-Gdx.input.getY()), new Vector2(50, 50));
        sr = new ShapeRenderer();
        spriteBatch = new SpriteBatch();
        huisBatch = new SpriteBatch();
        huisTexture = new Texture("huis.png");
        huisSprite = new Sprite(huisTexture);
        spriteTexture = new Texture("b2.png");
        spriteTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
        sprite = new Sprite(spriteTexture);
        sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch = new SpriteBatch();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        float delta = Gdx.graphics.getDeltaTime();
        scrollTimer += Gdx.graphics.getDeltaTime();
        if (scrollTimer > 2f)
            scrollTimer = 0.0f;
        sprite.setV(scrollTimer + 2);
        sprite.setV2(scrollTimer);
        player.update();
        paper.update();
        spriteBatch.begin();
        sprite.draw(spriteBatch);
        for (int i = 0; i < huisArray.size(); i++) {
            huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
            if (huisArray.get(i).getY() <= 200) {
                huisArray.remove(i);
                i--;
            }               
        }
        if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
            addNewHuis();
        }
        huisBatch.begin();
        for (int i = 0; i < huisArray.size(); i++) {
            huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
        }
        huisLinksY -= huisVelocity * delta;
        huisRechtsY -= huisVelocity * delta;
        batch.begin();
        player.draw(batch);
        paper.draw(batch);
        batch.end();
        sr.begin(ShapeType.Filled);
        sr.setColor(Color.BLUE);
        sr.setColor(Color.RED);
        sr.setColor(Color.YELLOW);
        sr.rect(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY(), paper.getSize().x,
        paper.getSize().y);
        sr.end();
    }

    private void addNewHuis() {           
        huis = new Sprite();
        huis.setY(1800);
        huisArray.add(0, huis);
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub  
    }
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

从我的观点来看,你的房子阵列是空的 - 所以它没有任何东西可以绘制。您创建huisArray,但不向其添加任何内容。因此,当您首先调用render()时,huisArray.size()== 0为true。

for (int i = 0; i < huisArray.size(); i++) { //you do not step into this piece of code.
            huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
            if (huisArray.get(i).getY() <= 200) {
                huisArray.remove(i);
                i--;
            }               
        }

然后,如果那里有一些房子,你想要加一个房子。但它仍然是空的,所以你不添加任何东西。

if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
    addNewHuis();
}

你试图画出空的数组:

huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) { //you do not step into this piece of code either
    huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
}

解决方案:在create()中向huisArray添加一两个huisSprite。

希望有所帮助:)