扩展类的问题

发布于 2024-08-03 08:40:38 字数 5196 浏览 2 评论 0原文

我正在制作《辫子》。我有一个类 Wall 可以防止物体进入墙壁。 Wall 的一切都正常。但现在我正尝试对天花板做同样的事情。 我的天花板类扩展了Wall。我只是做了一个这样的构造函数:

public Ceiling(boolean deadly, int[] xPositions, int[] yPositions)
{
     super(deadly,
     Direction.Down, //Enum: the direction you have to leave the wall (Left, Right)
                     //Here of course down
     xPositions, yPositions);
}

现在我的关卡类中有一个包含所有墙壁的 ArrayList 和一个包含所有天花板的 ArrayList。 我必须以这种方式添加墙壁:

walls.add(new Wall(false, Direction.Right, ..., ...));

并以这种方式添加天花板:

ceilings.add(new Ceiling(false, ..., ...));

... 替换坐标。

如果我检查天花板中是否有一个物体:什么也没有发生:该物体穿过天花板。如果我使用这种方式添加天花板,它会起作用:

ceilings.add(new Wall(false, Direction.Down, ..., ...));

我希望我已经解释得很好。 有人知道问题是什么吗?

谢谢

编辑:

这是我的碰撞代码:

public boolean intersects(Rectangle r)
{
    if (!bounds.intersects(r))
    {
        return false;
    }
    for (int i = 1; i < yPositions.length; i++) {
        Line l = new Line(xPositions[i - 1], yPositions[i - 1], xPositions[i], yPositions[i]);
        if (r.intersectsLine(l)) {
            return true;
        }
    }
    return false;
}

我的代码


'doodelijk' means deadly

墙壁:

package levels;

import domein.Direction;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class Wall
{

    public boolean doodelijk;
    public int[] yPositions;
    public int[] xPositions;
    private Rectangle bounds;
    public Direction directionToLeave;

    public Wall(boolean doodelijk, Direction directionToLeave, int[] yPositions, int[] xPositions)
    {
        this.doodelijk = doodelijk;
        this.yPositions = yPositions;
        this.xPositions = xPositions;
        this.directionToLeave = directionToLeave;
        createRectangle();
    }

    public boolean intersects(Rectangle r)
    {
        if (!bounds.intersects(r))
        {
            return false;
        }
        for (int i = 1; i < yPositions.length; i++) {
            Line l = new Line(xPositions[i - 1], yPositions[i - 1], xPositions[i], yPositions[i]);
            if (r.intersectsLine(l)) {
                return true;
            }
        }
        return false;
    }

    private void createRectangle()
    {
        int x = Integer.MAX_VALUE;
        int y = Integer.MAX_VALUE;
        int x1 = 0;
        int y1 = 0;
        for (int i = 0; i < xPositions.length; i++)
        {
            int tx = xPositions[i];
            int ty = yPositions[i];
            if (x > tx)
            {
                x = tx;
            }
            if (y > ty)
            {
                y = ty;
            }
            if (x1 < tx)
            {
                x1 = tx;
            }
            if (y1 < ty)
            {
                y1 = ty;
            }
        }
        bounds = new Rectangle(x, y, x1 - x + 1, y1 - y +1);
        System.out.println("Rect: " + bounds);
    }

    class Line extends Line2D
    {

        Point2D p1;
        Point2D p2;

        public Line()
        {
            p1 = new Point();
            p2 = new Point();
        }

        public Line(Point2D p1, Point2D p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }

        public Line(double X1, double Y1, double X2, double Y2)
        {
            this();
            setLine(X1, Y1, X2, Y2);
        }

        @Override
        public double getX1()
        {
            return p1.getX();
        }

        @Override
        public double getY1()
        {
            return p1.getY();
        }

        @Override
        public Point2D getP1()
        {
            return p1;
        }

        @Override
        public double getX2()
        {
            return p2.getX();
        }

        @Override
        public double getY2()
        {
            return p2.getY();
        }

        @Override
        public Point2D getP2()
        {
            return p2;
        }

        @Override
        public void setLine(double X1, double Y1, double X2, double Y2)
        {
            p1.setLocation(X1, Y1);
            p2.setLocation(X2, Y2);
        }

        public Rectangle2D getBounds2D()
        {
            return new Rectangle((int) getX1(), (int) getY1(), (int) (getX2() - getX1()), (int) (getX2() - getY1()));
        }
    }

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

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

    public void setPositions(int[] yPos, int[] xPos)
    {
        setXpositions(xPos);
        setYpositions(yPos);
    }
}

天花板:

package levels;

import domein.Direction;


public class Ceiling extends Wall
{
    public Ceiling(boolean doodelijk, int[] xPositions, int[] yPositions)
    {
        super(doodelijk, Direction.Down, yPositions, xPositions);
    }
}

I'm making Braid. I have a class Wall that prevents that an object goes into a wall.
Everything with Wall is working. But now I'm trying to make the same with the ceilings.
My ceiling class extends Wall. I've simply made a constructor like this:

