Java 向数组LibGDX添加实例

Java 向数组LibGDX添加实例,java,android,arrays,arraylist,libgdx,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 i

我试图在一个关卡图中这样做,只要有一个“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();
        }
    }
}
“Ground.java”:

“GameBlock.java”:

“Screen.java”:


在这种情况下,我仍然忽略了不必要的类,就像其他对象类和资产类一样。

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

String[][] level = {
  {"g", "r", "g"},
  {" ", "x", "t"},
  //...
};

for (int y = 0; y < level[0].length; y++)
{
  for (int x = 0; x < level.length; x++)
  {
      if (level[x][y].equals("g"))
        draw(groundTile, x * tilewidth, y * tileheight);
      else if (level[x][y].equals("x"))
        draw(otherTile, x * tilewidth, y * tileheight);
  }
}
String[][]级别={
{“g”,“r”,“g”},
{“x”,“t”},
//...
};
对于(int y=0;y

当你有大关卡的时候,这尤其方便。您完全知道根据相机位置、分辨率和平铺大小绘制什么。现在,您只需遍历数组的必要部分,而不是整个列表。

一个小提示:使用libgdx
array
类,而不是
ArrayList
。Libgdx集合已优化为,因此至少在移动设备上,它们可能会给您带来一点FPS提升。ArrayList在Libgdx中的行为与在其他任何地方的行为相同。尝试向基础类添加toString方法并记录列表toString(),以缩小错误范围。顺便说一句,您正在类构造函数外部实例化一个一次性sprite批,它将泄漏内存,因为它从未被处置。这到底意味着什么?我应该扔掉雪碧吗?我已经在主类中,并且该spritebatch被设置为使用它的每个类的spritebatch。那么,有人可以帮助我吗?我正在将实例添加到列表中是因为效率。那么,没有办法实现我要求的吗?我不明白为什么您的方法会更高效。arraylist本质上是一个有一点点过热的数组。就我所知,您的代码应该可以正常工作。试着调试并密切关注你的arraylist。我已经尝试了很多很多东西。如果我使用这个方法,那么我必须重复几行代码。这就是我说它更有效率的主要原因。而且,如果我使用你的方法,变量就不会改变。例如,我遇到了一种情况,对象有一个
活动的
布尔值(比如敌人)。如果我说
alive=falsemap[x][y].setAlive(false)
将与
map.get(mapWidth*y+x).setAlive
相同。我不明白你为什么要重复代码行,或者你的意思是什么。
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);
    }
}
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();
}
package com.mygdx.thetimetraveller;

public interface Screen {
    void load();
    void display();
}
String[][] level = {
  {"g", "r", "g"},
  {" ", "x", "t"},
  //...
};

for (int y = 0; y < level[0].length; y++)
{
  for (int x = 0; x < level.length; x++)
  {
      if (level[x][y].equals("g"))
        draw(groundTile, x * tilewidth, y * tileheight);
      else if (level[x][y].equals("x"))
        draw(otherTile, x * tilewidth, y * tileheight);
  }
}