将实例添加到数组LibGDX

时间:2015-09-23 03:32:16

标签: java android arrays arraylist libgdx

我试图在级别映射中实现它,只要有" g",就会将新的Ground实例附加到ArrayList。当我使用.add()方法时,似乎每次调用函数后数组列表都会重置。这是我的代码:

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.ArrayList;
import java.util.List;

public class Game implements Screen {
    SpriteBatch batch = new SpriteBatch();

    List<Ground> groundList = new ArrayList<Ground>();

    String[] level = {
        "                    ",
        "                    ",
        "                    ",
        "                    ",
        "                    ",
        "                    ",
        "      g             ",
        "g                   ",
        "ggggggggggggg ggg gg",
        "g                   "
    };

    Game(SpriteBatch batch) {
        this.batch = batch;
        this.load();
    }

    @Override
    public void load() {
        for(int c = 0; c < level.length; c ++){
            for(int r = 0; r < level[c].length(); r ++){
                switch(level[c].charAt(r)) {
                    case 'g':
                        groundList.add(new Ground(r * 108, c * 108, this.batch));
                        break;
                }
            }
        }
    }

    @Override
    public void display() {
        for(int i = 0; i < groundList.size(); i ++) {
            groundList.get(i).Draw();
        }
    }
}

当我运行时,只显示一个地块,这是最后一个&#34; g&#34;在数组中(底行,第一个字符)。那么,我怎样才能让它成为一个新实例被推入arraylist?或者,我可以使用arraylist的替代品吗?总的来说,我希望能够循环遍历任何列表,并在.Draw()方法的嵌套for循环中附加的每个实例上调用load()方法。我知道有很多StackOverflow论坛的人会问如何在常规java中附加到arraylist,但是LibGDX的行为不同,所以我找不到任何帮助。提前谢谢!

该代码位于&#34; Game.java&#34;。这是代码的其余部分: &#34; Main.java&#34;:

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Main extends ApplicationAdapter {
    SpriteBatch batch;
    OrthographicCamera camera;
    Screen gameState;

    private int gameScene = 4;

    @Override
    public void create () {
        batch = new SpriteBatch();
        gameState = new Game(batch);
        camera = new OrthographicCamera();
        camera.setToOrtho(true, 1920, 1080);
        Assets.imagesLoad();
    }

    @Override
    public void dispose () {
        batch.dispose();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        switch (gameScene) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                gameState = new Game(batch);
                break;
        }
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
            gameState.display();
        batch.end();
    }
}

&#34; Ground.java&#34;:

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Ground extends GameBlock {
    Ground(double x, double y, SpriteBatch batch) {
        setX(x);
        setY(y);
        setBatch(batch);
    }

    @Override
    public void Draw() {
        batch.draw(Assets.simpleBlockTexture01_sprite, (float) x, (float) y);
    }
}

&#34; GameBlock.java&#34;:

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public abstract class GameBlock {
    protected static double x;
    protected static double y;
    protected static SpriteBatch batch = new SpriteBatch();

    public void setX(double x) {
        this.x = x;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getX() {
        return this.x;
    }
    public double getY() {
        return this.y;
    }
    public void setBatch(SpriteBatch batch) {
        this.batch = batch;
    }
    public abstract void Draw();
}

&#34; Screen.java&#34;:

package com.mygdx.thetimetraveller;

public interface Screen {
    void load();
    void display();
}

我还在为这种情况留下不必要的类,比如其他对象类和资产类。

1 个答案:

答案 0 :(得分:0)

为什么要将这些理由添加到列表中?为什么你不使用你的阵列直接绘制你需要的东西?我也会选择一个多维数组。看看以下内容:

{{#location}}
<div class="loc-name">{{name}}</div>
<div>{{address}}</div>
<div>{{address2}}</div>
<div>{{town}}{{#if town}},{{/if}} {{county}} {{postal}}</div>
<div>{{hours1}}</div>
<div>{{hours2}}</div>
<div>{{hours3}}</div>
<div>{{phone}}</div>
<a href="email">{{email}}</a>
<div><a href="{{web}}" target="_blank">{{niceURL web}}</a></div>
{{/location}}

当您拥有较大的等级时,这尤其方便。你完全知道根据相机位置,分辨率和瓷砖大小来绘制什么。现在你只需要在整个列表中遍历数组的必要部分。