2013-03-22 107 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