简体   繁体   English

在太空侵略者风格游戏中使用ArrayLIst创建项目符号对象

[英]Using an ArrayLIst to create bullet objects in space invaders style game

I am currently working on a space invaders style game but I have run into a bit of trouble with multiple instances of bullets. 我目前正在开发太空侵略者风格的游戏,但在遇到多个子弹实例时遇到了一些麻烦。 At the moment I can only fire one. 目前,我只能解雇一个。 I have been trying to get it to work with an Array List but I just can't seem to get it to work. 我一直在尝试使其与数组列表一起使用,但似乎无法使其工作。 The closest I got I got it to working was, it fired multiple bullets but they all spawned from the same location as in the bullets didn't spawn in relation to the ships position. 我得到的最接近的结果是,它发射了多发子弹,但是它们都是从相同的位置产生的,因为子弹相对于船只的位置没有产生。 The game also crashed when I removed an object after it exceeded it's boundary. 当我移除一个超出其边界的对象时,游戏也崩溃了。 Can anyone help me to see where I am going wrong. 谁能帮我看看我要去哪里错了。 Here is some code that I have so far the parts commented out are my attempts at getting the array list to work 到目前为止,这是一些代码,我注释掉的部分是我尝试使数组列表起作用的尝试

    import java.util.ArrayList;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.GameContainer;

    public class Player extends Entity 
    {
 private int speed = 5;
 private ArrayList<Bullet> bulletList;
 private boolean firing;
 private Bullet bullet;

public Player()
{   
    bullet = new Bullet();
    //bulletList = new ArrayList<Bullet>();
    this.setImage("ship");
    this.setPosition(350,450);
    this.setDimenseions(100, 100);
    this.createRectangle();
}

@Override
public void entityLogic(GameContainer gc, int deltaTime) 
{
    Input input = gc.getInput();

    if(input.isKeyDown(Input.KEY_A))
    {
        this.x -= speed;
    }

    if(input.isKeyDown(Input.KEY_D))
    {
        this.x += speed; 
    }

    if(input.isKeyDown(Input.KEY_W))
    {
        this.y -= speed;
    }

    if(input.isKeyDown(Input.KEY_S))
    {
        this.y += speed;
    }

    if(input.isKeyPressed(Input.KEY_SPACE))
    {
        firing = true;
        bullet.x = this.getX()+40;

        //BulletList.add(new Bullet());
    }

    if(firing)
    {
        /*Carries out the logic for the bullet*/

        //for(Bullet b : bulletList)
        //{
            //b.entityLogic(gc, deltaTime);
        //}

        //Moves the bullet negativly along the y axis
        bullet.entityLogic(gc, deltaTime);
    }
}

@Override
public void entityRendering(Graphics g) 
{
    g.drawImage(this.getImage(), this.getX(), this.getY());

    if(firing)
    {
        /*Draws each bullet object in the list*/

        //for(Bullet b : bulletList)
        //{
            //b.entityRendering(g);
        //}

        bullet.entityRendering(g);
    }
}

}

First of all forget about your Bullet bullet instance variable. 首先,忘掉Bullet bullet实例变量。 You don't need it, the list is enough. 您不需要它,列表就足够了。

Another thing is that you could use a LinkedList instead that an ArrayList because you don't need random access and you have to add and remove items frequently, when you iterate over bullets to check for collision use a ListIterator<T> and remove them on the fly. 另一件事是,您可以使用LinkedList而不是ArrayList因为您不需要随机访问,并且必须频繁添加和删除项,当您遍历项目符号以检查碰撞时,请使用ListIterator<T>并将其删除苍蝇。

Finally it should be something like: 最后应该是这样的:

List<Bullet> bullets = new ArrayList<Bullet>();

public void entityLogic(GameContainer gc, int deltaTime) {
  // since this method is called many times you should shoot a bullet just every X msec
  if (spacebar pressed) {
    // you spawn a new bullet according to player position
    Bullet bullet = new Bullet(player.x,player.y);
    // you add it to the list
    bullets.add(bullet);
  }

  // destroy bullets which are outside the viewport
  for (int i = 0; i < bullets.size(); ++i) {
    Bullet bullet = bullets.get(i);
    if (bullet.isOutsideBounds()) {
      bullets.remove(i);
      i--;      
    }
}

public void entityRendering(Graphics g) {
  for (Bullet bullet : bullets)
    bullets.entityRenering(g);
}
  }

This is just to give you the basic idea. 这只是给您的基本思想。

I don't know slick2d and how it manages the rendering and logic threads, if they are two different threads then you should use a syncronized list, eg: 我不知道slick2d以及它如何管理渲染和逻辑线程,如果它们是两个不同的线程,那么您应该使用同步列表,例如:

List<Bullet> bullets = Collections.synchronizedList(new ArrayList<Bullet>());

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

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