public Ceiling(boolean deadly, int[] xPositions, int[] yPositions)
{
     super(deadly,
     Direction.Down, //Enum: the direction you have to leave the wall (Left, Right)
                     //Here of course down
     xPositions, yPositions);
}

Now I have in my level-class an ArrayList of all the Walls and an ArrayList of all the Ceilings.
I have to add the Walls on this way:

walls.add(new Wall(false, Direction.Right, ..., ...));

And the Ceilings on this way:

ceilings.add(new Ceiling(false, ..., ...));

The ... replaces the coordinates.

If I check if there is an object in a Ceiling: there has nothing happened: the object goes through the ceiling. And if I use this way to add a Ceiling, it works:

ceilings.add(new Wall(false, Direction.Down, ..., ...));

I hope I've explained well.
Does somebody know what the problem is??

Thanks

Edit:

This is my collition code:

public boolean intersects(Rectangle r)
{
    if (!bounds.intersects(r))
    {
        return false;
    }
    for (int i = 1; i < yPositions.length; i++) {
        Line l = new Line(xPositions[i - 1], yPositions[i - 1], xPositions[i], yPositions[i]);
        if (r.intersectsLine(l)) {
            return true;
        }
    }
    return false;
}

My Code


'doodelijk' means deadly

Wall:

package levels;

import domein.Direction;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class Wall
{

    public boolean doodelijk;
    public int[] yPositions;
    public int[] xPositions;
    private Rectangle bounds;
    public Direction directionToLeave;

    public Wall(boolean doodelijk, Direction directionToLeave, int[] yPositions, int[] xPositions)
    {
        this.doodelijk = doodelijk;
        this.yPositions = yPositions;
        this.xPositions = xPositions;
        this.directionToLeave = directionToLeave;
        createRectangle();
    }

    public boolean intersects(Rectangle r)
    {
        if (!bounds.intersects(r))
        {
            return false;
        }
        for (int i = 1; i < yPositions.length; i++) {
            Line l = new Line(xPositions[i - 1], yPositions[i - 1], xPositions[i], yPositions[i]);
            if (r.intersectsLine(l)) {
                return true;
            }
        }
        return false;
    }

    private void createRectangle()
    {
        int x = Integer.MAX_VALUE;
        int y = Integer.MAX_VALUE;
        int x1 = 0;
        int y1 = 0;
        for (int i = 0; i < xPositions.length; i++)
        {
            int tx = xPositions[i];
            int ty = yPositions[i];
            if (x > tx)
            {
                x = tx;
            }
            if (y > ty)
            {
                y = ty;
            }
            if (x1 < tx)
            {
                x1 = tx;
            }
            if (y1 < ty)
            {
                y1 = ty;
            }
        }
        bounds = new Rectangle(x, y, x1 - x + 1, y1 - y +1);
        System.out.println("Rect: " + bounds);
    }

    class Line extends Line2D
    {

        Point2D p1;
        Point2D p2;

        public Line()
        {
            p1 = new Point();
            p2 = new Point();
        }

        public Line(Point2D p1, Point2D p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }

        public Line(double X1, double Y1, double X2, double Y2)
        {
            this();
            setLine(X1, Y1, X2, Y2);
        }

        @Override
        public double getX1()
        {
            return p1.getX();
        }

        @Override
        public double getY1()
        {
            return p1.getY();
        }

        @Override
        public Point2D getP1()
        {
            return p1;
        }

        @Override
        public double getX2()
        {
            return p2.getX();
        }

        @Override
        public double getY2()
        {
            return p2.getY();
        }

        @Override
        public Point2D getP2()
        {
            return p2;
        }

        @Override
        public void setLine(double X1, double Y1, double X2, double Y2)
        {
            p1.setLocation(X1, Y1);
            p2.setLocation(X2, Y2);
        }

        public Rectangle2D getBounds2D()
        {
            return new Rectangle((int) getX1(), (int) getY1(), (int) (getX2() - getX1()), (int) (getX2() - getY1()));
        }
    }

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

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

    public void setPositions(int[] yPos, int[] xPos)
    {
        setXpositions(xPos);
        setYpositions(yPos);
    }
}

Ceiling:

package levels;

import domein.Direction;


public class Ceiling extends Wall
{
    public Ceiling(boolean doodelijk, int[] xPositions, int[] yPositions)
    {
        super(doodelijk, Direction.Down, yPositions, xPositions);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-08-10 08:40:38

您确定 xposition 和 yposition 参数的设置吗?也就是说,天花板真的在你想象的地方吗?您的构造函数的两个变体是否相同?

Are you sure about your settings of the xposition and yposition arguments? That is, is the ceiling really where you think it is? Is it the same in your two variants of the constructor?

寄居者 2024-08-10 08:40:38

我猜问题出在检查“冲突”的代码中。我看不出你给的那个有什么问题。

I guess the problem is in the code that checks for the "collision". I cant see any problem in the one you've given.

緦唸λ蓇 2024-08-10 08:40:38

我没有发现你的课程有任何明显的问题。其他地方可能存在错误,您必须发布完整的类以便我们识别它。

I don't see any obvious problems with your classes. There might be a bug somewhere else and you would have to post the complete classes for us to identify it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文