为什么我的 ArrayList 项目在消失时没有自动移除?

huangapple go评论72阅读模式
英文:

Why aren't my ArrayList items removing themselves when they disapear?

问题

我正在尝试让我用Java编写的Asteroids游戏,在ArrayList容器离开屏幕后被移除。我已经找到了在屏幕外停止打印的方法,但是我可以在控制台中看到数组仍在继续增长。不确定下一步该怎么做。

我认为让它们在离开屏幕时被移除的方法之一是使用ArrayList的remove或set功能。在视觉上,一切都消失了,但在控制台中,我的ArrayList仍在增长。我考虑过设置一个长度限制,但不确定是否有比这更好的方法。

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

public class AsteroidGame extends Frame {

    private int FrameWidth = 500;
    private int FrameHeight = 400;

    static public void main(String[] args) {
        AsteroidGame world = new AsteroidGame();
        world.show();
        world.run();
    }

    public AsteroidGame() {
        setTitle("Asteroid Game");
        setSize(FrameWidth, FrameHeight);
        addKeyListener(new keyDown());
        addWindowListener(new CloseQuit());
    }

    public void run() {
        while (true) {
            movePieces();
            repaint();
            try {
                Thread.sleep(100);

            } catch (Exception e) {
            }
        }
    }

    private ArrayList asteroids = new ArrayList();
    private ArrayList rockets = new ArrayList();
    private Station station = new Station(FrameWidth / 2, FrameHeight - 20);

    public void paint(Graphics g) {
        station.paint(g);
        Iterator astIt = asteroids.iterator();
        while (astIt.hasNext()) {
            Asteroid rock = (Asteroid) astIt.next();
            if (rock.y >= 400 || rock.x >= 500){
                rock = null;
            } else {
                rock.paint(g);
            }

        }
        Iterator rocIt = rockets.iterator();
        while (rocIt.hasNext()) {
            Rocket rock = (Rocket) rocIt.next();
            if (rock.y >= 400 || rock.x >= 500) {
                rock = null;
            } else {
                rock.paint(g);
            }
        }

    }

    public void movePieces() {
        if (Math.random() < 0.3) {
            Asteroid newRock = new Asteroid(FrameWidth * Math.random(), 20, 10 * Math.random() - 5, 3 + 3 * Math.random());
            if (newRock.y >= 500 || newRock.x >= 500){

                asteroids.remove(0);

            } else{
                asteroids.add(newRock);
            }
            System.out.println(asteroids.size());

        }

        Iterator astIt = asteroids.iterator();
        while (astIt.hasNext()) {
            Asteroid rock = (Asteroid) astIt.next();
            if (rock.y >= 400 || rock.x >= 500) {
                rock = null;
            } else {
                rock.move();
                station.checkHit(rock);
            }


        }
        Iterator rocIt = rockets.iterator();
        while (rocIt.hasNext()) {
            Rocket rock = (Rocket) rocIt.next();
            if (rock.y >= 400 || rock.x >= 500) {
                rock = null;
            } else {
                rock.move(asteroids);
            }

        }

    }

    private class gameMover extends Thread {

        public void run() {
            while (true) {
                movePieces();
                repaint();
                try {
                    sleep(100);

                } catch (Exception e) {
                }
            }
        }
    }
}
英文:

I am trying to get my Asteroids game in Java to have the ArrayList container be removed once it's off screen. I figured out how to stop having it print when off screen, but can see in my console the array continues to grow. Not sure where to go from here.

