【发布时间】:2020-05-05 22:52:21
【问题描述】:
正如标题所说,我遇到了对象重叠的问题。我希望它们能够重叠,因为我需要对象 X 位于对象 Y 的顶部以获得一个点,如果我将对象 X 从位于 Y 的顶部移除,我将移除所述点。那么我的问题是,如果对象 Y 是在对象 X 之前创建的,一旦我将对象 X 放在 Y 之上,我就不能再移动它并且它总是输出到控制台,对象 Y。我想知道是否会有更简单的解决这个问题的方法。
我尝试前进,但盒子没有让步:
[
这是我用来生成关卡数据的代码
private List<ImageTile> createLevel(){
ArrayList<Movable> aux1 = new ArrayList<>();
ArrayList<Immovable> aux2 = new ArrayList<>();
try {
Scanner sc = new Scanner(new File ("levels/level" + levelID + ".txt"));
String line = "";
while(sc.hasNextLine()) {
for (int y = 0; y < HEIGHT; y++) {
line = sc.nextLine();
for(int x = 0; x < WIDTH ; x++) {
levelObjects.add(new floor(new Point2D(x,y), "Floor", 0));//adding the floor before anything else
char letter = line.charAt(x); // checking the character in the X coordinate
if (letter == 'E') { // in case, there's a E, that's the starting position of the player
player = new Forklift(new Point2D(x,y), "Forklift_U", 2);
levelObjects.add(player);
objects.add(player);
} else if(letter != ' ') {
AbstractObject obj = ObjectCreation.readChar(letter, x, y); // going to look for the object in Factory to be put in the X and Y position
if(obj instanceof Immovable) {
aux2.add((Immovable) obj);
}else if(obj instanceof Movable) {
aux1.add((Movable) obj);
}
// comp.comprator(obj, obj);
objects.add(obj);
levelObjects.add(obj);//implementing said object into the Level
}
}
}
}
sc.close(); //Closing Scanner
} catch (FileNotFoundException e) {
System.out.println("No levels found!");
}
still = aux2;
moving = aux1;
return levelObjects;
}
然后我使用通用移动功能检查盒子(或实例 Movable 的任何对象部分)是否可以移动到下一个位置
public void move(Direction direction) {
Vector2D pos = direction.asVector(); // Transforming the direction in a vector right off the bat.
Point2D currentPosition = getPosition(); // getting the current position of either player or object.
Point2D nextPosition = currentPosition.plus(pos); // the next position as to which the player or the object intend to go to.
if(isTransposable(nextPosition)) { // calls the function to see if the object is able to move to the next position, this prevents the boxes from pushing up into the walls and from into each other {
setPosition(nextPosition); //if it can, then the object will go to the next position
}
}
这是检查物体是否可以前进到下一个位置;
public boolean isTransposable(Point2D pos) { // is the object able to move to the next position
for(AbstractObject obj1 : Game.getInstance().getObjects()) { // for-each to get all the objects, with the exception of the floor, from the game.
if((obj1.isBlockable() && obj1.getPosition().equals(pos))){ // if the object is able to block and if it's position is the same as the nextPosition.
return false;
}
}
return true; // otherwise it's able to be walked on.
}
【问题讨论】:
-
什么意思?我不知道在此处发布的要求或大部分时间需要包含的内容...如果您能告诉我,我将不胜感激!
-
我不知道输入代码是强制性的,但话说回来,我应该在发帖后立即考虑实现它...
-
嗯,现在解决了,所以我删除了我的 cmets :)