gpt4 book ai didi

Java awt 为什么我只能删除链表中最近添加的内容

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:19 24 4
gpt4 key购买 nike

我正在为我的大学计算机科学项目使用 Java awt 创建一个太空射击游戏。

我通过计时器每 3 秒生成一次敌人,并将其添加到 LinkedList 中,然后 for 循环 渲染它们。

在我的玩家子弹对象的类中,有 if 语句来检查激光是否进入敌人的范围,如果它们都为真,则会将敌人从敌人的范围中移除。 链接列表

但是,仅删除 LinkedList 中最近添加的内容;子弹穿过其他子弹,但什么也没发生。这是我第一次制作游戏,也是我第一次使用LinkedList,所以请原谅任何误解。

Controller 类控制敌人,Laser类是子弹,Enemy类是Enemy对象。还有一个播放器、Main 和 GUI 类。

import java.awt.*;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

public class Controller
{

private LinkedList<Enemy> e = new LinkedList<Enemy>();

Enemy tempEnemy, tempEnemy2
;
Main main;
int refreshSpawn = 3000; //move timer refresh rate
int xpos;
int width;
int ypos;
int height;
Timer spawnTimer = new Timer();

public Controller(Main main)
{
this.main = main;
spawn();
}
public void spawn()
{
spawnTimer.schedule(new TimerTask()
{
public void run() //run method and timer
{
addEnemy(new Enemy(main, (int)(Math.random()*4+2)));
}
}, 0, refreshSpawn);
}
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
{
tempEnemy = e.get(i);
xpos = tempEnemy.getX();
width = tempEnemy.getXsize();
ypos = tempEnemy.getY();
height = tempEnemy.getYsize();
tempEnemy.render(g);
}
}
public void update()
{
for(int i = 0; i < e.size(); i++)
{
tempEnemy2 = e.get(i);
tempEnemy2.move();
}
}
public void addEnemy(Enemy enemy)
{
e.add(enemy);
System.out.println(e.size());
//spawn();
}
public void removeEnemy()
{
e.remove(tempEnemy);
}
public int getX()
{
return xpos;
}
public int getY()
{
return ypos;
}
public int getXsize()
{
return width;
}
public int getYsize()
{
return height;
}
public Enemy getEnemy()
{
return tempEnemy;
}
}
import java.awt.*;

public class Enemy
{
Image ship; //image of enemy ship
int x, y; //ship position
int speed;

public Enemy(Main main, int speed) //constructing enemy
{
this.speed = speed;
ship = main.getImage(main.getDocumentBase(), "enemyShip"+(int)(Math.random()*6+1)+".png"); //picture for enemy ship
x = (int)(Math.random()*900+1); //enemy has a starting position at a random x point
y = -100; //start ship slightly off screen so it doesn't suddenly appear
}
public void move()
{
y += speed;
if(y > 600)
{
y = -100;
x = (int)(Math.random()*900);
}
}
public void render(Graphics g)
{
g.drawImage(ship, x, y, null);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getXsize()
{
return ship.getWidth(null);
}
public int getYsize()
{
return ship.getHeight(null);
}
}
import java.awt.*;

public class Laser
{
Image img; //image of laser
int laserSpeed = 10; //speed of laser
int x, y; //position of laser
int xSize, ySize; //size of laser
Controller cont;
GUI gui;
public Laser(Image img, int x, int y, Controller cont, GUI gui) //constructing laser
{
this.cont = cont;
this.img = img; //setting laser image
this.gui = gui;
xSize = x; //size of laser
ySize = y; //size of laser
}
public void shoot(int x, int y, int shipSize)
{
this.x = x + (shipSize/2) - (xSize/2);
this.y = y;
}
public void move()
{
y -= laserSpeed;
if(x <= cont.getX() + cont.getXsize() && x + xSize >= cont.getX() - cont.getXsize())
{
if(y <= cont.getY() + cont.getYsize() && y > 0)
{
remove();
cont.removeEnemy();
gui.scoreUp(5);
}
}
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getXSize()
{
return xSize;
}
public int getYSize()
{
return ySize;
}
public Image getImage()
{
return img;
}
public void remove()
{
y = -ySize;
x = -100;
}
}

最佳答案

据我所知,tempEnemy 已通过 render 方法分配给 LinkedList 中的最后一个元素。这意味着当您调用 removeEnemy 时,它将删除最后渲染的对象(可能是您添加的最后一个对象)。

你应该做的是告诉Controller它应该使用哪个Enemy,它完全不知道你调用它时的意图是什么......

关于Java awt 为什么我只能删除链表中最近添加的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com