简体   繁体   English

Java awt为什么我只能删除链表的最新添加

[英]Java awt Why can I only remove most recent addition to Linked List

I'm creating a space shooter game in Java awt for my college computer science project. 我正在为大学计算机科学项目用Java awt创建一个太空射击游戏。

The enemies that I have spawn every 3 seconds via a timer and are added to a LinkedList , and a for loop renders them all. 我通过计时器每3秒钟产生一次的敌人被添加到LinkedList ,并且for loop将它们全部渲染。

In the class I have for my player's bullet object, there are if statements to check whether the laser comes into the bounds of an enemy, and if they are all true it removes the enemy from the LinkedList . 在我为玩家的子弹对象准备的课程中,有if语句来检查激光是否进入敌人的范围,如果它们全部成立,它将从LinkedList移除敌人。

However, only the most recent addition to the LinkedList is being removed; 但是,仅删除了LinkedList最新添加的内容。 the bullet passes through the others and nothing happens. 子弹穿过其他子弹,什么也没发生。 This is my first time making a game, and the first time I've ever used a LinkedList , so excuse any misunderstandings. 这是我第一次玩游戏,也是我第一次使用LinkedList ,因此请谅解。

The controller class controls the enemies, the Laser class is the bullet and the Enemy class is the Enemy object. 控制器类控制敌人, Laser类是子弹, Enemy类是Enemy There's also a player, Main and GUI class. 还有一个播放器,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;
}
}

From what I can tell, tempEnemy is assigned to the last element in the LinkedList by the render method. 据我所知, tempEnemyrender方法分配给LinkedList的最后一个元素。 This means that when you call removeEnemy it is removing the last rendered object (likely the last object you added). 这意味着,当您调用removeEnemy它将删除最后一个渲染的对象(可能是您添加的最后一个对象)。

What you should be doing is telling the Controller which Enemy it should be using, it has absolutely no idea what your intentions are when you call it... 您应该做的就是告诉Controller它应该使用哪个Enemy ,当您调用它时,它绝对不知道您的意图是什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM