2013-01-08 2 views
0

현재 우주 침략자 스타일 게임에서 작업하고 있지만 총알이 여러 개있는 경우 문제가 발생했습니다. 지금은 하나만 발사 할 수 있습니다. 나는 그것을 배열리스트와 함께 작동 시키려고 노력해 왔지만 나는 그것을 작동시키지 못하고있다. 가장 가까운 곳에서 여러개의 총알을 발사했으나 총알이 우주선 위치와 관련하여 스폰하지 않은 것과 같은 위치에서 산란했습니다. 오브젝트가 경계를 초과 한 후 오브젝트를 제거하면 게임이 충돌했습니다. 누구든지 제가 잘못 가고있는 곳을 보도록 도와 줄 수 있습니까? 여기에 지금까지 주석 부분 내 시도가 Bullet bullet 인스턴스 변수에 대해 잊지 모든ArrayLIst를 사용하여 우주 침략자 스타일의 게임에서 총알 개체 만들기

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); 
    } 
} 

} 

답변

3

먼저 작동하도록 배열 목록을 얻기에있다가 일부 코드입니다. 너는 필요 없어, 그 목록만으로 충분 해.

또 다른 것은 당신이 ArrayList 당신이 랜덤 액세스를 필요로하지 않기 때문에 것을 대신 LinkedList를 사용할 수 있다는 것입니다 그리고 당신은 ListIterator<T>를 사용하여 제거 충돌를 확인하기 위해 총알을 반복 할 때, 자주 항목을 추가하고 제거해야 즉석에서.

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); 
} 
    } 

이 당신에게 기본 아이디어를주고 그냥 :

마지막으로 뭔가 같이해야합니다.

나는 slick2d을 모르고는 렌더링 및 논리 스레드를 관리하는 방법을 그들은 두 개의 서로 다른 스레드가있는 경우에, 당신은 원 공급기 목록, 예를 들어 사용해야합니다

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

감사합니다. 그것은 그것을 바로 정렬했습니다. 나는 내가 어떻게 보지 못했는지 모른다. 나는 휴일 후에 녹슬 게되기 시작하고 있다고 생각한다. – Crispy91

관련 문제