2013-03-22 45 views
2

所以我就子彈類:獲得一類從另一個類

import java.awt.*; 

public class Bullet extends GameObject 
{ 
    private Player player; 
    private int deltaX; 

    public Bullet(final Player player, final int deltaX, final int xPos, final int yPos, final int width, final int height, final String img) { 
     this.deltaX = deltaX; 
     this.player = player; 
     this.xPos = xPos; 
     this.yPos = yPos; 
     this.height = height; 
     this.width = width; 
     this.rect = new Rectangle(xPos, yPos, width, height); 
     this.img = getImage(img); 
    } 
    @Override 
    public void draw(Graphics g) 
    { 
     g.drawImage(img, xPos, yPos, width, height, null); 
    } 

    @Override 
    void update(final Shooter shooter, final int id) 
    { 
     if(rect.intersects(player.rect)) 
     { 
      shooter.bullets.remove(this); 
      if(!(shooter.player1.getHull() == 0)) 
      { 
       player.setHealth(player.getHealth() - 1); 
       if(!(getStamina() < 1)) 
        if(shooter.player1.getStamina() > 10) 
         shooter.player1.setStamina(shooter.player1.getStamina() - 10); 
        else 
         shooter.player1.setStamina(shooter.player1.getStamina() - 1); 
       else 
        shooter.player1.setStamina(shooter.player1.getStamina() - 0); 


      } 
      else 
      { 
       player.setHealth(player.getHealth() - 2); 

      } 

      if(!(player.getHull() == 0)) 
       player.setHull(player.getHull() - 2); 
      else 
       player.setHull(player.getHull() - 0); 
     } 





     else if (yPos < -100 || yPos > 2000) 
     { 
      shooter.bullets.remove(this); 
     } 
     else 
     { 
      if(deltaX == 1) 
      { 
       yPos++; 
       rect.y++; 
      } 
      else 
      { 
       yPos--; 
       rect.y--; 
       yPos--; 
       rect.y--; 
      } 

     } 

    } 


    public void setPlayer(Player player) { 
     this.player = player; 
    } 
    public Player getPlayer() 
    { 
     return player; 
    } 

    @Override 
    Image getImage(String img) { 
     return Toolkit.getDefaultToolkit().getImage(img); 
    } 

    public int getDeltaX() { 
     return deltaX; 
    } 

    public void setDeltaX(int deltaX) { 
     this.deltaX = deltaX; 
    } 


} 

這是我的流星類:

import java.awt.*; 

public class Meteor extends GameObject 
{ 
    private Player player; 
    private int deltaX; 


    public Meteor(final Player player, final int deltaX, final int xPos, final int yPos, final int width, final int height, final String img) { 
     this.deltaX = deltaX; 
     this.player = player; 
     this.xPos = xPos; 
     this.yPos = yPos; 
     this.height = height; 
     this.width = width; 
     this.rect = new Rectangle(xPos, yPos, width, height); 
     this.img = getImage(img); 
    } 
    @Override 
    public void draw(Graphics g) 
    { 
     g.drawImage(img, xPos, yPos, width, height, null); 
    } 

    @Override 
    void update(final Shooter shooter, final int id) 
    { 
     if (yPos < -100 || yPos > 2000) 
     { 
      shooter.meteors.remove(this); 
     } 
     else 
     { 
      if(deltaX == 1) 
      { 
       yPos++; 
       rect.y++; 
      } 
      else 
      { 
       yPos++; 
       rect.y++; 
      } 
     } 
     if(rect.intersects(shooter.player1.rect)) 
     { 
      System.out.println("Collision"); 
      shooter.meteors.remove(this); 
      shooter.player1.setHealth(shooter.player1.getHealth() - 100); 
     } 
    } 


    public void setPlayer(Player player) { 
     this.player = player; 
    } 
    public Player getPlayer() 
    { 
     return player; 
    } 

    @Override 
    Image getImage(String img) { 
     return Toolkit.getDefaultToolkit().getImage(img); 
    } 

    public int getDeltaX() { 
     return deltaX; 
    } 

    public void setDeltaX(int deltaX) { 
     this.deltaX = deltaX; 
    } 


} 

現在在流星類我想用這樣的:

