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


}

そして、これは私の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;
    }
}
4

3 に答える 3

1

クイックフィックスは

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

より長い答えは

クラス同士がどのように相互作用するかを考える必要があります。私がお勧めする本は、「Head First Design Patterns 」という本です。

1 つの例は、GameObject クラスのデリゲート メソッドを使用してコードを簡素化できることです。弾丸、射手、流星は、おそらく、衝突ロジックが を使用して実装されているかどうかを知る必要も気にする必要もありませんRectangle。さらに、衝突ロジックの変更が必要になる場合があります。

上のサンプルメソッドGameObject

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

次に、あなたのコードは

bullet.intersect(shooter.getPlayer())

于 2013-03-22T15:49:33.440 に答える
0

まず、Bullet内への参照が必要Meteorです。たとえば、プロパティを作成します

private Bullet bullet;//現時点では、参照は設定されていません(つまり、bullet == nullを意味します)

自分で定義したセッターメソッドで設定するか、コンストラクターパラメーターとして参照を渡します(オブジェクトの関係とデザインによって異なります)。

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 に答える