当前位置: 首页 > 知识库问答 >
问题:

(libgdx/java)ArrayList不会清除/删除类的实例?

龙德义
2023-03-14

项目类别:

       public class Item {
          public float x, y, speedx, speedy;
        public Rectangle container;
        public Texture texture;
       static Timer timer = new Timer();
      static  int amount;
       static int spawned;
        public int itemtype;
        //   float delay = 1; // seconds

        public void move() {
            x += speedx;
            y += speedy;

            container.x = x;
            container.y = y;

        }
  public void setTexture(int itemtype){
        switch(itemtype){
            case 1:
                texture = new Texture("items/item1.png");
                break;
            case 2:
                texture = new Texture("items/item2.png");
                break;
            case 3:
                texture = new Texture("items/item3.png");
                break;
            case 4:
                texture = new Texture("items/item4.png");
                break;
            case 5:
                texture = new Texture("items/item5.png");
                break;
            case 6:
                texture = new Texture("items/item6.png");
                break;
            case 7:
                texture = new Texture("items/item7.png");
                break;
            case 8:
                texture = new Texture("items/item8.png");
                break;
            case 9:
                texture = new Texture("items/item9.png");
                break;
            case 10:
                texture = new Texture("items/item10.png");
                break;
            default:
                texture = new Texture("items/error.png");
                break;
        }

    }
          public static void spawnItem(int amount){
            Item.amount = amount;
              mainscreen.items.clear();
                  //  for(int spawned = 0; spawned <= amount; spawned++){



                        timer.schedule(new Timer.Task() {

                            @Override
                            public void run() {
                                if (mainscreen.canclick == false) {
                                    Item item = new Item();
                                    item.x = 600;
                                    item.y = -42;
                                    item.speedx = -20;
                                    item.speedy = 0;
                                    Rectangle itemcontainer = new Rectangle();
                                    itemcontainer.x = item.x;
                                    itemcontainer.y = item.y;
                                    itemcontainer.width =                       mainscreen.container.getWidth() / 4f;
                                    itemcontainer.height = mainscreen.container.getHeight() - 15f;
                                    item.container = itemcontainer;
         item.itemtype = MathUtils.random(1, 10);
item.setTexture(item.itemtype);
                                    mainscreen.items.add(item);
                                    spawned++;
                                }
                                for (Item item : mainscreen.items) {
                                    if (item.x <= -4000) {
                                        if (spawned >= Item.amount) {
                                            mainscreen.canclick = true;


                                            timer.stop();
                                            spawned = 0;
                                        }

                                    } else {

                                    }
                                }
                           }
                        }, 0, 0.325f);

                }
    public void dispose(){
        texture.dispose();
    }
            }

主屏幕类别:

public class mainscreen implements Screen, GestureDetector.GestureListener,InputProcessor {
    @Override
    public void render(float delta) {
        this.delta = delta;
        Gdx.gl.glClearColor(115 / 255F, 115 / 255F, 115 / 255F, 1 / 255F);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);

      if(Gdx.input.justTouched()) {

          Vector3 touch1 = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
          camera.unproject(touch1);


          if (debug.contains(touch1.x, touch1.y)) {
              items.clear();
          }

          if (start.contains(touch1.x, touch1.y)) {
          if (canclick == true) {

                  canclick = false;

    Item.spawnItem(20);
              }

          }
      }

}

日志:第一次单击开始时:(计时器完成后)

canclick: true
items list: [com.abc.luckyllama.Item@37237aa0, com.abc.luckyllama.Item@2de938e3, com.abc.luckyllama.Item@3cb912d5, com.abc.luckyllama.Item@2bae592c, com.abc.luckyllama.Item@774c083, com.abc.luckyllama.Item@633edeae, com.abc.luckyllama.Item@176557a6, com.abc.luckyllama.Item@4edb1b5f, com.abc.luckyllama.Item@6f8abadf, com.abc.luckyllama.Item@7a54d22e, com.abc.luckyllama.Item@473162a5, com.abc.luckyllama.Item@51a698ff, com.abc.luckyllama.Item@6bc08c56, com.abc.luckyllama.Item@37d9e6a2, com.abc.luckyllama.Item@7bb19eb6, com.abc.luckyllama.Item@1eb5805f, com.abc.luckyllama.Item@71780de3, com.abc.luckyllama.Item@9ec0998, com.abc.luckyllama.Item@7edf723d, com.abc.luckyllama.Item@4c5aa2c1]

单击调试按钮后(清除数组列表):

canclick: true
items list: []

再次点击开始按钮后:(计时器完成后)

 canclick: true