if(bullet.rect.intersect(shooter.player1.rect) 
{..} 

但是這不起作用,因爲我不能從中引用子彈類。有什麼辦法可以讓它工作嗎?

這是遊戲對象類

import java.awt.*; 

public abstract class GameObject 
{ 
    protected Rectangle rect; 
    protected int xPos; 
    protected int yPos; 
    protected int height; 
    protected int width; 
    protected Image img; 
    protected int health; 
    protected int stamina; 
    protected int hull; 

    abstract void draw(Graphics g); 

    abstract void update(final Shooter shooter, final int id); 

    abstract Image getImage(String img); 

    public int getHealth() { 
     return health; 
    } 

    public void setHealth(int health) { 
     this.health = health; 
    } 
    public int getStamina() { 
     return stamina; 
    } 

    public void setStamina(int stamina) { 
     this.stamina = stamina; 
    } 

    public Rectangle getRect() { 
     return rect; 
    } 

    public void setRect(Rectangle rect) { 
     this.rect = rect; 
    } 

    public int getHull() { 

     return hull; 
    } 

    public void setHull(int hull) { 
     this.hull = hull; 
    } 

    public int getxPos() { 
     return xPos; 
    } 

    public void setxPos(int xPos) { 
     this.xPos = xPos; 
    } 

    public int getyPos() { 
     return yPos; 
    } 

    public void setyPos(int yPos) { 
     this.yPos = yPos; 
    } 

    public Image getImg() { 
     return img; 
    } 

    public void setImg(Image img) { 
     this.img = img; 
    } 
    public int getHeight() { 
     return height; 
    } 

    public void setHeight(int height) { 
     this.height = height; 
    } 

    public int getWidth() { 
     return width; 
    } 

    public void setWidth(int width) { 
     this.width = width; 
    } 
} 

回答

1

矩形似乎是GameObject類的保護屬性。

您可以在您的Bullet課程中添加一個公共獲取者。

public Rectangle getRect() { 
    return rect; 
} 

然後調用它:

所有的
if(bullet.getRect().intersect(shooter.player1.rect)) 
+0

我很抱歉,但那個給我一個錯誤。這是gameobject類: – Thimo 2013-03-22 15:25:24

+0

@Thimo什麼錯誤? – 2013-03-22 15:26:14

+0

失蹤)in if條件? – cwhsu 2013-03-22 15:39:57

0

首先,你需要內MeteorBullet參考。例如創建一個屬性

private Bullet bullet; //在這一刻參考未設置(這意味着子彈== NULL)

並且由自己定義的設置器方法對其進行設置,或通過參考作爲構造函數參數(取決於對象關係和您的設計)。

RectangleBullet對象的屬性應該被定義爲私有/保護和內Meteor取出由(因爲包封良好的圖案)的吸氣劑。

因此,這意味着

Rectangle rectangle = bullet.getRectangle();

其中getRectangle()是一個定義,由你,吸氣爲rect財產。

另外我建議你閱讀關於封裝。

在開始採取這裏看看

http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

+0

對流星中子彈的引用使它因某種原因崩潰 線程「AWT-EventQueue-0」中的異常java.lang.NullPointerException \t at Meteor。 (Meteor.java:8) \t在Player.update(Player.java:214) \t在Shooter.paintComponent(Shooter.java:124) \t在Shooter.paint(Shooter.java:98) – Thimo 2013-03-22 17:20:45

+0

的NullPointerException裝置總是那個參考沒有設置。請參閱報告異常的代碼中的行,並對其進行調試以檢查其中是否有空。我猜你在當前實現的第8行中不能得到這個異常,因爲你粘貼的這些代碼都有空的第8行Meteor。嘗試訪問對象屬性時拋出NullPointerException,但是您犯了一個錯誤,並且未設置引用。很容易找到問題。使用調試器(例如在eclipse中)可以幫助解決許多更復雜的情況。 – 2013-03-22 17:27:23

+0

我使用IntelliJ,不知道如何使用它。你可以幫我嗎? – Thimo 2013-03-22 17:34:31

1

速戰速決是

bullet.getRect().intersect(shooter.getPlayer().getRect()) 

較長的答案是

你需要給一些思考如何你的clas ses彼此交互。我推薦的一本書被稱爲「首先設計圖案」。

一個示例是,您可以使用GameObject類上的委託方法來簡化代碼。你的子彈,射手和流星可能不需要知道或關心碰撞邏輯是否使用Rectangle實現。另外,您可能需要更改碰撞邏輯。在GameObject

public boolean intersect (GameObject anotherObject) { 
    return this.rect.intersect(anotherObject.rect); 
} 

的抽樣方法然後你的代碼將

bullet.intersect(shooter.getPlayer())

+0

這使得它出於某種原因崩潰:私人子彈項目符號;我把它放在Meteor.java的東西里 – Thimo 2013-03-22 17:17:12