从另一个班级获取类型

时间:2013-03-22 15:15:16

标签: java oop

所以我得到了子弹课程:

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


}

这是我的Meteor课程:

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


}

现在在Meteor课上我想用这个:

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

但这不起作用,因为我无法从中引用子弹类。有没有办法使它工作?

这是GameObject类

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

3 个答案:

答案 0 :(得分:1)

Rect似乎是GameObject类的受保护属性。

您可以在Bullet课程中添加公共getter。

public Rectangle getRect() {
    return rect;
}

然后调用它:

if(bullet.getRect().intersect(shooter.player1.rect))

答案 1 :(得分:1)

快速解决方法是

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

更长的答案是

您需要考虑一下您的课程如何互相交流。我推荐的一本书名为“ Head First Design Patterns 。”

一个例子是您可以使用GameObject类上的委托方法来简化代码。您的子弹,射手和流星可能不需要知道或关心是否使用Rectangle实现了碰撞逻辑。此外,您可能需要更改碰撞逻辑。

GameObject

上的示例方法
public boolean intersect (GameObject anotherObject) {
   return this.rect.intersect(anotherObject.rect);
}

然后你的代码将是

bullet.intersect(shooter.getPlayer())

答案 2 :(得分:0)

首先,您需要在Bullet内引用Meteor。例如,创建一个属性

  

private Bullet bullet; //此时未设置引用(表示bullet == null)

并通过自己定义的setter方法设置它,或者将引用作为构造函数参数传递(取决于对象关系和设计)。

Rectangle对象的

Bullet属性应定义为私有/受保护,并且由getter获取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

相关问题