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


}

现在在 Meteor 类中我想使用这个:

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

3 回答 3

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())

于 2013-03-22T15:49:33.440 回答
1

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

你可以在你的Bullet类中添加一个公共 getter。

public Rectangle getRect() {
    return rect;
}

然后调用它:

if(bullet.getRect().intersect(shooter.player1.rect))
于 2013-03-22T15:20:05.647 回答
0

首先,您需要对Bulletwithin的引用Meteor。例如创建一个属性

private Bullet bullet;// 此时没有设置引用(这意味着项目符号 == null)

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

Rectangle对象的属性Bullet应定义为私有/受保护Meteor并由 getter 在内部获取(因为封装良好的模式)。

所以这意味着

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

于 2013-03-22T15:24:44.320 回答