I think the way I can get them to be removed when off screen is by either using the remove or set feature with arrayLists. Visually everything is disappearing right, but in the console my ArrayList is still growing. I thought about setting a limit of how long it can be, but not sure if there is a better way than that.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
public class AsteroidGame extends Frame {
private int FrameWidth = 500;
private int FrameHeight = 400;
static public void main(String[] args) {
AsteroidGame world = new AsteroidGame();
world.show();
world.run();
}
public AsteroidGame() {
setTitle(&quot;Asteroid Game0&quot;);
setSize(FrameWidth, FrameHeight);
addKeyListener(new keyDown());
addWindowListener(new CloseQuit());
}
public void run() {
while (true) {
movePieces();
repaint();
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
private ArrayList asteroids = new ArrayList();
private ArrayList rockets = new ArrayList();
private Station station = new Station(FrameWidth / 2, FrameHeight - 20);
public void paint(Graphics g) {
station.paint(g);
Iterator astIt = asteroids.iterator();
while (astIt.hasNext()) {
Asteroid rock = (Asteroid) astIt.next();
if (rock.y &gt;= 400 || rock.x &gt;= 500){
rock = null;
} else {
rock.paint(g);
}
}
Iterator rocIt = rockets.iterator();
while (rocIt.hasNext()) {
Rocket rock = (Rocket) rocIt.next();
if (rock.y &gt;= 400 || rock.x &gt;= 500) {
rock = null;
} else {
rock.paint(g);
}
}
}
public void movePieces() {
if (Math.random() &lt; 0.3) {
Asteroid newRock = new Asteroid(FrameWidth * Math.random(), 20, 10 * Math.random() - 5, 3 + 3 * Math.random());
if (newRock.y &gt;= 500 || newRock.x &gt;= 500){
asteroids.remove(0);
} else{
asteroids.add(newRock);
}
System.out.println(asteroids.size());
}
Iterator astIt = asteroids.iterator();
while (astIt.hasNext()) {
Asteroid rock = (Asteroid) astIt.next();
if (rock.y &gt;= 400 || rock.x &gt;= 500) {
rock = null;
} else {
rock.move();
station.checkHit(rock);
}
}
Iterator rocIt = rockets.iterator();
while (rocIt.hasNext()) {
Rocket rock = (Rocket) rocIt.next();
if (rock.y &gt;= 400 || rock.x &gt;= 500) {
rock = null;
} else {
rock.move(asteroids);
}
}
}
private class gameMover extends Thread {
public void run() {
while (true) {
movePieces();
repaint();
try {
sleep(100);
} catch (Exception e) {
}
}
}
}

答案1

得分: 2

将:

rock = null;

改为:

astIt.remove();

null 赋值给变量,该变量已被赋予列表元素的值,对列表或其中的元素毫无影响;它只影响变量所保存的值。


另外,不错的变量命名选择,rock 既适用于两种类型的对象 - 要么是 "rocket" 的缩写,要么是 "astroid" 的一个合理同义词。

英文:

Change:

rock = null;

to:

astIt.remove();

Assigning null to the variable that has been assigned the value of an element of a List does absolutely nothing to either the List or the element in it; it only affects the value that the variable holds.


As an aside, nice variable name choice of rock - it is appropraite for both types of object - either an abbreviation of "rocket" or a reasonable synonym for an astroid.

答案2

得分: 0

Change rock = null; to asteroids.remove(rock); or astIt.remove();
and it should be fine, also no need to set the variable to null as the garbage collector will take care of it for you.
EDIT
Actually asteroids.remove(rock); will throw an exception as said in the comments of this answer, so nevermind it and use the other.

Also I think when in movePieces() you create a new rock and you check if this new rock is outside the screen, I don't think removing the first asteroid in the ArrayList is correct as you will not add the new rock (which may be right if the rock can actually randomly spawn outside the screen) but you will also remove a maybe fine working asteroid from the ArrayList (and thus from the game and the screen).

So, personally I would change that part of code to:

if (Math.random() < 0.3) {
    Asteroid newRock = new Asteroid(FrameWidth * Math.random(), 20, 10 * Math.random() - 5, 3 + 3 * Math.random());
    if (!(newRock.y >= 500 || newRock.x >= 500)){
        asteroids.add(newRock);
    }
    System.out.println(asteroids.size());
}

But tell me if this works for you.

英文:

Change rock = null; to asteroids.remove(rock); or astIt.remove();
and it should be fine, also no need to set the variable to null as the garbage collector will take care of it for you.<br>
EDIT<br>
Actually asteroids.remove(rock); will throw an exception as said in the comments of this answer, so nevermind it and use the other.

Also I think when in movePieces() you create a new rock and you check if this new rock is outside the screen, I don't think removing the first asteroid in the ArrayList is correct as you will not add the new rock (which may be right if the rock can actually randomly spawn outside the screen) but you will also remove a maybe fine working asteroid from the ArrayList (and thus from the game and the screen).<br>

So, personally I would change that part of code to:

if (Math.random() &lt; 0.3) {
Asteroid newRock = new Asteroid(FrameWidth * Math.random(), 20, 10 * Math.random() - 5, 3 + 3 * Math.random());
if (!(newRock.y &gt;= 500 || newRock.x &gt;= 500)){
asteroids.add(newRock);
}
System.out.println(asteroids.size());
}

But tell me if this works for you.

huangapple
  • 本文由 发表于 2020年9月3日 06:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63714085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定