我想在游戏中绘制连续掉落在屏幕两侧的房屋(huis =房屋)。 (在“世界”向下移动的草地上。”


但是,有了这段代码,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
    }
}


任何帮助,将不胜感激。

最佳答案

从我的角度来看,您的房屋阵列是空的-因此它没有任何内容可绘制。您创建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()中将一两个huisSprite添加到huisArray中。

希望能帮助到你 :)

10-01 05:57