items list: [com.abc.luckyllama.Item@7d7cb9bc, com.abc.luckyllama.Item@1435cf42, com.abc.luckyllama.Item@117e1963, com.abc.luckyllama.Item@82bfd27, com.abc.luckyllama.Item@108214c7, com.abc.luckyllama.Item@2a77864a, com.abc.luckyllama.Item@4b232766, com.abc.luckyllama.Item@1cb629e0, com.abc.luckyllama.Item@1c92229d, com.abc.luckyllama.Item@ac1b293, com.abc.luckyllama.Item@588bbcba, com.abc.luckyllama.Item@75df6762, com.abc.luckyllama.Item@78d4358e, com.abc.luckyllama.Item@7f86452d, com.abc.luckyllama.Item@7aed480b, com.abc.luckyllama.Item@7407d443, com.abc.luckyllama.Item@2da6e708, com.abc.luckyllama.Item@604470bc, com.abc.luckyllama.Item@70f9d1af, com.abc.luckyllama.Item@3a16a63f, com.abc.luckyllama.Item@201288d2, com.abc.luckyllama.Item@6310ddfc, com.abc.luckyllama.Item@5d5a1c98, com.abc.luckyllama.Item@52727e52, com.abc.luckyllama.Item@669228d6]

您可以看到ArrayList中的项目没有被清除。它增加了。我认为这是因为在spawnItem()中创建的Item实例仍然存在。我该如何解决这个问题?

我注意到每次我点击按钮都没有更多的项目。项目产生得更快。但是如何阻止这种情况呢?

共有1个答案

龙俭
2023-03-14

我修复了它!问题是我需要创建一个计时器。任务分开,使用task.cancel();来停止计时器。任务:

import com.badlogic.gdx.utils.Timer;
public class Item {
public static Timer.Task task;
   public static void spawnItem(int amount){
        Item.amount = amount;

        task = new Timer.Task() {
            @Override
            public void run() {

                if (mainscreen.canclick == false) {
                    item = new Item();

                    item.x = 600;
                    item.y = -42;
                    item.speedx = -20;
                    item.speedy = 0;
                    Rectangle itemcontainer = new Rectangle();
                    itemcontainer.x = item.x;
                    itemcontainer.y = item.y;
                    itemcontainer.width = mainscreen.container.getWidth() / 3f;
                    itemcontainer.height = mainscreen.container.getHeight() - 15f;
                    item.container = itemcontainer;
                    item.itemtype = MathUtils.random(1, 10);

                    item.setTexture(item.itemtype);


                    mainscreen.items.add(item);
                    mainscreen.itemsspawned += 1;


                    //   mainscreen.items.remove(item);


                    spawned++;
                }
                for (Item item : mainscreen.items) {
                    if (item.x <= -4000) {
                        if (spawned >= Item.amount) {
                            mainscreen.canclick = true;

                            timer.clear();
                            timer.stop();
                            task.cancel();
                            spawned = 0;

                        }

                    }
                }


            }
        };


        timer.schedule(task, 0, 0.4f);

    }
}
 类似资料:
  • 我在互联网上看了很多,但不知何故,找不到任何好处,总是清除屏幕在渲染方法或不太理解它。 那么清除屏幕的好处是什么?清除不会导致游戏在每次调用render方法时从头开始绘制所有内容吗?

  • 我的实体。ValidationStep与documentDetail有一对一的关系,documentDetail与documentValidations有一个完全的关系 我的删除查询 父ValidationStep被删除,但是docDetail和documentValidations仍然在数据库中。

  • 问题内容: 我有成员类的简单ArrayLists: 会员等级: 朋友的ArrayList包含所有用户朋友。我只希望从好友列表中删除组成员(如果存在): 但这对列表没有任何帮助… 查看日志语句,该朋友实际上确实出现在列表中。 为什么不起作用? 问题答案: 如何确定2个成员相等?我猜它们是否具有相同的ID,您认为它们相等,但是Java希望它们成为内存中的完全相同的引用,而事实并非如此。要更正此问题,您

  • 问题内容: 谢谢Marko。我重写代码。尝试使其简单。这次它可以真正编译。但它只能删除彼此相邻的重复项。例如,如果我输入1 2 3 3 4 4 5 1,则输出为1 2 3 4 5 1。最后它无法拾取重复项。(顺便说一句:本网站的新内容,如果使任何显示混乱我的歉意) 这是新的代码: 问题答案: 要回答“删除java arraylist中的重复项”问题: 只需将所有元素放到中,就可以完成。 -要么-

  • 问题内容: 如果满足条件,我需要从中删除一些对象,我想知道哪种方法会更有效。 情况如下:我有一个包含包含其他一些对象的类。我必须对此进行迭代,并删除满足特定条件的所有元素。据我所知,这些是我要删除的选项: 创建一个新的并添加不符合条件的元素。迭代之后,从旧的数组列表交换到没有元素的新数组列表。 创建一个新的并添加满足条件的元素。迭代后,使用传递要删除对象的方法。 有没有更有效的方法从中删除对象?

  • 我有一个名为Interval的setInterval,它运行一个倒计时计时器。我有一个开始按钮,第一次点击时播放,第二次暂停就好了。当我双击时,它会将计时器显示回零,但似乎并没有清除实际的计时器。将只播放在显示被0替换之前停止的地方。