Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem with extending classes
    primarykey
    data
    text
    <p>I'm making Braid. I have a class <code>Wall</code> that prevents that an object goes into a wall. Everything with <code>Wall</code> is working. But now I'm trying to make the same with the ceilings. My ceiling class extends <code>Wall</code>. I've simply made a constructor like this:</p> <pre><code>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); } </code></pre> <p>Now I have in my level-class an <code>ArrayList</code> of all the Walls and an <code>ArrayList</code> of all the Ceilings. I have to add the Walls on this way:</p> <pre><code>walls.add(new Wall(false, Direction.Right, ..., ...)); </code></pre> <p>And the Ceilings on this way:</p> <pre><code>ceilings.add(new Ceiling(false, ..., ...)); </code></pre> <p>The <code>...</code> replaces the coordinates.</p> <p>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:</p> <pre><code>ceilings.add(new Wall(false, Direction.Down, ..., ...)); </code></pre> <p>I hope I've explained well. Does somebody know what the problem is??</p> <p>Thanks</p> <p><strong>Edit:</strong></p> <p>This is my collition code:</p> <pre><code>public boolean intersects(Rectangle r) { if (!bounds.intersects(r)) { return false; } for (int i = 1; i &lt; 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; } </code></pre> <p><strong>My Code</strong> <hr> '<code>doodelijk</code>' means deadly</p> <p>Wall:</p> <pre><code>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 &lt; 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 &lt; xPositions.length; i++) { int tx = xPositions[i]; int ty = yPositions[i]; if (x &gt; tx) { x = tx; } if (y &gt; ty) { y = ty; } if (x1 &lt; tx) { x1 = tx; } if (y1 &lt; 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); } } </code></pre> <p>Ceiling:</p> <pre><code>package levels; import domein.Direction; public class Ceiling extends Wall { public Ceiling(boolean doodelijk, int[] xPositions, int[] yPositions) { super(doodelijk, Direction.Down, yPositions, xPositions); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload