*删除了Irrelvant方法,例如块色等。
建立一个打砖块游戏,当然要使用多个类。 当从一个类到另一个类调用方法时,即时通讯出现问题,无法解析符号。
我尝试检查从该方法继承的类是否为公共类,还尝试使用该函数的getter(不确定是否可以,Java的新手)。
public class Rectangle {
private double width;
private double height;
private Line[] rectangleLines;
private Point upperLeft;
/**
* Instantiates a new Rectangle.
*
* @param upperLeft the upper left
* @param width the width
* @param height the height
*/
// Create a new rectangle with location and width/height.
public Rectangle(Point upperLeft, double width, double height) {
this.upperLeft = upperLeft;
this.width = width;
this.height = height;
this.rectangleLines = new Line[4];
this.initRectangleLines();
}
/**
* Instantiates a new Rectangle.
*
* @param x the x
* @param y the y
* @param width the width
* @param height the height
*/
public Rectangle(double x, double y, double width, double height) {
this.upperLeft = new Point(x, y);
this.width = width;
this.height = height;
this.rectangleLines = new Line[4];
this.initRectangleLines();
}
private void initRectangleLines() {
this.rectangleLines[0] = new Line(upperLeft, new Point(upperLeft.getX() + this.width, upperLeft.getY()));
this.rectangleLines[1] = new Line(new Point(upperLeft.getX() + this.width, upperLeft.getY())
, new Point(upperLeft.getX() + this.width, upperLeft.getY() + height));
this.rectangleLines[2] = new Line(new Point(upperLeft.getX() + width, upperLeft.getY() + height)
, new Point(upperLeft.getX(), upperLeft.getY() + height));
this.rectangleLines[3] = new Line(new Point(upperLeft.getX(), upperLeft.getY() + height), upperLeft);
}
/**
* Intersection points java . util . list.
*
* @param line the line
* @return the java . util . list
*/
// Return a (possibly empty) List of intersection points
// with the specified line.
public java.util.List<Point> intersectionPoints(Line line) {
List<Point> intersections = new ArrayList<Point>();
for (int i = 0; i < rectangleLines.length; i++) {
if (rectangleLines[i].isIntersecting(line))
intersections.add(rectangleLines[i].intersectionWith(line));
}
/*for (Point element : intersections)
{
}*/
return intersections;
}
/**
* Gets top.
*
* @return the top
*/
public Line getTop() {
return this.rectangleLines[0];
}
/**
* Gets right.
*
* @return the right
*/
public Line getRight() {
return this.rectangleLines[1];
}
/**
* Gets bottom.
*
* @return the bottom
*/
public Line getBottom() {
return this.rectangleLines[2];
}
/**
* Gets left.
*
* @return the left
*/
public Line getLeft() {
return this.rectangleLines[3];
}
/**
* Gets width.
*
* @return the width
*/
// Return the width and height of the rectangle
public double getWidth() {
return this.width;
}
/**
* Gets height.
*
* @return the height
*/
public double getHeight() {
return this.height;
}
/**
* Gets upper left.
*
* @return the upper left
*/
// Returns the upper-left point of the rectangle.
public Point getUpperLeft() {
return this.upperLeft;
}
/**
* Is colliding boolean.
*
* @param p the point.
* @return the boolean
*/
public boolean isColliding(Point p) {
return this.upperLeft.getX() <= p.getX()
&& this.upperLeft.getX() + this.width >= p.getX()
&& this.upperLeft.getY() <= p.getY()
&& this.upperLeft.getY()+ this.height >= p.getY();
}
}
import java.awt.*;
/**
* The type Block.
*/
public class Block implements Collidable {
private Rectangle rect;
private Color color;
private int hits;
/**
* Instantiates a new Block.
*
* @param r the r
* @param c the c
* @param h the h
*/
public Block(Rectangle r, Color c, int h) {
this.rect = r;
this.color = c;
this.hits = h;
}
/**
* Instantiates a new Block.
*
* @param upperLeft the upper left
* @param width the width
* @param height the height
* @param c the c
* @param h the h
*/
public Block(Point upperLeft, double width, double height, Color c, int h) {
this.rect = new Rectangle(upperLeft, width, height);
this.color = c;
this.hits = h;
}
@Override
public Rectangle getCollisionRectangle() {
return this.rect;
}
@Override
public Velocity hit(Point collisionPoint, Velocity currentVelocity) {
boolean changeX = false;
boolean changeY = false;
if (this.rect.getTop().isColliding(collisionPoint)){
}
}
}
问题出在最后一段,试图使用来自Block中Rectangle的isColliding并显示标题错误。